pixi-tween is a plugin for Pixi.js v3.0.8 or higher to create tween animations.
Online examples: Easing, TweenPath
npm install pixi-tween
If you use Browserify or Webpack you can use pixi-tween like this:
var PIXI = require('pixi.js');
var tweenManager = require('pixi-tween'); //pixi-tween is added automatically to the PIXI namespace
//create PIXI renderer
var renderer = new PIXI.autoDetectRenderer(800,600);
document.body.appendChild(renderer.view);
var stage = new PIXI.Container();
function animate(){
window.requestAnimationFrame(animate);
renderer.render(stage);
PIXI.tweenManager.update();
}
animate();
var renderer = new PIXI.autoDetectRenderer(800,600);
document.body.appendChild(renderer.view);
var stage = new PIXI.Container();
function animate(){
window.requestAnimationFrame(animate);
renderer.render(stage);
PIXI.tweenManager.update();
}
animate();
This plugin add a new namespace names tween
to the PIXI namespace, and this new namespace has 3 new classes Tween, TweenPath and TweenManager, also add an Easing object. And create an instance for TweenManager in PIXI.tweenManager, but all you need is add PIXI.tweenManager.update() in your requestAnimationFrame. You can pass as params for PIXI.tweenManager.update(delta)
your own delta time, if you don't pass anything it will be calculated internally, for max accuracy calculating the delta time you can use the AnimationLoop plugin.
When a tween is ended, the instance will kept in the memory and in the tweenManager, but you can prevent this if you set .expire = true in the tween.
var renderer = new PIXI.autoDetectRenderer(800,600);
document.body.appendChild(renderer.view);
var animationLoop = new PIXI.AnimationLoop(renderer);
//Add a postrender or prerender event to add the tweenManager.update in the raf.
animationLoop.on('postrender', function(){
PIXI.tweenManager.update(this.delta); //Pass as param the delta time to PIXI.tweenManager.update
});
animationLoop.start();
Tween extends from PIXI.utils.EventEmitter, and emit some events: start, end, repeat, update, stop and pingpong. More info: Node.js Events
- start - callback(): Fired when the tween starts. If the tween has a delay, this event fires when the delay time is ended.
- end - callback(): Fired when the tween is over. If the .loop option it's true, this event never will be fired, and if the tween has an .repeat number, this event will be fired just when all the repeats are done.
- repeat - callback(repeat): Fired at every repeat cycle, if your tween has .repeat=5 this events will be fired 5 times.
- update - callback(elapsedTime): Fired at each frame.
- stop - callback(): Fired only when it's used the .stop() method. It's useful to know when a timer is cancelled.
- pingpong - callback(): If the pingPong option it's true, this events will be fired when the tweens returns back.
Move an object along a path it's easy with TweenPath. TweenPath use a similar PIXI.Graphics API to create paths, and once it's created our path we just need to add it to a tween with .path = ourPathCreated.
If you need draw your path (useful to debug), PIXI.Graphics has been enhanced with a new method named .drawPath(path). Use it the same way like .drawRectangle, .drawShape, etc...
See the Examples folder.
The constructor
An array with the tweens created
The update method, make sure it is in the raf. You can pass a fixed delta time (like 0.016), your own calculated delta, or nothing. (Delta time in seconds not milliseconds).
Remove a tween from the .tweens array in the next frame.
Normally you want to use .createTween(target) to create a tween, but, you can also create a tween with new PIXI.Tween(target) and add it in the manager with this method.
Return a new instance of PIXI.Tween managed by this tweenManager.
Return an array with all the tweens for the given target.
The constructor
See PIXI.Graphics#bezierCurveTo
See PIXI.Graphics#quadraticCurveTo
Clear the path.
Set true to close your path.
Get the points number
The constructor
The object to animate
The TweenManager who manage this tween
The animation time
Read only, true if a tween is running
A easing function from PIXI.tween.Easing or a custom easing.
Set true if you want to delete this instance when the animation it's done.
Times to repeat this tween.
Repeat this tween forever.
Set a delay time in milliseconds before the timer's count.
Set true to repeat back this tween.
Read only.
Read only.
Set an instance of TweenPath to animate an object along the path.
Reverse the path.
Add this tween to a manager
When this tween it's finished, fire the chained tween.
Start the tween
Stop the tween
Set the params to animate. Example: {x:100, y:100}
Set the params to start the animation. If you don't call .from the the values will be the actual values of the .to object.
Remove this tween from the manager.
Clear this tween.
Reset this tween to the initial state, keeping the data like .time, .delay, etc...
The update method, you don't need to use this method, the manager will do this internally.
See Easing