Animation Manager
Sprite sheet animations (idle, run, jump) and tweens that ease any number from one value to another (menus sliding in, damage numbers floating up).
Files: animation-manager.js (the component, no dependencies) and animation-manager.test.mjs (6 tests).
Copy animation-manager.js into your game, for example to src/components/, and import it as an ES module.
Usage
import { createAnimator, createTweens, ease } from "./animation-manager.js";
const anim = createAnimator({
idle: { frames: [0, 1, 2, 1], fps: 6 },
run: { frames: [3, 4, 5, 6], fps: 12 },
attack: { frames: [7, 8, 9], fps: 15, loop: false, next: "idle" },
});
anim.play(player.vx ? "run" : "idle"); // calling play with the current clip does nothing
anim.update(dt); drawFrame(sheet, anim.frame, x, y);
const tweens = createTweens();
tweens.to(label, { y: label.y - 30, alpha: 0 }, 0.6, { ease: ease.outCubic, onDone: () => remove(label) });
tweens.update(dt);
API
createAnimator(clips, { speed }) → animclip: { frames, fps, loop = true, next, events: { frameIndex: “name” } }anim.play(name, { restart }) / anim.update(dt) / anim.frame / anim.clip / anim.done / anim.speedanim.on(fn)fn(“end”, clip) or fn(eventName, clip) for clip events (footstep sounds, hit frames)gridFrame(index, { frameW, frameH, columns }) → { sx, sy, sw, sh }source rectangle in a sprite sheetcreateTweens() → tweenstweens.to(target, props, seconds, { ease, delay, onDone, onUpdate, repeat, yoyo }) → { cancel(), done }tweens.update(dt) / tweens.cancelAll(target?) / tweens.count
ease: linear, inQuad, outQuad, inOutQuad, outCubic, inOutCubic, outBack, outElastic, outBounce
Engines
| Engine | Notes |
|---|---|
| Vanilla / Canvas 2D | draw with gridFrame(). |
| Phaser | use Phaser’s anims and tweens; these suit games without an engine. |
| PixiJS | AnimatedSprite covers sprite clips; the tweens work on any Pixi object (position.x, alpha, scale.x). |
| Three.js | the tweens work on mesh.position, material.opacity and so on; the animator suits sprite-sheet textures (set texture.offset from gridFrame). |
Tests
Node tests: frame timing, loops, one-shot clips and next, events, speed, sheet rectangles, tween values, delay, ease ends, yoyo and repeat, cancel.
Run them with node --test toolkit/components/animation-manager/ (Node 20 or newer, nothing to install).