AnimationClip

View source

Represents an animation asset composed of named AnimationTrack values.

Tracks are grouped by target IDs supplied by AnimationTarget, while the clip stores the combined animation duration. A new clip starts empty with a duration of 0; use AnimationClip#add and AnimationClip#calculateDuration to populate the asset.

Examples

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

const clip = new AnimationClip()
const track = new AnimationTrack(Position2DAnimationEffector)
track.times = [0, 1]
track.keyframes = [0, 0, 1, 1]
clip.add('/player/position', track)
clip.calculateDuration()
console.log(clip.duration) // 1

Constructor


constructor()

Properties


duration: number

View source

Clip length in the same time units as its track timestamps.

It starts at 0 and is only recomputed by AnimationClip#calculateDuration; adding, removing, or mutating a track does not update it automatically. The value is copied as-is by serialization.

Default: 0

Methods


add(name: string, track: AnimationTrack): AnimationClip

View source

Adds a track under a target id.

An existing id receives the track at the end of its array; a new id gets a one-element array. This method does not validate the track or recalculate AnimationClip#duration.

Parameters

NameDescriptionDefault
name——
track——

Returns

This clip, for fluent setup.

getTracks(id: string): AnimationTrack[]

View source

Returns the tracks assigned to a target id.

The returned array is the live array stored in the clip, so changing it changes the clip. The method returns undefined when the id is not in the map.

Parameters

NameDescriptionDefault
id——

remove(name: string): void

View source

Removes every track assigned to a target id.

Missing ids are ignored. The method does not recalculate AnimationClip#duration and returns undefined.

Parameters

NameDescriptionDefault
name——

calculateDuration(): void

View source

Recomputes the clip duration from its tracks.

The method resets duration to 0, then compares the final value in each track’s times array and keeps the greatest value. Empty tracks and final timestamps that do not exceed 0 leave the duration at 0. It does not sort or validate tracks and returns undefined.

validate(): boolean

View source

Validates every track currently stored in the clip.

Returns false as soon as a track’s AnimationTrack#validate method returns false; an empty clip is valid. This checks track values only and does not validate the duration or target-id keys.

Returns

Whether every stored track is valid.

staticdefault(): AnimationClip

View source

Creates a fresh empty clip.

Each call is equivalent to new AnimationClip(): it returns a distinct instance with no tracks and a duration of 0. It does not calculate a duration or share state with another clip.

Examples

const clip = AnimationClip.default()
console.log(clip.duration) // 0

Returns

A new empty clip.

staticserialize(value: AnimationClip): AnimationClipSerial

View source

Converts a clip to its serialized representation.

The tracks map becomes an array of [targetId, tracks] entries in map insertion order, and each nested track is passed to AnimationTrack.serialize. The stored duration is copied as-is; serialization does not calculate or validate it and does not mutate the clip.

Parameters

NameDescriptionDefault
value

Clip to serialize.

—

Returns

Serialized clip data.

staticvalidateSerial(value: unknown): value is AnimationClipSerial

View source

Checks the serialized shape accepted by AnimationClip.deserialize.

duration must be a JavaScript number and tracks must be an array of two-item [targetId, tracks] entries. Each target id must be a string and each nested value must pass AnimationTrack.validateSerial. Empty track arrays and duplicate target ids are accepted; this method does not check duration against timestamps or run AnimationTrack#validate.

Parameters

NameDescriptionDefault
value

Value to validate.

—

Returns

Whether the value has a valid clip serial shape.

staticdeserialize(value: AnimationClipSerial, out?: AnimationClip): AnimationClip

View source

Restores a serialized clip into a new or supplied output instance.

The output’s duration is copied and its tracks map is replaced with a new map; each serialized track is restored through AnimationTrack.deserialize. This method does not validate or recalculate the restored data, so validate external input with AnimationClip.validateSerial first.

Parameters

NameDescriptionDefault
value

Serialized clip data.

—
out?

Clip to populate.

...

Returns

The populated output clip.