Class
AnimationClip
View sourceRepresents 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) // 1Constructor
Properties
tracks: Map<string, AnimationTrack[]>
View sourceAnimation tracks grouped by their target id.
Each map value preserves the order in which tracks were added, so several
tracks can contribute to the same target. AnimationClip#add,
AnimationClip#getTracks, and AnimationClip#remove operate
on these live arrays and do not recalculate AnimationClip#duration.
Default: ...
duration: number
View sourceClip 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 sourceAdds 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.
getTracks(id: string): AnimationTrack[]
View sourceReturns 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.
remove(name: string): void
View sourceRemoves every track assigned to a target id.
Missing ids are ignored. The method does not recalculate
AnimationClip#duration and returns undefined.
calculateDuration(): void
View sourceRecomputes 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 sourceValidates 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.
staticdefault(): AnimationClip
View sourceCreates 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) // 0staticserialize(value: AnimationClip): AnimationClipSerial
View sourceConverts 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.
staticvalidateSerial(value: unknown): value is AnimationClipSerial
View sourceChecks 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.
staticdeserialize(value: AnimationClipSerial, out?: AnimationClip): AnimationClip
View sourceRestores 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.
