tween-base

2.2.2 • Public • Published

tween-base

unstable

This is a base class for tween types, built alongside tweenr and tween-ticker. It is not inherently tied to those modules, and may prove useful outside of those environments.

Usually you won't require tween-base itself, but the modules that build on top of it. Example:

var array = require('tween-array')
var ticker = require('tween-ticker')
 
var start = [25, 15],
    end = [10, 10]
 
ticker.push(array(start, end, { duration: 2 }))
    .on('complete', function(ev) {
        console.log("result:", ev.target)
    })

Usage

NPM

This describes the public (user-facing) API for tweens.

tween = BaseTween(opt)

Where options usually describes the following:

  • delay in time units, default 0
  • duration in time units, default 0
  • ease is an easing function -- tween engines may want to allow strings for user friendliness

methods

cancel()

Cancels a tween. Returns this for chaining. Next time this tween is ticked, it will:

  • emit a "cancelling" event
  • become inactive and stop updating the target
  • emit a "complete" event

A ticker engine might then choose to remove the tween from the queue.

members

tween.on(event, func)

A tween is an event emitter with the following events:

  • start triggered when the tween is first started
  • cancelling triggered before the tween completes, initiating from a call to cancel()
  • complete triggered when the tween is completed
  • update triggered after the tween updates its values

--

inheriting

See test/array.js for an example of a custom array interpolation.

Implementors might subclass like so:

var Base = require('tween-base')
var inherits = require('inherits')
 
function MyTween(target, opt) {
    Base.call(this, opt)
    this.target = target
}
 
inherits(MyTween, Base)
 
//called before 'start' event 
MyTween.prototype.ready = function() {
    //this is where you might store the current
    //state of "target" so that you can interpolate
    //from start to end
}
 
MyTween.prototype.lerp = function(alpha) {
    //interpolate "target" from start to end using alpha
}

License

MIT, see LICENSE.md for details.

Package Sidebar

Install

npm i tween-base

Weekly Downloads

104

Version

2.2.2

License

MIT

Last publish

Collaborators

  • mattdesl