Version 1, changed by guest 01/22/2006. Show version history
Dojo provides a powerful, event-based animation library for developers. I'll try and cover the basics here for anyone that wants to get started with making animations.
To create an animation object, you make the following call:
var anim = new dojo.animation.Animation(curve, duration, accel, repeatCount);
Note: All these map directly to properties of the animation object. So, for the example above, you can change the duration by doing the following:
anim.duration = 1000; // change duration to be 1 second
It isn't advised to change these values during an animation as the behavior for such an action isn't defined.
All animations are time-driven. What that means is that you specify the duration of an animation and it runs in that time frame . The benefit of this system is that animations will look the same regardless of CPU speed.
All animations run off of curves. A curve is a special object whose only requirement is that is has a getValue(n) method, where n is a number between 0 and 1, that returns an array of numbers. Dojo has some pre-built curves available in dojo.math.curves. See Curves for details. As an example, the code for a Line curve is as follows:
function Line(start, end) {
this.start = start;
this.end = end;
this.dimensions = start.length;//simple function to find point on an n-dimensional, straight line
this.getValue = function(n) {
var retVal = new Array(this.dimensions);
for(var i=0;i<this.dimensions;i++)
retVal[i] = ((this.end[i] - this.start[i]) * n) + this.start[i];
return retVal;
}return this;
}
Note, however, that by "line" we do not mean a physical line that you'd draw on a screen, but simply that we are moving from a starting point to an ending point linearly. Lines can be n-dimensional and are not limited to moving a node on the screen (as you'll see in the examples below).
The Dojo animation library allows developers to hook into all parts of an animation which enables them to be able to make advanced animations in a simple and intuitive manner.
The following events are provided:
Whenever an event handler is called, it is passed a single event argument. The argument has the following properties:
Methods
Properties
The following methods are available for animations:
API references are fine n' dandy, but everyone loves sample code. Here's some sample animations, most taken from dojo.graphics.htmlEffects...
Available as the pre-built animation, dojo.graphics.htmlEffects.slide with slideTo and slideBy variants.
var node = document.getElementById('animNode'); // with a suitable 'position'
var anim = new dojo.animation.Animation(
new dojo.math.curves.Line([0,0], [400,200]), // linear points from (0,0) to (400,200)
- , // do it over a 3 second duration
- // with no acceleration
);
dojo.event.connect(anim, "onAnimate", function(e) {
node.style.left = e.x + "px";
node.style.top = e.y + "px";
});
dojo.event.connect(anim, "onBegin", function(e) {
var div = document.createElement("div");
div.appendChild(document.createTextNode("Animation started!"));
document.body.appendChild(div);
});
dojo.event.connect(anim, "onEnd", function(e) {
var div = document.createElement("div");
div.appendChild(document.createTextNode("Animation done!"));
document.body.appendChild(div);
});
anim.play();
The ever-popular yellow fade animation. Simplified version of the pre-built animation, dojo.graphics.htmlEffects.highlight, colorFadeIn, and colorFadeFrom (not sure what the best name is). The pre-built version will get the background color of a node before hand and fade from your specified color to the background color. It is probably more ideal than the example below.
var anim = new dojo.animation.Animation(
// linear points from (255, 255, 156) to (255, 255, 255)
new dojo.math.curves.Line([255, 255, 156], [255, 255, 255]),
- , // do it over a 1 second duration
- // with no acceleration
);
dojo.event.connect(anim, "onAnimate", function(e) {
// use the "coordinates" as RGB values. Since it is an array,
// we can just join it with commas to get a nice rgb(r,g,b) CSS bg color
document.getElementById("elmToHighlight").style.backgroundColor =
"rgb(" + e.coordsAsInts().join(",") + ")";
});
dojo.event.connect(anim, "onEnd", function(e) {
// "Kill" the background color instead of leaving it white
// "transparent" would probably work, too.
document.getElementById("elmToHighlight").style.backgroundColor = "inherit";
});
anim.play();
For more examples, see htmlEffects.js.
See Also: AnimationSequence?, Curves, HTMLEffects