1.0.8 • Published 8 years ago

jquery-spriteanimator v1.0.8

Weekly downloads
2
License
CC-BY-NC-4.0
Repository
bitbucket
Last release
8 years ago

Jquery spriteAnimator plugin

Create fancy sprite animations with this jQuery plugin

Demo's:

Also check out https://galaxy.fili.nl, an animating website fully based on this library. The source for it can be found here

Simple looping animation example

reading spritesheet

Define a html-tag on the page and give it a sprite-sheet as the background-image. Then attach the spriteAnimator plugin on the jQuery wrapper, giving it some information about the sprite-sheet and where to put it on the page. Finally call .play() on it with some optional parameters.

<div id="sprite" style="background-image:url(reading.png)"></div>
<script>
     var spriteAnim = $('#sprite').spriteAnimator({
        top: 200,
        left: 100,
        cols: 3,
        rows: 9
    });
    spriteAnim.play({
        run: -1,
        delay: 100
    });
</script>

.spriteAnimator( Object options )

When attaching the spriteAnimator plugin you can define the following options

PropertyRequiredDefault valueExplanation
debugnofalseShow debug logging in console
urlnonullurl to spriteSheet, if not set the css background-image will be used
colsyesnullNumber of columns in the spritesheet
rowsyesnullNumber of rows in the spritesheet
cutOffFramesno0Number of sprites not used in the spritesheet, for example the last sprite in a sheet might be blank
top bottom left rightnoStarting offset position, will take current positions if not defined
startSpriteno1Sprite number to show when done loading
onLoadednonullCallback function that will be called when loading has finished

The spriteAnimator returns a reference to the plugin on which you can call the methods below.

.addScript( String name, Array script)

Add a named animation sequence. The script parameter should be an array consisting of frame objects. These frame objects can have the following properties:

PropertyRequiredDefault valueExplanation
spriteyesWhich sprite number to show
delaynoglobal delay timeTime in ms to wait after this frame has been rendered
top bottom left rightno0Move the position of the placeholder to any direction after frame has been rendered

Example:

spriteAnim.addScript('paw', [
    { sprite:22, delay:100 },
    { sprite:23, delay:100 },
    { sprite:24, delay:3000 },
    { sprite:23, delay:100 }
]);

.play( Object options )

Plays a named animation sequence or resume if not playing. If no options object is given, it resumes the current animation script or starts playing all frames.

PropertyRequiredDefault valueExplanation
playnotrueStart playing the animation right away
runno1The number of times the animation should run, -1 = infinite
delayno50Default delay for all frames that don't have a delay set
tempono1Timescale for all delays, double-speed = 2, half-speed = .5
reversednofalseDirection of the animation head, true == backwards
outOfViewStopnofalseStop animation if placeholder is no longer in view
scriptnoall framesNew animation array or named script that has been previously been defined, see .addScript()
onPlaynoCallback called when animator starts playing
onStopnonullCallback called when animator stops playing
onFramenonullCallback called when the new frame is rendered

.setTempo( Integer tempo )

Set a new tempo for the animation

.reverse()

Reverse direction of play

.stop()

Stop the animation and reset the playhead

.reset()

Reset playhead to first frame

.nextFrame()

Go forward one frame in script

.previousFrame()

Go back one frame in script

.goToFrame( Integer frameNumber )

Jump to certain frame within current animation sequence

.showSprite( Integer spriteNumber )

Show certain sprite (circumvents the current animation sequence)

.currentFrame()

Get the current frameNumber from script

.currentSprite()

Get the current spriteNumber that is shown

Controlling playback using jQuery events

The built-in jQuery .trigger() method can be used to trigger the following methods:

  • play()
  • reverse()
  • setTempo()
  • stop()
  • reset()
  • nextFrame()
  • previousFrame()
  • goToFrame()
  • showSprite()

This allows for some advanced interaction between multiple sprite animations to take place, for example:

var spriteAnim1 = $('#sprite1').spriteAnimator({
    cols: 3,
    rows: 9
});

var spriteAnim2 = $('#sprite2').spriteAnimator({
    cols: 3,
    rows: 9
});

spriteAnim1.trigger('play', {
    run: 3,
    script: [
        { sprite:1 },
        { sprite:2 },
        { sprite:3, delay:350} ,
        { sprite:4 }
    ],
    onStop: function() {
        spriteAnim2.trigger('play');
    }
});

In this example sprite1 will trigger play() on sprite2 after it's animation has been completed.

License

The artwork in this repository is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.

Meaning you are free to:

  • Share — copy and redistribute the material in any medium or format
  • Adapt — remix, transform, and build upon the material

Under the following terms:

  • Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
  • NonCommercial — You may not use the material for commercial purposes.
1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago