Gleam - v1.0.2
    Preparing search index...

    Class Animator

    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 a namespace reuse the same rendered images.

    Use play to switch animations, playOnce for one-shot animations that fall back to the previous one, and Animator.onEnd/onFrame callbacks for frame- or animation-end hooks.

    Index

    Constructors

    • 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.

      Parameters

      Returns Animator

    Properties

    active: boolean = true

    When false, update is a no-op. Set automatically to false after a single-frame animation finishes and via reset when no default animation exists.

    image: HTMLCanvasElement

    Current rendered frame (after flip processing). Pulled from the cache by setImage every time the frame advances.

    imageId: number = 0

    Index of the current frame within the current animation's sprites array.

    lookLeft: boolean = false

    Flip the rendered sprite horizontally. Caches a separate "flipped" bucket so toggling is cheap.

    onEnd: onEndType | undefined

    One-shot callback that fires when the current animation's last frame finishes. Cleared after firing.

    size: Vec2 = ...

    Current sprite's (width, height). Updated by setImage.

    Accessors

    Methods

    • 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.

      Parameters

      • name: string
      • sprites: HTMLCanvasElement[] | HTMLImageElement[]
      • timing: number
      • defaultAnim: boolean = false

      Returns void

    • Blit the current cached frame at entity.pos + offset, shifted left by size.x to compensate for the 2×-wide cache canvas.

      Parameters

      • context: CanvasRenderingContext2D
      • offset: Vec2 = ...

      Returns void

    • 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.

      Parameters

      • context: CanvasRenderingContext2D
      • angle: number
      • offset: Vec2 = ...

      Returns void

    • true when name matches the currently-playing animation.

      Parameters

      • name: string

      Returns boolean

    • 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.

      Parameters

      Returns void

    • Queue an animation to play once the current one finishes. Pass undefined to cancel the queue. Used internally by playOnce.

      Parameters

      • name: string | undefined

      Returns void

    • Play name once, then return to the previously-playing animation. Calling with the currently-playing name re-loops it indefinitely (lastPlayed restores to itself).

      Parameters

      Returns void

    • Randomize the frame timer to a value in [0, current.timing). Useful when spawning many instances of the same animation to break phase lockstep.

      Returns void

    • Drop every registered animation and clear this instance's cached frames. active resets to true; pending callbacks are cleared.

      Returns void

    • Switch back to the default animation if one was registered (marked via defaultAnim); otherwise just stop animating (active = false).

      Returns void

    • Update 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.

      Returns void

    • Advance the timer; when it crosses current.timing, step to the next frame, fire any onFrame[index] callback, and on rollover fire onEnd and queue lastPlayed (set by playOnce). No-op when active is false.

      Parameters

      • dt: number

      Returns void

    • Drop cached rendered sprites. Pass a namespace to evict only that prefix; omit to clear all.

      Parameters

      • Optionalnamespace: string

      Returns void