I am trying to make a ‘brush’ tool in AS3 (pure, not Flex) which simulates handwriting, making strokes to be smooth instead of cornered. Then, the trace must be reduced to cubic bezier curves which can be dragged and deformed, affecting the previously drawn path (like the illustrator’s pen tool).
I’m tracking the mouse movement to get a set of points to draw the path. As far as I know, I need to do a B-Spline path using that set of points. Then I should reduce it to cubic bezier curves (adding the the ‘pen tool’ functionality to the path).
I have already developed the pen tool, using an algorithm which reduces Cubic Beziers to Quadratic Beziers (and then using the Flash curveTo function). But I have no idea how to create a B-Spline (or another simplification), an then reduce it to Bezier curves.
Do you know any way to accomplish this?
The jhotdraw is an opensource project in Java for drawing. It converts free hand drawings into cubic bezier curves. The source is available – download and translate. Don’t get scared at the size of the project : you need only a couple of classes namely:
While translating start by changing all the collection declarations to Arrays (use vectors if you are targeting only FP10 users). I’ve some regexes that you might find useful in the conversion – I can post them if you want.
Here is a list of regexes that you might find useful. In each pair, paste the first one into search text area and second one into replace area, check the regex check box and use Find and Replace buttons. Don’t use
Replace All– none of these are guaranteed to be foolproof.Replace all
int/double namedeclarations withvar name:NumberReplace all
Point2D.Double namedeclarations withvar name:PointReplace all
int/double namedeclarations in function signatures withname:NumberReplace all
Point2D.Double namedeclarations in function signatures withname:PointBefore changing method signatures, make sure all methods are static:
Replace method signatures to AS format
Replace ArrayList.get(index) with array[index] //Warning: fails for list.get(list.size() – 1)
Replace
ArrayList.set(index, element)witharray[index] = element//Warning: fails for list.set(i, list.size())
Replace
arraylist.add(object)witharray.push(object)Replace method signatures to AS format (non static methods)
Replace all
int/double/point/boolean namedeclarations in function signatures withname:typeReplace all variable declarations in its own line with an = to AS format
change placing of braces.
change
} elseinto} \n elseReplace 4 variable declarations in a single line to AS in different lines
Replace array declarations
Remove () casting – AS compiler doesn’t like them
Replace max etc into Math.max – AS doesn’t have static imports