Ai2Canvas Tutorial
Ai2Canvas Tutorial
Introduction
The Ai->Canvas plug-in enables Adobe Illustrator to export vector and bitmap artwork directly to an
HTML5 canvas element using JavaScript drawing commands. Animation can be added to control
rotation, scaling, opacity, and motion along a path. Then, events can be used to trigger other
animations. Finally, the exported HTML and JavaScript can be extended and used in your own
applications running on the latest versions of Internet Explorer, Firefox, Chrome, Safari, and Opera.
The Tutorial Assets folder includes the graphic and audio assets used in the examples.
You can also watch a video version of these tutorials on YouTube.
Drawing
Almost any Illustrator artwork can be exported to a HTML5 canvas element. Where canvas lacks support
for a specific feature, Ai->Canvas automatically rasterizes the artwork to a bitmap and properly positions
the image on the canvas.
Artwork
(related video on YouTube)
Images
(related video on YouTube)
Rasterizing
(related video on YouTube)
Animation
Bring your artwork to life by adding animations to your Illustrator drawings. Ai->Canvas supports
rotation, scaling, fading, and movement along a path. For more advanced users, add triggers that start
one animation when another animation completes.
Moving
(related video on YouTube)
Example
1. Create a new layer called earthOrbit(type: animation; direction: forward; duration: 10);. This
sets the layer as an animation path, sets a forward direction, and sets the duration of the
animation to 10 seconds.
2. In the new layer, use the ellipse tool to draw a circle with the sun at its center. Set its fill to
none and its stroke to gray. This represents the motion path of Earths orbit.
3. To associate the earth layer with the animation path (and reposition its origin point), change the
layer name to earth(origin: center; animation: earthOrbit);.
4. To associate the moon layer, change the layer name to moon(origin: -1.5, 0.5; rotate-direction:
cw; animation: earthOrbit);.
5. Choose File/Export/<canvas>, and watch the Earth and moon move along the path.
6. Create a new layer called rocketPath(type: animation; direction: forward; duration: 10);.
7. In the new rocketPath layer, use the pen tool to draw a path for the rocket to follow.
8. To associate the rocket with the path (and reposition its origin point), rename the rocket layer to
rocket(rasterize:rocket; origin: center; animation: rocketPath; follow-orientation: 90);. The
follow-orientation property sets the front of the rocket to 90 degrees (straight up) and
configures the drawing to follow the orientation of the path. If the rocket artwork isnt facing
straight up, be sure to rotate it.
9. To allow the rocketPath to begin and end outside of the stars, change the stars layer name to
stars(crop: yes);. The crop property clips the artwork to the bounds of the layer.
10. Choose File/Export/<canvas>, and watch the rocket follow the new path.
Easing
(related video on YouTube)
4. Add a timing function that slows down the asteroid as it progresses by renaming the
asteroidPath layer to asteroidPath(type: animation; duration: 8; iterations: 1; timing-function:
quintEaseOut);.
5. Choose File/Export/<canvas>, and watch the asteroid slow down as it moves along the path.
6. To make the asteroid look like its moving into the distance, add a scale animation to the
asteroid layer by renaming it to asteroid(origin: center; animation: asteroidPath; scaledirection: shrink; scale-duration: 8; scale-iterations: 1; scale-timing-function: quintEaseIn);.
7. Choose File/Export/<canvas>, and watch the asteroid shrink into the distance.
Triggers
(related video on YouTube)
Code
The purpose of Ai->Canvas is to accelerate your HTML5 canvas development. The HTML and JavaScript
that it exports is meant to be copied into your own application and extended. Learn how to subscribe to
built-in events, determine if a shape has been clicked, and debug the exported code.
Events
(related video on YouTube)
Like the built-in trigger feature (see Triggers under Animation), your
JavaScript code can subscribe to animation clock events and perform a
custom action when that event occurs. For example, you may want to
play a sound.
Example
1. Open the exported HTML file in your favorite text editor (Visual
Studio, Notepad, TextMate, etc.).
2. Add a new audio element just beneath <canvas></canvas> near the end of the file:
<audio id="whoosh" preload>
<source src="whoosh.mp3" />
<source src="whoosh.wav" />
</audio>
Be sure to copy both audio files to the same folder.
3. Add a new JavaScript function called playSound(); after the init() function:
function playSound() {
var whoosh = document.getElementById("whoosh");
whoosh.play();
}
4. Last, find the array index for asteroidPath by looking at the var animations = line near the top
of the file (index numbers start at 0). Then add the following to the init(); function just before
// Start animation clocks: animations[0].pathClock.started.subscribe(playSound);. This causes
the playSound(); function to be called when the asteroidPath animation starts.
5. Save the file, and reload it in the browser. The sound should play when the asteroid animation
starts.
Interaction
(related video on YouTube)
Example
1. Create a new layer named sunSpot();, and use the ellipse tool to draw a circle that overlaps
the clickable area of the sun.
2. Because the sunSpot layer is only used to configure a path to check for mouse clicks, there is no
need to actually see it on the canvas. So, set both fill and stroke styles to none.
3. To create a supernova animation that will start when the sunSpot area is clicked, rename the
sunGlow layer to: sunGlow(origin: center; scale-direction: grow; scale-duration: 6; scaleiterations: 1; scale-multiplier: 12; scale-offset: 1; scale-timing-function: expoEaseOut; alphadirection: fade-out; alpha-duration: 6; alpha-iterations: 1);. The scale-multiplier property
multiplies the scale value, and the scale-offset property adds a percentage of the original range.
4. Choose File/Export/<canvas>, and open the exported file in a text editor.
5. Add a new JavaScript function called sunClick(); after the init() function:
function sunClick(e) {
sunSpot(ctx);
var x = e.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft - canvas.offsetLeft;
var y = e.clientY + document.body.scrollTop +
document.documentElement.scrollTop - canvas.offsetTop;
if (ctx.isPointInPath(x, y)) {
sunGlow.scaleClock.restart();
sunGlow.alphaClock.restart();
}
}
6. To prevent the sunGlow animations from starting when the page loads, delete
sunGlow.scaleClock.start(); and sunGlow.alphaClock.start(); from the init() function.
7. At the end of the init(); function, add canvas.addEventListener("click", sunClick, false); to
capture mouse clicks.
8. Save the file, and reload it in the browser. Click the sunSpot area to start the animations.
Debugging
(related video on YouTube)
2. Note the image, animation, and draw information located below the canvas. For example, the
index of an animation function makes it easy to refer to the animation path in code (e.g.
animations[1]).
3. To scrub the animations, click the mouse anywhere on the canvas, then move the mouse left
and right. The Y location of the click determines the total length of the time window, from 0 at
the top, to 120 seconds at the bottom. The current and total times are displayed in the upperleft corner of the canvas.