Animation callbacks
by Peter Higgins

Dojo Animations provide a really simple way to listen to internal events, and fire custom code at various points during (or before) an animation. Its simplicity is what makes it really quite powerful.

All the fx functions return an instance of dojo._Animation, which provides all the tie-ins for your callbacks, and handles the internal timing of the sequence. Having these callbacks are great for complex actions, and even in simple cases. We’ll use dojo.fadeOut as an example.

There are two ways to connect to an animation event. During animation creation:

dojo.fadeOut({ 
    node:"testNode",
    onEnd:function(){
        console.log('animation done');
    }
}).play();

or by using dojo.connect (my preferred method):

var anim = dojo.fadeOut({ node:"testNode" });
var handle = dojo.connect(anim,"onEnd",function(){
    console.log("animation done");
});
anim.play();

This gives us more flexibility, and less potential of leakage. We can disconnect the onEnd function at any calling dojo.disconnect(handle), and connect a different
function on our whim. It’s three steps, compared to the one, but worth the extra effort in potentially long-lived pages with repeating animations.

In the interest of performance, you should cache as often as possible. Creating the animation
once and reusing onEnd (or any of the other available events: beforeBegin, onBegin, onAnimate, onPlay, onStop) is slightly more responsive.

You can see a variation of this technique in the dojo.moj.oe roller.js, which creates all the animations and connections once within a scope, and directly controls it’s own usage (which is slightly more advanced, but a good technique to learn none the less).

Tags: , ,

This entry was posted on Sunday, February 24th, 2008 at 4:20 pm and is filed under Dojo Cookies. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.