Tween

View source

Stores the shared value and timing state used by a tween component.

Tween is the reusable base for concrete tween components such as Position2DTween, Position3DTween, Orientation2DTween, Orientation3DTween, Scale2DTween, and Scale3DTween. A matching TweenPlugin uses the stored value and timing state to interpolate a concrete component value over time.

Add TweenRepeat or TweenFlip alongside a concrete tween to opt into repeated cycles or endpoint swapping.

Examples

import { Query, World } from '@wimaengine/ecs'
import { Position2D } from '@wimaengine/transform'
import { Position2DTween, TweenFlip } from '@wimaengine/tween'

const world = new World()
world.spawn([
  new Position2D(0, 0),
  new Position2DTween(
    new Position2D(100, 50), // to
    new Position2D(0, 0), // from
    2
  ),
  new TweenFlip()
])

new Query(world, [Position2D, Position2DTween]).each(([position, tween]) => {
  console.log(position, tween.timeTaken)
})
// Register the tween plugin on the app that runs this world to advance
// and interpolate the tween.

Constructor


constructor<T>(to: T, from: T, duration: number, repeat?: boolean, flip?: boolean, easing?: EasingFn)

View source

Creates tween state for an ending value, starting value, and duration.

Values are assigned directly; the constructor does not clone to or from, or validate duration. The optional booleans are stored on the instance; use TweenRepeat and TweenFlip to opt into repeated cycles and endpoint swapping.

Type Parameters

T

Parameters

NameDescriptionDefault
to

Value passed to interpolation at progress 1.

—
from

Value passed to interpolation at progress 0.

—
duration

Total tween duration in seconds.

—
repeat?

Stored repeat option.

false
flip?

Stored flip option.

true
easing?

Function applied to normalized progress.

Easing.linear

Properties


duration: number

View source

Total duration of the tween in seconds.

timeTaken represents elapsed time toward this value, and timeTaken / duration provides the normalized progress used to interpolate between the endpoints. The constructor stores the supplied number directly without validation.

Default: 0

finish: boolean

View source

Indicates whether the tween has completed.

The value starts as false and is stored as a boolean. Application code can set it when the tween reaches its desired state. Tween.copy copies the value directly.

Default: false

to: T

View source

Value at the end of the interpolation.

The constructor stores the supplied value by reference. Interpolation uses this value at progress 1. Tween.copy reuses primitive values and calls the value type’s static copy method for object values.

from: T

View source

Value at the start of the interpolation.

The constructor stores the supplied value by reference. Interpolation uses this value at progress 0. Tween.copy reuses primitive values and calls the value type’s static copy method for object values.

easing: EasingFn

View source

Easing function applied to normalized progress before interpolation.

The default is Easing.linear. The function receives timeTaken / duration, and its return value becomes the interpolation progress without additional clamping. The function is stored and copied by reference.

timeTaken: number

View source

Elapsed tween time in seconds.

It starts at 0. Together with duration, it forms timeTaken / duration, which is passed to easing before interpolation between from and to. Tween.copy copies the number directly.

Default: 0

Methods


staticcopy<U>(source: Tween<U>, target?: Tween<U>): Tween<U>

View source

Copies tween state into a target component.

The scalar fields (duration, finish, timeTaken, repeat, and flip) are assigned directly. easing is copied by reference. to and from are copied independently: non-object values are reused, while each object value must have a constructor with a callable static copy method, which is called as value.constructor.copy(value). That delegated method determines whether nested state is copied recursively.

If either object value cannot be copied, the method throws the string The source cannot be copied with value type \`. When target is omitted, the method creates a plain [Tween`](</api/tween/component/Tween>) rather than preserving the source’s concrete subclass; passing a target lets callers choose the object that is populated. The populated target is returned.

Type Parameters

U

Parameters

NameDescriptionDefault
source

Tween whose state is copied.

—
target?

Target to overwrite; defaults to a new base Tween.

...

Throws

If an object-valued endpoint has no callable static copy method.

Returns

The populated target.

staticclone<U>(target: Tween<U>): Tween<U>

View source

Creates a new base Tween containing copied state.

This delegates to Tween.copy without a target, so endpoint objects are copied with their static copy methods and the result is a plain Tween even when the source is a concrete tween subclass.

Type Parameters

U

Parameters

NameDescriptionDefault
target

Tween whose state is copied.

—

Throws

If an object-valued endpoint has no callable static copy method.

Returns

A new base Tween containing copied state.