Little TexturePacker animation engine. It allows you to play in a canvas your TexturePacker JSON export (array typed).
npm install -S sprite-reader
var SpriteReader = require('sprite-reader');
var reader;
var img = new Image();
img.onload = function() {
reader = new SpriteReader(img, require('sprite-data.json'));
anim();
};
img.src = 'sprite.jpg';
function anim() {
requestAnimationFrame(anim);
reader.update();
}In order to work with multipack (and only in that case), TexturePacker configuration must follow some configuration rules :
Algorithm: BasicSort by: NameSort order: AscendingDetect identical spriteshas to be unchecked
new SpriteReader(image, json[, options]);The loaded images which contain the sprites. Single Image can be passed without array.
JSON datas associated to image order. Length of the array must be equals to image length.
Start playing immediately when instanciated
The targeting canvas element. If not provided, one will be created and must be inserted, accessible through canvas property.
Fill background color
FPS of the animation
Starting sprite
Loop when it reaches to value
Called when all sprites have been cached
Called when the animation complete. Does not work if repeat is set.
Called at each repeat
Called when the animation has played all repeat
How many time the animation should repeat
If true, the created canvas (if no one was provided) would be styled with width and height divided by 2
If true, animation will be played in reverse side
Frame to stop the animation
Update the current sprite according to fps. This method has to ba called by your own in a raf.
- force
Booleanfalse: Ifforceis set totrue, FPS will be ignore and current frame will be updated
Start playing from last known position
Pause at current position
Stop playing and set current position to from value
Switch playing side, optionaly forcing it
- side
Booleannull: Switch playing side. If side is set totrue, normal side will be forced;falsewill force reverse.
from or to can be omitted.
- from
Number: Set starting frame and go to it - to
Number: Set ending frame
- frame
Number: Move cursor toframeand stop playing
Clear the canvas and stop it
Return the targeting canvas. If no canvas was given to the constructor, you will have to add this to your page.
FPS of the animation
Loop when it reaches to value
How many time the animation should repeat
Called when the animation complete. Does not work if repeat is set.
Called at each repeat
Called when the animation has played all repeat
Should works in IE9+ browsers (canvas compatibility).
For IE9 support you will have to install :
perfnowpolyfill withnpm install -S perfnow
First you should install EditorConfig package in your code editor. Then,
cd .git/hooks
ln -s ../../hooks/pre-commit
npm install
Then 2 commands are available :
npm run watchwatch and build js filesnpm run buildclean, build and uglify js files