LittleJS 1.28.0

TweenSystem

Tween

A tween: drives a callback with a value interpolated between start and end over duration seconds. Pauses with the game by default.

  • In TypeScript it is a Tween<T> of the type it tweens, which comes from start and end or the callback's parameter, so (v: number)=> ... takes a number

Constructor

new Tween(callback, start, end, duration, options)tweenSystem.js:61

Create a new tween. The callback fires immediately with start so the target snaps to the start value on the same frame the tween is created.

start and end may be numbers, Vector2, Vector3 or Color instances, or any object exposing a lerp(other, percent) => sameType method. The callback receives the interpolated value (a number, or a fresh instance for lerp-able types). Both endpoints must be the same type.

callback function

Called with the interpolated value each frame

start T optional, default 0

Starting value

end T optional, default 1

Ending value

duration number optional, default 1

Duration in seconds

options Object optional
ease function optional

Easing function (defaults to LINEAR)

useRealTime boolean optional, default false

Advance even when the game is paused (matches Timer's useRealTime)

paused boolean optional, default false

Start in paused state

// Animate a fade-out over 2 seconds with an ease-out sine curve.
new Tween((v) => obj.alpha = v, 1, 0, 2, { ease: Ease.OUT(Ease.SINE) });

Members

Methods

Members

callback : functiontweenSystem.js:97

Called with the interpolated value each frame

duration : numbertweenSystem.js:105

Total duration in seconds

ease : functiontweenSystem.js:109

Easing curve mapping [0,1] -> [0,1]

Ending value

life : numbertweenSystem.js:107

Remaining time in seconds (counts down from duration to 0)

onComplete : undefined|functiontweenSystem.js:117

Called once the tween completes: when its last pass ends, the last iteration of a loop or pingPong, and again each time a restart plays through; then() sets it

paused : booleantweenSystem.js:113

If true, stop advancing until cleared

Starting value

target : Object|undefinedtweenSystem.js:142

The object tweenProperty animates, the tween stops once it is destroyed, even while paused

useRealTime : booleantweenSystem.js:111

If true, advance even when the game is paused

Methods

getPercent() → numbertweenSystem.js:239

Get how far this tween has progressed, from 0 (just started) to 1 (completed). Clamped — overshoot past completion still reads 1.

getValue() → TtweenSystem.js:248

Get the current interpolated value (the value most recently passed to the callback). Returns a number, Vector2, Vector3 or Color depending on the tween's start/end types.

interp(life) → TtweenSystem.js:260

Compute the interpolated value at the given remaining life. At life === duration the result is start; at life === 0 it is end.

  • At life 0 it is the end value exactly
  • A vector goes past its ends as far as the easing does, as a number does; a Color stays between them, so its channels stay in range, and any other type goes as far as its own lerp takes it
life number
isActive() → booleantweenSystem.js:231

True if this tween is in the active list and not paused

loop(count) → Tween<T>tweenSystem.js:183

Repeat this tween n total times. After each iteration finishes, the same tween starts over, so the handle returned stays good for the whole loop: pause or stop it to pause or stop every iteration left. loop() with no argument loops forever.

Mutually exclusive with pingPong; calling either replaces the other. A then callback, set before or after, is called when the last iteration ends.

count number optional, default Infinity

Pause this tween. While paused, tweenUpdate skips it

pingPong(count) → Tween<T>tweenSystem.js:198

Like loop, but swap start and end between iterations so the value bounces back and forth. pingPong() with no argument bounces forever.

Mutually exclusive with loop; calling either replaces the other. A then callback, set before or after, is called when the last iteration ends.

count number optional, default Infinity

Reset this tween to the start: life back to duration, pause cleared, re-added to the active list if previously stopped, and the callback re-fired with the start value. It replays one pass: a loop or pingPong that has finished is not started over, a pingPong that ended on its way back plays that way again, and a restart mid loop keeps the iterations left. Call loop or pingPong again after restart to repeat it. The then callback is kept and is called again when it completes.

Resume a paused tween

setEase(easeFn) → Tween<T>tweenSystem.js:152

Set the easing curve and return this for chaining

easeFn function

Remove this tween from the active list, ending a loop or pingPong too, without calling the then-callback. It keeps the then-callback, so a restart calls it when it completes.

then(callback) → Tween<T>tweenSystem.js:167

Set the completion callback, onComplete, and return this for chaining. It is called once the tween completes: when its pass ends, or for a loop or pingPong when its last iteration ends, so an endless one never calls it. Calling then again replaces the previous callback.

  • It works with loop and pingPong in either order, neither replaces the other
  • It is kept by restart, so a restarted tween calls it again when it completes
  • stop and tweenStopAll end a tween without calling it
callback function