sprite-anim is a simple spritesheet animation engine.
npm install sprite-anim --save
- common API (play / pause / stop / gotoAndPlay / gotoAndStop / dispose)
- initialize frames with data (JSONArrayParser), automatically with dimensions (SimpleParser) or your own custom parser
- works with DOM elements (DOMRenderer), canvas element (CanvasRenderer), off-screen canvas (OffScreenCanvasRenderer) or your own custom renderer
IE 6+ with DOM element, IE 9+ with DOM and canvas element. If you need to support IE 8- use es5-shim for EcmaScript 5 methods compatibility.
var SpriteAnim = require('sprite-anim');
require(['sprite-anim.js'], function(SpriteAnim){
});
<script src="path/to/file/sprite-anim.js"></script>
<script>
// global variable SpriteAnim
</script>
var animElt = document.getElementById('anim');
var renderer = new SpriteAnim.DOMRenderer(animElt);
var parser = new SpriteAnim.SimpleParser({width: 1410, height: 3960}, {width: 470, height: 120});
var anim = new SpriteAnim(parser, renderer, {frameRate: 25});
anim.play();
var animElt = document.getElementById('anim');
var renderer = new SpriteAnim.CanvasRenderer(animElt);
var parser = new SpriteAnim.JSONArrayParser(framesData);
var anim = new SpriteAnim(parser, renderer, {frameRate: 25});
anim.play();
Initialize frames directly with spritesheet image dimensions and frame dimensions.
spriteSize:Object{width: Number, height: Number}frameSize:Object{width: Number, height: Number}
Initialize frames with an Array of frames data, following the TexturePacker JSONArray output.
data:ObjectscaleFactor(optional):Number
You can implement your own parser.
A parser must have these properties :
numFrames: number of framesframes: an array of frames{x, y, index, width, height}
var CustomParser = function(framesData){
this.numFrames = 0;
this.frames = [];
// populate frames and increment numFrames
};
Render frame with a DOM element (background-position).
element: DOM element
Render frame with a canvas element (drawImage).
canvas: canvas elementsprite:Imagespritesheet image
You can implement your own renderer.
A renderer must have a render method with a parameter frame.
The frame param is an object with properties {x, y, index, width, height}.
var CustomRenderer = function(){
};
CustomRenderer.prototype.render = function(frame){
// draw the frame
};
new SpriteAnim(parser, renderer, options)
frameRate(Number) Animation frame rate (default:60)loop(Boolean) Iftrueloop animation (default:false)yoyo(Boolean) Iftruerepeat from end when looping (default:false)numFrames(Boolean) Force total framesmanualUpdate(Boolean) Iftruethe animation will not update itself. (default:false) You'll have to update it manually with an explicitonEnterFrame()call on a custom render loop.
Horizontal position from the top left corner of the container. (default: 0)
Vertical position from the top left corner of the container. (default: 0)
Alpha value of the animation. A value between 0 and 1. Currently only supported on canvas mode. (default: 1)
If true loop animation (default: false)
If true repeat from end when looping (default: false)
Animation frame rate
Total frames
Current frame index
true if animation currently playing
true if animation complete
Play animation
Pause animation
Pause and reset animation (frame index = 0)
Go to a frame index and play animation
Go to a frame index and pause animation
Called internally each frame.
If you add the manualUpdate option and call this method directly in a external render loop you have to pass a timeStamp argument (from the requestAnimationFrame callback).
Dispose SpriteAnim instance
Dispatched when animation ended
Dispatched on each frame