pixi-tween is a plugin for Pix
pixi-tween is a plugin for Pixi.js v3.0.8 or higher to create tween animations.
Online examples: Easing, TweenPath
Installationnpm install pixi-tween
Usage
Browserify - Webpack
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(); Prebuilt files
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(); How it works
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.
Using AnimationLoopvar 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(); Events
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. PathsMove 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...
ExamplesSee the Examples folder.
API TweenManager constructor()The constructor
.tweensAn array with the tweens created
.update( delta )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).
.removeTween( tween )Remove a tween from the .tweens array in the next frame.
.addTween( tween )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.
.createTween( target )Return a new instance of PIXI.Tween managed by this tweenManager.
.getTweensForTarget( target )Return an array with all the tweens for the given target.
TweenPath constructor()The constructor
.moveTo( x, y )See PIXI.Graphics#moveTo
.lineTo( x, y )See PIXI.Graphics#lineTo
.bezierCurveTo( cpX, cpY, cpX2, cpY2, toX, toY )See PIXI.Graphics#bezierCurveTo
.quadraticCurveTo( cpX, cpY, toX, toY )See PIXI.Graphics#quadraticCurveTo
.arcTo( x1, y1, x2, y2, radius )See PIXI.Graphics#arcTo
.arc( cx, cy, radius, startAngle, endAngle, anticlockwise )See PIXI.Graphics#arc
.drawShape( shape )See PIXI.Graphics#drawShape
.clear()Clear the path.
.closedSet true to close your path.
.lengthGet the points number
Tween constructor( target [,manager] )The constructor
.targetThe object to animate
.managerThe TweenManager who manage this tween
.timeThe animation time
.activeRead only, true if a tween is running
.easingA easing function from PIXI.tween.Easing or a custom easing.
.expireSet true if you want to delete this instance when the animation it's done.
.repeatTimes to repeat this tween.
.loopRepeat this tween forever.
.delaySet a delay time in milliseconds before the timer's count.
.pingPongSet true to repeat back this tween.
.isStartedRead only.
.isEndedRead only.
.pathSet an instance of TweenPath to animate an object along the path.
.pathReverseReverse the path.
.addTo( manager )Add this tween to a manager
.chain( tween )When this tween it's finished, fire the chained tween.
.start()Start the tween
.stop()Stop the tween
.to( object )Set the params to animate. Example: {x:100, y:100}
.from( object )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()Remove this tween from the manager.
.clear()Clear this tween.
.reset()Reset this tween to the initial state, keeping the data like .time, .delay, etc...
update( delta [,deltaMS] )The update method, you don't need to use this method, the manager will do this internally.
EasingSee Easing
版权声明:
1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。2、网站不提供资料下载,如需下载请到原作者页面进行下载。