AnimationTrack

View source

Stores and evaluates timed numeric keyframes for one animated property.

An AnimationClip can group tracks by name. A track pairs one time entry with each flattened keyframe value set, using its effector to determine the values consumed per keyframe. Evaluate it with AnimationTrack#getCurrent, then pass the result to that effector for the target selected by AnimationTarget.

The track performs component-wise linear interpolation only; it does not apply values to an entity itself. Use an AnimationEffector such as Position2DAnimationEffector to consume the evaluated values.

Examples

import {
  AnimationClip,
  AnimationTrack,
  Position2DAnimationEffector
} from '@wimaengine/animation'

const track = new AnimationTrack(Position2DAnimationEffector)
track.times = [0, 1]
track.keyframes = [0, 0, 10, 4]

const clip = new AnimationClip().add('player', track)
clip.calculateDuration()
console.log(track.getCurrent(0.5)) // [5, 2]
// A target with id 'player' associates this track with that target.

Constructor


Properties


keyframes: number[]

View source

Flattened numeric keyframe values. Each timestamp consumes exactly effector.elementSize() consecutive entries.

Default: []

effector: AnimationEffectorEnforcer

View source

Effector contract used to determine keyframe width and later apply the interpolated values to an entity. The reference is stored directly.

Methods


validate(): boolean

View source

Checks the in-memory keyframe layout.

Timestamps must be in nondecreasing order, and the flattened keyframe length must equal times.length * effector.elementSize(). The method does not check that entries are finite numbers, does not require a non-empty track, and can throw if the effector does not provide elementSize(). Empty arrays therefore pass when the effector reports a positive element size, although interpolation still requires at least one keyframe.

Returns

Whether the arrays satisfy the track layout.

staticserialize(value: AnimationTrack): AnimationTrackSerial

View source

Converts a track to serialized arrays and its effector reference.

The times and keyframes arrays are shallow-copied. The effector reference is returned directly; it is neither cloned nor converted.

Parameters

NameDescriptionDefault
value

Track to serialize.

—

Returns

Serialized track data.

staticdeserialize(value: AnimationTrackSerial, out?: AnimationTrack): AnimationTrack

View source

Restores serialized track fields into a track value.

Both arrays are copied with slice(), and the effector reference is assigned directly. The method does not call AnimationTrack#validate or validate the serialized input; when out is omitted, it first creates a track with a temporary null effector and then replaces it.

Parameters

NameDescriptionDefault
value

Serialized track data.

—
out?

Track to populate.

...

Returns

The populated output track.

staticvalidateSerial(value: unknown): value is AnimationTrackSerial

View source

Checks the shallow serialized shape used by AnimationTrack.deserialize.

A non-null object must contain times, keyframes, and effector. times and keyframes must be arrays containing only values whose typeof is "number"; effector only needs to be non-null and non-undefined. This check does not verify timestamp ordering, keyframe width, finite values, or the effector’s methods.

Parameters

NameDescriptionDefault
value——

Returns

Whether the serialized shape is valid.

getCurrent(delta: number): number[]

View source

Evaluates the track at a time using component-wise linear interpolation.

Values before or at the first timestamp resolve to the first keyframe; values after or at the last timestamp resolve to the last keyframe. A time between keyframes is interpolated independently for each effector element. The track must contain at least one keyframe and have a valid effector layout; this method does not call validate() first.

Parameters

NameDescriptionDefault
delta

Time at which to evaluate the track.

—

Returns

Interpolated values with the effector’s element size.

getCurrentLerp(delta: number): [number, number, number]

View source

Finds the keyframe interval and interpolation amount for a time.

The result is [indexA, indexB, t]. At or before the first timestamp it is [0, 0, 0]; at or after the last timestamp it is [lastIndex, lastIndex, 0]. For an interior time, indexA is the preceding keyframe, indexB is the first keyframe whose timestamp is at least delta, and t is the normalized distance between them.

This method assumes a non-empty, nondecreasing times array and does not validate it. Duplicate timestamps are permitted by validate(); interval selection at a duplicate boundary follows the first timestamp that is at least delta.

Parameters

NameDescriptionDefault
delta

Time at which to find an interval.

—

Returns

Two keyframe indexes and interpolation amount.