I recently had need of an arrow drawing function in Flash which I wanted to use to annotate diagrams. I was unable to find one through Google that suited my needs, so I decided to roll my own Actionscript class for drawing arrows. I tried to keep the arrow drawing interface easy to use but flexible. It draws a clean vector outline of the arrow, so you can use the graphics.lineStyle and graphics.beginFill to set the color styles.

Figure 1. A sample of the various styles of arrow that can be easily created with GraphicsUtil.drawArrow
You need only provide a graphics instance as well as the start and end points of the arrow. The GraphicsUtil class can handle the rest. This is the simplest arrow you can draw. It will take on whatever lineStyle and fill properties you set on the graphics instance. In this regard, it functions similarly to the graphics.drawRect and graphics.drawCircle methods.
Code Sample 1. The simplest use case for GraphicsUtil.drawArrow
import com.dncompute.graphics.GraphicsUtil;
//Create a display object to draw into and set the colors
var shape:Shape = new Shape();
shape.graphics.lineStyle(1,0x000000);
shape.graphics.beginFill(0x999999);
//Draw an arrow from 30,30 to 100,100
GraphicsUtil.drawArrow(
shape.graphics,
new Point(30,30),new Point(100,100)
);

Figure 2. The Actionscript shown in Code Sample 1 will generate this arrow; it is the default arrow that will be created if you don’t pass any styling parameters.
If you need to customize the look of the arrow, you can do so in Actionscript by passing in a style object. This can be done using the shorthand generic object notation (i.e., an associate array). You can also style the arrow using the more OOP friendly method of creating an instance of the ArrowStyle class.
Code Sample 2a. Shorthand Styling Using Object Notation
import com.dncompute.graphics.GraphicsUtil;
import flash.display.Shape;
//Create a display object to draw into and set the colors
var shape:Shape = new Shape();
shape.graphics.lineStyle(1,0x999999);
shape.graphics.beginFill(0x000000);
//Draw an arrow from 30,30 to 100,100
GraphicsUtil.drawArrow(shape.graphics,
new Point(30,30),new Point(100,100),
{shaftThickness:5,headWidth:40,headLength:60,
shaftPosition:.25,edgeControlPosition:.75}
);
Code Sample 2b. Style Using the ArrowStyle Class
import com.dncompute.graphics.GraphicsUtil;
import com.dncompute.graphics.ArrowStyle;
//Create a display object to draw into and set the colors
var shape:Shape = new Shape();
shape.graphics.lineStyle(1,0x999999);
shape.graphics.beginFill(0x000000);
//Set the arrow style
var style:ArrowStyle = new ArrowStyle();
style.shaftThickness = 5;
style.headWidth = 40;
style.headLength = 60;
style.shaftPosition = .25;
style.edgeControlPosition = .75;
//Draw an arrow from 30,30 to 100,100
GraphicsUtil.drawArrow(shape.graphics,
new Point(30,30),new Point(100,100),
style
);

Figure 3. The Actionscript in Code Sample 2a and 2b will generate the same styled arrow you see here.
While it’s relatively easy to style the arrows, the style parameters may seem unintuitive at first. To help alleviate any confusion, I’ve provided the following arrow style generator which will allow you to interactively create an arrow style and print out the generated code. You can also use the “randomize” button to morph the shape into a randomly styled arrow.
The drawArrow method uses a line intersection function based on Erik Gustavsson’s tutorial and sample code. I recommend Mr. Gustavsson’s tutorial if you are interested in the math involved in drawing the arrow outline.
Yeah, great stuff.
Nice! A usable tool right out of the box.
Very usefull… thx for share
Thank you thank you thank you! This is exactly what I needed. Thank you again for making this work available!
hey that#s so cool! You can make other stuff than arrows too, just use ur imagination… I made a giant dick! w00t!
Many thanks, this is very helpful. And your style editor just rocks!
thank you, saved me a lot of work!
Is this code only valid in AS3? Cause I am programming in AS2 atm and there is no Shape class.
Thank you for this code
but how to use it?
where i must write this code ???
Great work, very useful nice litte class
:D
Coool, thank you!
Thanks very much Noel! I made a few changes to support curved lines with your arrow tips. Check out my changes at my blog and maybe you can incorporate them into your package…
http://www.huyler.net/flexblog/2009/02/18/drawing-curved-lines-and-arrows-in-flex/
@Chris – Great work! I didn’t even think about using this with curved arrows. If I find time down the line, I’d love to incorporate your changes into my package.
Thanks for an awesome feature!
Just a side note, to help a beginner, could you explain how to import your package into a project? It’s embarrassing, but I am having trouble getting it “in there” so I can use it.
Flex documentation is tedious. If it weren’t for all the bloggers like yourself, I’d be really lost.
Thanks again!
if you need help with a flash as3 scenario send me a quick email. flash is my thing, happy to help any of you.
legitadobe at gmail dot com
@John
Your classes need to be in any directory that’s a part of your “classpath”. Unfortunately, your classpath can point at different directories depending on how your project is set up. In Flash, unless you’ve set it otherwise, the classpath is usually pointed at wherever your “.fla” file is. So, if your fla is in a folder called “myProject”, your directory should look something like:
myProject > com > dncompute >graphics > GraphicsUtil.as
In Flex, the “com” folder would typically be placed in the “src” directory of your project.
Thank you so much! was looking for a quick easy way to draw arrows showing the direction of a line and your library offer much much more!
very help ful. You saved me ton of time
thank you
This solution is very good, but, i have a little problem. I have a flash player 10, and program in AS 3.0, but my application doesn’t run. Any solution for me? The stage was white.
Thanks.
Awesome stuff !!! Thank you very much for sharing. It saved a lot of time.
Very nice!
You helped me a lot, thanks!
Outstandingly helpful tool, Noel. If you wouldn’t mind offering a little free support to a noob…
I’m using this class to add markers as overlays on images. I create a new Shape object and then use Shape.graphics methods to load the arrow into that Shape. I use the same object to load ellipses and rectangles, all driven by XML proprties. All looks good, but I find that if I add interaction to the Shape by attaching mouse event listeners, it works for the ellipses and rectangles but not for the arrows. Am I missing something obvious?
Kindly ignore my question. It was a simple mistake — I added the shape to the wrong display container. Sorry. And thanks again for a great utility.
How can I draw the a line then a line with an arrow? Basically a line with turn and arrow at the end. I modified the code, but the intersection between the line and the line with the arrow is not line up.
This is a great library, thank you very much.
I have a question though. If I want to move the arrow triangle so it appears half way down the line, is that possible? Im trying to make a flowchart-like app and I need the flow arrows to appear in the middle of the line.
Thanks,
Luke
how to remove an arrow. removechild is not working ? new to as3 pls help. or point to some source where i can find a solution.
Thanks, saved me a lot of work. Appreciate the config tool as well, really went the extra mile. :)
thank you! Great work, works out of the box :)
Really excellent contribution [to the community].
Would you mind if I added this functionality to Degrafa ?
@Thomas Burleson
Not at all, feel free to use it wherever you want. I would always appreciate a mention/link in the credits for the project.
Thank you very much! solved all my project needs.
thanks so much! this is exactly what I needed.
Thank you so much! This is perfect.
dubbeat says:
Thats really cool!!
No more dodgey arrows from Google images for me!! :)