# EaselJS: Animation and Ticker

Synopsis: Create a simple programmatic animation, and learn about the Ticker class.
Topics: animation, Ticker, setFPS, useRAF, getTime, setPaused, requestAnimationFrame, Stage.update, time based animation
Target: EaselJS v0.5.0

## Animation Basics

Animation is simply changing the visual properties of an object over time. There are a number of tweening classes that can make this easy (such as TweenJS and TweenLite), but in this tutorial we will explore the basic concepts without using one.

If you run the following code on a regular interval, the circle will animate moving to the right:

circle.x = circle.x + 3; if (circle.x > stage.canvas.width) { circle.x = 0; }

Simple, but what's the best way to set up that regular interval? You could use your own implementation with setInterval, setTimeout, or requestAnimationFrame, and EaselJS would work perfectly, as long as you remember to call `stage.update()` after updating your display list.

To make things easy, EaselJS comes with the `Ticker` class, which provides a regular heartbeat (tick) for your application, provides pause and time deltas, and wraps both setTimeout and requestAnimationFrame so you can use them interchangeably.

## Ticker

The `Ticker` class provides a simple static interface (meaning, you don't ever create a `new Ticker()`) to propagate a tick to various objects. To use it we just add a listener with `addListener(listener)`. The listener can be a function, or an object with a `tick` function defined.

The code below adds the window as a listener, and defines a tick function that will be called 20 times per second (`Ticker`'s default framerate): createjs.Ticker.addListener(window); function tick() { console.log("TICK!!!"); }

You can easily change the default framerate by either setting an interval (the time between ticks) or a framerate (the number of ticks per frame).

// these are equivalent, 1000ms / 40fps = 25ms createjs.Ticker.setInterval(25); createjs.Ticker.setFPS(40);

Let's combine all of that to make a circle move across the stage at 30 frames per second. And don't forget to call `stage.update()` at the end of each tick to draw the changes to the canvas! Check out the source for simple.html for the complete code.

## Time based animation

For many applications, it's a good idea to make your animations independent of your frame rate. This allows you to change the framerate dynamically, and ensures your animations run for the same amount of time, even if they are running on a slow device that isn't maintaining the target framerate.

`Ticker` makes time based animations easy, by passing your listener a parameter that indicates the amount of time that has elapsed since the previous tick. It also exposes a `getTime` method which provides you with the total time elapsed since `Ticker` initialized.

You can make your animation time based by simply factoring in the elapsed time when we make our property changes. function tick(elapsedTime) { // move 100 pixels per second (elapsedTimeInMS / 1000msPerSecond * pixelsPerSecond): circle.x += elapsedTime/1000*100; // this will log a steadily increasing value: console.log("total time: "+createjs.Ticker.getTime()); }

Now you can change the framerate, and the circle will take the same amount of time to cross the canvas (give or take a few milliseconds).

Notice in the above demo how when you change the FPS, the red circle still moves at the same velocity, whereas the blue circle's velocity is relative to the framerate. Also note how at 20fps, the red circle moves very slightly faster than the blue circle, because it accounts for frames that take slightly longer than the expected 50ms.

## Pausing

`Ticker` also provides the ability to pause all of your animations. By default, any listener you add to Ticker is "pauseable". Calling `Ticker.setPaused(true);` will stop `Ticker` from calling `tick` on all pauseable listeners.

You can also register a listener as not pauseable, but passing `false` as the second parameter, like so: createjs.Ticker.addListener(myListener, false);

These listeners will continue to be ticked even when `Ticker` is paused. They will also be passed a second parameter when called, that indicates whether `Ticker` is currently paused. As an example, you could use this to keep UI animations running, even when a game is paused.

Finally, the `getTime` method accepts a `pauseable` parameter, and will return the appropriate time total based on it.

In the following demo, play with toggling pause, and see how the red "pauseable" circle stops, where the green "unpauseable" circle does not. You can also see how the total pauseable time stops updating when `Ticker` is paused.

## onTick

When you call `stage.update()`, it calls `onTick()` on every display object before drawing to the canvas. Any parameters passed to `update()` will be passed on to `onTick()` handlers. This lets you handle your animation in the context of your display object.

circle.onTick = function() { this.x += 5; }

You can also add your stage directly as a listener to `Ticker`, because `Stage` has a `tick` method that shortcuts to `update`. It's rare that you would want to use this, but it can be handy for quick tests. See the source of onTick.html for an example.

## requestAnimationFrame

Most modern browsers support a new animation related API called `requestAnimationFrame`. It provides the benefit of synching programmatic changes with screen redraws, and will also throttle the framerate of background content (such as an unfocused tab) to reduce CPU and battery use.

If you set `Ticker.useRAF = true`, then `Ticker` will use requestAnimationFrame if it is supported in the current browser, or fall back to setTimeout if not. If you do this, it is recommended that you set your framerate to a divisor of 60 (ex. 15, 20, 30, 60) in order to get the most consistent results.

## Performance

Remember that a higher framerate doesn't always result in smoother animation. A lower framerate uses less CPU, and can provide more consistent performance. Try to find the right balance for your project.

If you'd like to check what your real framerate is, you can call `Ticker.getMeasuredFPS()` to get the average real framerate for the past 1 second.

## TweenJS

If you're planning to do a lot of animation, you might want to check out TweenJS or another tweening library. You can use simple commands to tween properties over time and create sequences of animations.

createjs.TweenJS.get(circle).to({x:300}, 1000).to({x:0}, 0).call(onAnimationComplete);