Animitter is an animation loop + EventEmitter for browser, node, browserify or amd.


by Kyle Phillips

Animitter is a combination of an EventEmitter and a feature-filled animation loop. It uses requestAnimationFrame with an automatic fallback to setTimeout and offers several additional features, such as framerate throttling, easy starting and stopping of the loop, providing deltaTime in milliseconds between frames, as well as total elapsedTime and frameCount. Listen to the built-in update, start, stop, complete and reset events, or emit your own.

npm install animitter --save

copy ./animitter.js or ./animitter.min.js into your project

<script src="js/animitter.js"></script>

or with require.js/amd:

require(['animitter'], function( animitter ){});
var loop = animitter(function(deltaTimeelapsedTimeframeCount){
    //do something 
var loop = animitter(function(deltaTimeelapsedTimeframeCount){
    //do something 
loop.on('start', function(deltaTimeelapsedTimeframeCount){
    //loop started 
loop.on('update', function(deltaTimeelapsedTimeframeCount){
    if( frameCount === 100 ){
        //`this` is scoped to the Animitter instance 
loop.on('stop', function(deltaTimeelapsedTimeframeCount){
    //this will get triggered on a `complete` also 
loop.on('complete', function(deltaTimeelapsedTimeframeCount){
    //done, can't be started again unless reset 
loop.on('reset', function(deltaTimeelapsedTimeframeCount){

Animitter uses the same EventEmitter as Node.js. This allows you to emit events, add listeners with on,once,addListener or remove listeners with removeListener or removeAllListeners.

The following example periodically emits a custom event. A listener is added for the event which removes itself after a few uses:

var timesDovesHaveFlown = 0,
    shouldMakeDovesFly = function(){ return Math.random() > 0.9; };
var loop = animitter(function(deltaTimeelapsedTimeframeCount){
    //play an animation 
    if( shouldMakeDovesFly() ){
        //after the event-type, pass any parameters you want the listener to receive 
        this.emit('doves-fly', doves);
loop.on('doves-fly', function(doves){
    //make doves fly here 
    if( timesDovesHaveFlown > 4 ){
        this.removeListener('doves-fly', makeDovesFly);

Animitter inherits from EventEmitter which provides methods such as emit, on, removeListener etc… below (in alphabetical order) are the methods that animitter provides directly:

stop the loop and mark it as completed and unable to start again.

stop the loop and remove all listeners.

return the framerate, 0-60.

return the time between the last two frames in milliseconds

return the time between the last frame and the first frame in milliseconds.

return the number of times the loop has repeated.

return true if the loop is currently active

return true if the loop has been marked as completed.

stops the loop and resets its times, frameCount and whether it was completed, leaves listeners intact.

throttle the loop to a preferred framerate between 0-60.

starts repeating the update loop.

stops repeating the update loop.

updates the loop once.

The animitter object comes with the property running this counter indicates the number of animitter instances that are currently animating. This can be helpful for debugging to ensure that you are properly stopping all of your animitter instances.

Animitter comes with TAP tests. These tests are compatible in node and in browser. To run the tests in node:

npm test

To run tests in browser: A very simple way of accomplishing this, is to use something like budo and watch your console for logs:

npm install budo -g
budo test.js

In v1.0.0 a few aspects of the API have changed. Most notably the parameter signature of all events is now:

function onEvent( deltaTimeelapsedTimeframeCount ){ }

Additionally, animitter().isAnimating() has been renamed to isRunning() and { async: true } option no longer is available. Instead users should call animitter().update() directly to update their loop asynchronously.

MIT License