aispritejs
v0.6.0
Published
Input-driven, renderer-agnostic 2D sprite animation runtime — a tiny, Rive-like visual state machine driven by Number / Boolean / Trigger inputs. JSON transition graph, deterministic update(dt), zero runtime dependencies. Browser / Node / Bun / Deno / Web
Maintainers
Readme
aispritejs
Input-driven, renderer-agnostic 2D sprite animation runtime. A JSON graph maps Number/Boolean/Trigger inputs to visual states and frames; adapters bind the chosen frame to a renderer.
Status: 0.6.0 - stable family-aligned surface. Core, PixiJS adapter, atlas parser, and JSON Schema subpath are shipped.
Install
pnpm add aispritejs
pnpm add pixi.js # only when using aispritejs/pixiimport { createSpriteAnimator } from "aispritejs";Quick Start - Core
const anim = createSpriteAnimator({
inputs: {
speed: { type: "number", default: 0 },
jump: { type: "trigger" },
},
initial: "idle",
states: {
idle: { animation: "idle", loop: true },
run: { animation: "run", loop: true, speed: 1 },
jump: { animation: "jump", loop: false, onEnd: "idle" },
},
transitions: [
{ from: "idle", to: "run", when: [{ input: "speed", op: "GreaterThan", value: 0 }] },
{ from: "*", to: "jump", when: [{ input: "jump", op: "Trigger" }] },
],
animations: {
idle: ["idle_0"],
run: ["run_0", "run_1"],
jump: ["jump_0", "jump_1"],
},
});
anim.setInput("speed", 1);
anim.fireTrigger("jump");
anim.update(16.7);
console.log(anim.activeState, anim.activeFrameKey);PixiJS Adapter
import { createPixiSpriteAnimator } from "aispritejs/pixi";
const view = createPixiSpriteAnimator(sprite, graph, spritesheet);
view.update(deltaMs);
view.dispose(); // disposes core animator; does not destroy the Pixi spritepixi.js is an optional peer dependency and is imported type-only by the adapter. The root package never imports Pixi, DOM, or canvas APIs.
Atlas and Schema
parseAtlas(atlas, control?)converts a PixiJS-v8 style atlas plus control block into aSpriteGraph.loadAtlas(atlas, control?)parses and creates aSpriteAnimator.aispritejs/schemaexportsschemas/aispritejs-graph.schema.jsonfor editor/CI validation.- Parser validation is structural (
InvalidAtlasError); compiler validation is semantic (InvalidGraphError). - An explicit
controlgets the same structural checks as a control block inside the atlas, and itsinitial/defaultFrameDurationmust be a string / number (an atlas's own block drops wrong-typed ones).
Core API
createSpriteAnimator(graph)returns aSpriteAnimator; a malformed or invalid graph throwsInvalidGraphErrorand builds nothing.setInput(name, value)accepts Number/Boolean inputs.fireTrigger(name)consumes Trigger inputs on transition.update(deltaMs)advances time, transitions, frame index, andonEnd. A negative, non-finite, or overflowing step is clamped to no progress.reset()returns to the initial state.dispose()is idempotent; mutators throwSpriteAnimatorDisposedErrorafterward.onStateChange(handler, options?)andonComplete(handler, options?)supportonceandsignal.
Sharp Edges
- Non-looping states with
onEndtransition during the sameupdate()tick that completes the clip. reset()/update()called from insideonStateChange/onCompleterun after the current update finishes (including itsonEndauto-transition); areset()fromonCompletetherefore yieldsonStateChange(onEndTarget, from)thenonStateChange(initial, onEndTarget), ending in the initial state;dispose()from a listener stops the update immediately.AnimatedSpriteis accepted by the Pixi adapter because it extendsSprite, but playback is stopped on bind so it cannot fight the adapter.- Every frame key in every declared animation must exist in the texture map/spritesheet — including an animation no state references — or the adapter throws
MissingTextureErrorat construction. Anull/undefinedentry counts as missing. - A texture map with an object under the key
texturesis read as aSpritesheet; if a frame is literally namedtextures, pass theSpritesheet(or{ textures: map }) instead of the bare map. duration,defaultFrameDuration, and statespeedmust be finite numbers greater than zero.initial, stateanimation/onEnd, transitionfrom/to, and conditioninput/opmust be strings, and a transitionprioritymust be an integer.
AI Context
- Short index:
llms.txt - Full generated context:
llms-full.txt - Stability contract:
STABILITY.md - Current review backlog:
REVIEW.md - Examples index:
examples/README.md - Release history:
CHANGELOG.md
License
MIT
