Bind to entity and stamp namespace as the prefix for cache keys (${namespace}.${animationName}). Use distinct namespaces for animators whose sprite sets differ — sharing a namespace across mismatched sprite sets serves the wrong cached frames.
Current rendered frame (after flip processing). Pulled from the cache by setImage every time the frame advances.
Index of the current frame within the current animation's sprites array.
Flip the rendered sprite horizontally. Caches a separate "flipped" bucket so toggling is cheap.
One-shot callback that fires when the current animation's last frame finishes. Cleared after firing.
Current sprite's (width, height). Updated by setImage.
The currently-playing animation. undefined if no animations have been added yet.
Register a new animation. Logs an error (but still registers) if defaultAnim is true while another default already exists, or if name collides with an existing animation. Auto-plays the new animation when defaultAnim is true.
Convenience wrapper around add that takes a packed SpriteAnimation. defaultAnim is OR'd with anim.default.
Blit the current cached frame at entity.pos + offset, shifted left by size.x to compensate for the 2×-wide cache canvas.
Draw the current frame rotated by angle radians around a sprite-relative pivot (75% width, 50% height). Uses setTransform and resets the transform on exit.
true when name matches the currently-playing animation.
Switch to the named animation, rewinding timer and frame index. Optionally register onEnd (fires after the last frame) and onFrame (frame-indexed callbacks). Any previous Animator.onEnd fires before the new one is set. Throws if name isn't registered.
OptionalonEnd: onEndTypeOptionalonFrame: onFrameTypeplay only if name isn't already the current animation. Returns true if it started a new playback, false if it was already playing.
OptionalonEnd: onEndTypeOptionalonFrame: onFrameTypeQueue an animation to play once the current one finishes. Pass undefined to cancel the queue. Used internally by playOnce.
Play name once, then return to the previously-playing animation. Calling with the currently-playing name re-loops it indefinitely (lastPlayed restores to itself).
OptionalonEnd: onEndTypeOptionalonFrame: onFrameTypeRandomize the frame timer to a value in [0, current.timing). Useful when spawning many instances of the same animation to break phase lockstep.
Drop every registered animation and clear this instance's cached frames. active resets to true; pending callbacks are cleared.
Switch back to the default animation if one was registered (marked via defaultAnim); otherwise just stop animating (active = false).
ProtectedsetUpdate image and size from the current sprite. Assumes uniform sprite size within an animation. Caches rendered canvases per (animation, frame, lookLeft) — if you mutate entity.flipX after first render, call removeAllAnimations() or recreate the Animator to invalidate.
StaticclearDrop cached rendered sprites. Pass a namespace to evict only that prefix; omit to clear all.
Optionalnamespace: string
Sprite-sheet animator. Hosts a list of named animations (registered via add / addAnimation) and drives them per-frame from
update(dt). Pre-renders each frame to a 2×-wide cached canvas keyed by${namespace}.${animationName}— so multiple entities sharing anamespacereuse the same rendered images.Use play to switch animations, playOnce for one-shot animations that fall back to the previous one, and Animator.onEnd/
onFramecallbacks for frame- or animation-end hooks.