SamplePlugin Class
A sample TweenJS plugin. This plugin is purely for demonstration, and contains documentation and helpful tips on building plugins.
It sets the y position of the target based on a sinusoidal function of its x position.
NOTE: The code for this class is heavily commented. Please look at it if you'd like to write a plugin.
A TweenJS plugin is simply an object that exposes two properties (id, priority), and three methods (init, step, and change).
Generally a plugin will also expose an install
method as well, though this is not strictly necessary.
Constructor
SamplePlugin
()
Item Index
Methods
change
-
tween
-
step
-
prop
-
value
-
ratio
-
end
Called before a property is updated by the tween.
Parameters:
-
tween
TweenThe related tween instance.
-
step
TweenStepThe related tween step. This class is currently undocumented. See the bottom of Tween.js for info.
-
prop
StringThe name of the property being tweened.
-
value
AnyThe current tweened value of the property, as calculated by TweenJS. Previous plugins may have modified this value.
-
ratio
NumberA value indicating the eased progress through the current step. This number is generally between 0 and 1, though some eases will generate values outside this range.
-
end
BooleanIndicates that the tween has reached the end and is about to deregister itself.
Returns:
Return the value that should be assigned to the target property.
init
-
tween
-
prop
-
value
Called by TweenJS when a new property initializes on a tween. Generally, the call
to Plugin.init
will be immediately followed by a call to Plugin.step
.
For example:
foo.x = 0;
foo.y = 100;
Tween.get(foo)
.to({x:10}) // init called with prop = "x", value = 0
.to({x:20}) // init is NOT called, since x was already inited
.to({y:200}) // init called with prop = "y", value = 100
Parameters:
Returns:
The modified starting tween value for the property. In most cases, you would simply wouldn't return anything,
but some plugins may need to modify the starting value. You can also return Tween.IGNORE
to prevent this prop
from being added to the tween at all.
install
()
static
Installs this plugin for use with TweenJS. Call this once after TweenJS is loaded to enable this plugin.
step
-
tween
-
step
-
props
Called when a new step is added to a tween (ie. a new "to" action is added to a tween).
For example: Tween.get(foo) .to({x:10}) // step called .wait(100) // step is NOT called .to({x:20, y:200}) // step called
Properties
ID
String
static
readonly
READ-ONLY. A unique identifying string for this plugin. Used by TweenJS to ensure duplicate plugins are not installed on a tween.
priority
Number
static
Used by TweenJS to determine when to call this plugin relative to others. Plugins with higher priority read first, and write last.
For example, if plugin A has priority=0
, and plugin B has priority=9
then B's init
and step
methods would
be called before A's, but B's change
method would be called after A's.
Default: 0