Skip to content

Animation

SpriteAnimation is a CPU-side frame sequencer for spritesheet animations. It produces Rect values to pass as sourceRect to SpriteBatch.draw().

import { SpriteAnimation } from 'webgpu-spritebatch'
const anim = SpriteAnimation.fromGrid(texture, {
columns: 8,
rows: 4,
frameCount: 30, // defaults to columns x rows
frameDuration: 0.1, // seconds per frame
mode: 'loop', // 'loop' | 'once' | 'pingPong'
})
const anim = SpriteAnimation.fromRects(myRects, {
frameDuration: 0.1,
mode: 'loop',
})

Extract sub-animations from a master animation without copying frame data:

const idle = sheet.slice(0, 8)
const attack = sheet.pick([16, 17, 18, 19, 18, 17])
const death = sheet.slice(24, 30, { frameDuration: 0.15, mode: 'once' })
// Each frame:
anim.update(deltaTime)
batch.draw(spriteSheet, {
position: [x, y],
sourceRect: anim.currentFrame,
})
PropertyTypeDescription
currentFrameRectSource rectangle for the current frame
frameIndexnumberCurrent frame index
frameCountnumberTotal frames
isCompletebooleantrue when mode is 'once' and finished
isPlayingbooleanWhether the animation is playing
speednumberPlayback speed multiplier (default 1)
modeAnimationModePlayback mode
frameDurationnumberSeconds per frame
MethodDescription
update(dt)Advance by dt seconds
play()Resume (resets if complete)
pause()Pause at current frame
reset()Rewind to frame 0 and start playing