- Overview
- Documents
Motio is a small JavaScript library for simple but powerful sprite based animations and panning. It takes an element, and changes the background position to create an animation effect.
Dependencies
Motio has no dependencies, but there is an optional Motio jQuery plugin version available.
Compatibility
Works everywhere.
Source: darsa.in
1. INCLUDE JS FILES
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script src="js/motio.min.js"></script>
2. HTML
You have an element with background that holds either seamless repeating image (for panning), or animation frames sprite (sprite based animation).
<div id="panning" class="panning"></div> <div id="sprite" class="sprite"></div>
3. CSS
The size and background of the elements is handled by your CSS.
.panning { width: auto; // Span to the full width of container height: 300px; background: url('repeating_sky.jpg'); } .sprite { width: 256px; // Width of one animation frame height: 256px; // Height of one animation frame background: url('animation_frames_sprite.png'); }
4. JAVASCRIPT
Motio calls than look like this:
// Panning var panning = new Motio(document.getElementById('panning'), { fps: 30, // Frames per second. More fps = higher CPU load. speedX: -30 // Negative horizontal speed = panning to left. }); panning.play(); // Start playing animation // Sprite var sprite = new Motio(document.getElementById('sprite'), { fps: 10, // Frames per second. More fps = higher CPU load. frames: 14 // Number of animation frames in sprite }); sprite.play(); // Start playing animation
Motio knows that animation is sprite based when you set the frames option.
Initiating animation with .play() method is not needed when calling via a jQuery proxy, as in this case the animation is initiated automatically after new Motio object has been created.
5. OPTIONS
var panning = new Motio(element, { fps: 15, // Frames per second. // Sprite animation specific options frames: 0, // Number of frames in sprite. vertical: 0, // Tells Motio that you are using vertically stacked sprite image. width: 0, // Set the frame width manually (optional). height: 0, // Set the frame height manually (optional). // Panning specific options speedX: 0, // Horizontal panning speed in pixels per second. speedY: 0, // Vertical panning speed in pixels per second. bgWidth: 0, // Width of the background image (optional). bgHeight: 0 // Height of the background image (optional). });
6. PROPERTIES
motio.element
Type: Object
The element Motio is called on.
Type: Object
Object with all options used by the current Motio object. This is essentially a Motio.defaultsobject extended by options passed to new Motio().
Type: Integer
Width of the frame. Doesn't update, so if you have a panning animation on body element and you resize the window, it will not reflect in this value. You don't really ever need this property in panning mode anyway.
Type: Integer
Height of the frame. Doesn't update, so if you have a panning animation on body element and you resize the window, it will not reflect in this value. You don't really ever need this property in panning mode anyway.
Type: Boolean
This property is true when Motio is paused, false otherwise.
Panning mode specific properties
Type: Object
Background position object:
{ x: 100, // Horizontal background position. y: 100 // Vertical background position. }
Sprite mode specific properties
Type: Integer
Active frame index.
Type: Integer
Number of frames total.
7 METHODS
play
motio.play( [ reverse ] );
Starts playing continuous animation that doesn't stop until interrupted by other methods.
- reverse: Boolean Pass true to animate in the opposite direction.
The reverse argument is relevant only in sprite animation mode, as panning animation direction is controlled by passing a positive or negative integers into the speedX & speedY options.
motio.pause();
Pauses the current animation.
motio.toggle();
Pauses when playing, resumes when paused.
Available only in sprite animation mode!
motio.toStart( [ immediate ] [, callback] );
Animate to the first frame, and pause the animation. If the first frame is already active, it will repeat the animation from the last frame.
- immediate Boolean Whether the last frame should be activated immediately, skipping the animation.
- callback Function Callback to be fired when animation reaches the destination.
If you interrupt the animation before it reaches the destination, callback won't fire.
motio.toStart(true); // Activate first frame immediately without animation. motio.toStart(callback); // Execute callback when animation reaches the destination. motio.toStart(true, callback); // Combination of both arguments.
Available only in sprite animation mode!
motio.toEnd( [ immediate ] [, callback] );
Animates to the last frame, and pause the animation. If the last frame is already active, it will repeat the animation from the first frame.
- immediate Boolean Whether the last frame should be activated immediately, skipping the animation.
- callback Function Callback to be fired when animation reaches the last frame.
If you interrupt the animation before it reaches the destination, callback won't fire.
motio.toEnd(true); // Activate last frame immediately without animation. motio.toEnd(callback); // Execute callback when animation reaches the destination. motio.toEnd(true, callback); // Combination of both arguments.
Available only in sprite animation mode!
motio.to( frame, [ immediate ] [, callback] );
Animate to the specified frame, and pause the animation.
- frame Integer Animation destination frame index, starting at 0.
- immediate Boolean Whether the last frame should be activated immediately, skipping the animation.
- callback Function Callback to be fired when animation reaches the destination.
If you interrupt the animation before it reaches the destination, callback won't fire.
If the current active frame is specified as the destination, Motio will be paused, and callback will be fired immediately.
motio.to(2, true); // Activate 3rd frame immediately without animation. motio.to(2, callback); // Execute callback when animation reaches the 3rd frame. motio.to(2, true, callback); // Combination of both arguments.
motio.set( name, value );
Change a specified option value.
- name String Name of the option to be changed.
- value Mixed New option value.
Only these options can be changed dynamically:
- fps
- speedX
- speedY
Example:
motio.set('speedX', 100);
motio.on( eventName, callback );
Registers a callback to one or more of the Motio events. All available events and arguments they receive can be found in the Events documentation.
- eventName: Mixed Name of the event, or callback map object.
- callback: Mixed Callback function, or an array with callback functions.
Examples:
// Basic usage motio.on('frame', function () {}); // Multiple events, one callback motio.on('play pause', function () {}); // Multiple callbacks for multiple events motio.on('play pause', [ function () {}, function () {} ]); // Callback map object motio.on({ play: function () {}, frame: [ function () {}, function () {} ] });
motio.off( eventName [, callback ] );
Removes one, multiple, or all callbacks from one of the Motio events.
- eventName: String Name of the event.
- callback: Mixed Callback function, or an array with callback functions to be removed. Omit to remove all callbacks.
Examples:
// Removes one callback from load event motio.off('load', fn1); // Removes one callback from multiple events motio.off('load move', fn1); // Removes multiple callbacks from multiple event motio.off('load move', [ fn1, fn2 ]); // Removes all callbacks from load event motio.off('load');
motio.destroy();
Pauses the animation, and resets the background position to 0 0.
8. EVENTS
pause
Triggered when animation is paused.
Triggered when animation is resumed.
Triggered on each animation frame.