For this article we are going to draw something with a little complexity to better compare the two languages. I have chosen a warning icon I made in Photoshop.
An Initial Difference
As we dive right into the code try not to be too concerned about the math. The point of this article is not to explain the trigonometry behind a triangle, just to compare how we draw the triangle. That said, the first thing we do is declare a few variables.
The code is similar in both languages, but two differences appear immediately and will be consistent throughout this article:
Basic Shape: A Triangular Path
The icon we are drawing has three major components:
Although beginning a path is slightly different, both JS and AS3 use the methods
To add a little complexity, let’s round the corners of the triangle (and we are not going to take the stroke shortcut I used in in my previous article: HTML 5 Canvas Example).
To round the corners we will use Bézier curves, which are supported by both languages. The type of Bézier curve will be quadratic (opposed to cubic). Quadradtic Bézier curves have two anchor points; the curve of the line between them is defined by one control point (Cubic Bézier curves have two control points).
Adding a pair of control points at each corner will give a rounded effect.
quadraticCurveTo on the HTML5 canvas to create the control point. ActionScript 3 uses the method
Create the Inner Stroke Path
Next we create a smaller triangle within our first triangle. Later we will stroke it and utilize standard properties to curve the border of our stroke for us.
With any icon, we have a message. The warning standard is an exclamation point (aka a “bang” character) which we add in the center of our icon. We could use a text representation but we will not do that for a various reasons (the user might not have that font, the size cost for embedding an entire typeface just for one character is not worth it, etc.)
Again the major difference between the two approaches are the methods
To finish our bang character, we add a circle:
Draw Inside the Lines
The paths are complete:
We see the results below:
A Subtle Shadow
We are almost there, but there is something missing. Let’s add a subtle shadow.