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.
dubbeat says:
Thats really cool!!
No more dodgey arrows from Google images for me!! :)
Martin says:
Yeah, great stuff.
Angel Romero says:
Nice! A usable tool right out of the box.
`GraphicsUtil. A Utility Class for Drawing Arrows » ideaography` says:
[...] http://www.dncompute.com/blog/2008/07/17/graphicsutil-a-utility-class-for-drawing-arrows.html [...]
Nicolas Prof says:
Very usefull… thx for share
Kelso’s Corner » Blog Archive » GraphicsUtil. A Utility Class for Drawing Arrows (Ideaography) says:
[...] handy little actionscript class (download) from doesnotcompute to, well, draw lines and arrows. The great thing about this article [...]
News Flash: Roundup of Flash-related News (July 22, 2008) - Down the Foxhole says:
[...] ActionScripting Arrows New York-based Flash developer Noel Billig has been spending his spare time developing a nifty little Flash tool and ActionScript class to dynamically draw arrows. For all the details, check out Noel’s blog. [...]
Bookmarks about Drawing says:
[...] – bookmarked by 4 members originally found by stardoctor on 2008-08-01 GraphicsUtil. A Utility Class for Drawing Arrows http://www.dncompute.com/blog/2008/07/17/graphicsutil-a-utility-class-for-drawing-arrows.html – [...]
Alec says:
Thank you thank you thank you! This is exactly what I needed. Thank you again for making this work available!
AS3 | Actionscript 3 Classes « Flash Enabled Blog says:
[...] – GraphicsUtil – A Utility Class for Drawing Arrows [...]
矢印 - ActionScript - Rest Term says:
[...] doesnotcompute » GraphicsUtil. A Utility Class for Drawing Arrows [...]
randygland says:
hey that#s so cool! You can make other stuff than arrows too, just use ur imagination… I made a giant dick! w00t!
Canuckster says:
Many thanks, this is very helpful. And your style editor just rocks!
virtualize says:
thank you, saved me a lot of work!
Kaven says:
Is this code only valid in AS3? Cause I am programming in AS2 atm and there is no Shape class.
Alias says:
Thank you for this code
but how to use it?
where i must write this code ???
Roamer says:
Great work, very useful nice litte class
:D
peko says:
Coool, thank you!
Christopher Huyler’s Flex Blog » Blog Archive » Drawing curved lines and arrows in Flex says:
[...] Billig of dncompute.com created a great API for drawing arrows at the end of lines. Utilizing his API we can quickly [...]
Chris says:
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/
Noel says:
@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.
John says:
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!
legit says:
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
Noel says:
@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.asIn Flex, the “com” folder would typically be placed in the “src” directory of your project.
David says:
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!
firemonk says:
very help ful. You saved me ton of time
thank you
Maycon says:
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.
Paritosh Bisi says:
Awesome stuff !!! Thank you very much for sharing. It saved a lot of time.
Mario Junior says:
Very nice!
You helped me a lot, thanks!
Jerome Cance says:
Excellent! This works like a charm. Thanks a lot.