sprite-reader
Little TexturePacker animation engine. It allows you to play in a canvas your TexturePacker JSON export (array typed).
Installation
npm install -S sprite-reader
How to use
var SpriteReader = ; var reader; var img = ;img { reader = img ; ;};imgsrc = 'sprite.jpg'; { ; reader;}
With multipack
In order to work with multipack (and only in that case), TexturePacker configuration must follow some configuration rules :
Algorithm: Basic
Sort by: Name
Sort order: Ascending
Detect identical sprites
has to be unchecked
Documentation
Constructor
image json options;
Array[Image]
image The loaded images which contain the sprites. Single Image
can be passed without array.
Array[Object]
json JSON datas associated to image
order. Length of the array must be equals to image
length.
Options
Boolean
true
autoplay Start playing immediately when instanciated
Canvas
null
canvas The targeting canvas element. If not provided, one will be created and must be inserted, accessible through canvas
property.
Hex
null
fillColor Fill background color
Number
30
fps FPS of the animation
Number
0
from Starting sprite
Boolean
false
loop Loop when it reaches to
value
Function
null
onReady Called when all sprites have been cached
Function
null
onComplete Called when the animation complete. Does not work if repeat is set.
Function
null
onRepeat Called at each repeat
Function
null
onRepeatComplete Called when the animation has played all repeat
Number
0
repeat How many time the animation should repeat
Boolean
false
retina If true, the created canvas (if no one was provided) would be styled with width
and height
divided by 2
Boolean
false
reverse If true
, animation will be played in reverse side
Number
null
to Frame to stop the animation
Methods
update([force])
Update the current sprite according to fps
. This method has to ba called by your own in a raf.
- force
Boolean
false
: Ifforce
is set totrue
, FPS will be ignore and current frame will be updated
play()
Start playing from last known position
pause()
Pause at current position
stop()
Stop playing and set current position to from
value
reverse([side])
Switch playing side, optionaly forcing it
- side
Boolean
null
: Switch playing side. If side is set totrue
, normal side will be forced;false
will force reverse.
goFromTo([from, to])
from
or to
can be omitted.
- from
Number
: Set starting frame and go to it - to
Number
: Set ending frame
goToAndStop(frame)
- frame
Number
: Move cursor toframe
and stop playing
destroy()
Clear the canvas and stop it
Properties
Getter
Canvas
canvas Return the targeting canvas. If no canvas
was given to the constructor, you will have to add this to your page.
Setters
Number
fps FPS of the animation
Boolean
loop Loop when it reaches to
value
Number
repeat How many time the animation should repeat
Function
onComplete Called when the animation complete. Does not work if repeat is set.
Function
onRepeat Called at each repeat
Function
onRepeatComplete Called when the animation has played all repeat
Browser compatibility
Should works in IE9+ browsers (canvas compatibility).
For IE9 support you will have to install :
perfnow
polyfill withnpm install -S perfnow
Contribute
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 watch
watch and build js filesnpm run build
clean, build and uglify js files