Appends a new div element to object and animates through a sequence of frames in a single image.
Download the production version or the development version.
In your web page:
<script src="jquery.js"></script>
<script src="dist/jquery.spriteAnimation.min.js"></script>
<script>
$(".testButton").spriteAnimation({
src: "filmstrip.png",
frameWidth: 24,
frameHeight: 24,
frameSpacing: 1,
frameRate: 60,
length: 11
})
</script>
$(".testButton").spriteAnimation('play');
Plays the animation sequence forward.
$(".testButton").spriteAnimation('rewind');
Plays the animation sequence backward.
$(".testButton").spriteAnimation('stop');
Stops the animation sequence where it is.
$(".testButton").spriteAnimation('reset');
Stops the animation sequence and frame to 0.
Commands that require parameters should be set as properties on objects. Commands can be issued in the same object as option updates.
$(".testButton").spriteAnimation({gotoFrame:0});
Sets the current frame of the animation to the integer value
Special parameter 'end'
goes to the last frame of the animation. Useful for reversing.
$(".testButton").spriteAnimation({gotoFrameRatio:0});
Takes floats 0-1. Sets the current frame of the animation to the frame at the ratio of the total animation. Useful for tweening.
Image source url. If not provided, the script tries to use the element's background-image
or the <img src>
.
{ src: "filmstrip.png" }
Position of the first frame in sequence. (allows you to put multiple sequences in one image)
// Object with x and y parameters.
{ firstFramePosition: {x:0, y:0} }
Direction to look for subsequent frames.
// String, either 'x' or 'y'.
{ orientation: "x" }
Width of the frame in px. Defaults to the css width of the element.
// Number
{ frameWidth: 25 }
Height of the frame in px. Defaults to the css width of the element.
// Number
{ frameHeight: 25 }
Spacing between frames in px
// Number
{ frameSpacing: 0 }
Frames per second
// Number
{ frameRate: 60 }
Number of frames in the sequence.
// Number
{ length: 25 }
Loop on complete.
// Boolean
{ loop: false }
Callback on complete.
// Function
{ onComplete: null }
Adds a command to execute after the settings have been set
// Takes any String command from the Commands section
{ command: null }
Sequences can be added via the addSequence
or addSequences
options. If no sequences are explicitly defined, a default sequence labeled 'a'
is created transparently using the main options
object.
Sequence objects can contain the following parameters, but default to the same values as above if they are omitted:
{ addSequences: {
'myLabel':{
duration:25,
length:false,
firstFramePosition: {x:0, y:0},
reverse: false,
onComplete: null
}
}
or
{ addSequence: {
label: 'myLabel',
length:25,
loop:false,
firstFramePosition: {x:0, y:0},
reverse: false,
onComplete: null
}
Note the inclusion of a label
in an object passed to addSequence
is required, while the object key is used as the label in addSequences
Used to change the sequence
//String
{ setSequence: 'myLabel' }
Shortcut method that sets a new sequence and immediately starts playing it.
{ gotoAndPlay: 'myLabel' }
Is the same as
{ setSequence: 'myLabel',
command: 'play'
}
jquery.spriteAnimation fires spriteAnimation:complete
events on the hosting jquery object whenever any sequence completes. It also fires a spriteAnimation:complete:[sequence label]
event when labeled sequences complete.
####v0.2.1
- Changed the
duration
property tolength