Animating layers
Animating layers
jCanvas provides an animateLayer()
method for animating the properties of a jCanvas layer.
The animateLayer()
method accepts up to five arguments:
- The
index
orname
of the layer to be animated. The layer object itself is also an acceptable value. - An object containing the properties to animate and their end values
- The duration of the animation in milliseconds (optional; defaults to
400
) - The easing of the animation (optional; defaults to
'swing'
) - A callback function that runs when the animation completes (optional); it accepts the layer object as its only argument
jCanvas can animate numeric values, as well as colors (hex, RGB, or color names). jCanvas also enables jQuery to utilize this color animation for HTML elements.
Additionally, you may use the string '+="
or "-="
to animate a property from the current value.
Functions as Property Values
You can also use a callback function . Whatever the function returns will be used as the end-value to which that property will be animated.
As always, the value of this
in your callback function is the canvas DOM element.
This capability is especially useful when using the below animateLayerGroup()
method, in which you may not have direct access to each layer in the group. Now, such direct access to these layers is possible.
Animating layer groups
You can also animate all layers in a layer group using the animateLayerGroup()
method.
The method accepts the same basic arguments as the animateLayer()
method,
Stopping animation
Similar to jQuery’s stop()
method, you can stop any layer animation in progress by calling the stopLayer()
method.
Additionally, you may (optionally) pass in true
as a second argument, which will also remove any queued animations.
You can also stop animation for all layers in a group using the stopLayerGroup()
method
Delaying animation
Similar to jQuery’s delay()
method, you can delay any layer’s animation queue by calling the delayLayer()
method.
The method accepts two arguments: the layer name/index, and the number of milliseconds to delay animation.
You can also delay animation for all layers in a group using the delayLayerGroup()
method
Running a function at every step
Just like jQuery, the animateLayer()
method in jCanvas supports an alternate syntax for providing additional options (like a step
callback).
Notes
Multiple animateLayer()
calls can be queued up rather than using multiple callback functions.
The callback parameter for the animateLayerGroup()
method will run when each layer in the group finishes animating.