rig.js

Characters that react, from one file.

A small animation rig for 2D games. You describe a character as data and it breathes, blinks, looks and flinches by itself. The rig only computes numbers, so you keep drawing with what you already use: canvas, SVG, WebGL or the DOM. No editor, no file format, no dependency.

Build a character in seven steps

Each step adds a few lines to the definition on the left. The drawing code and the game loop never change. Move the pointer over the stage, click it, and edit any number: the character updates as you type.

What the rig outputs: channels, last 4 seconds

What else is in the file

Four live stages, each with the part of its definition that does the work. Click or move over them.

Limbs that reach

Give a target; a constraint solves the two angles. The arms take the elbow side that keeps them off the face, and the head turns within its limits.

constraints: [
  { ik: 'armL', bones: ['armL.a1', 'armL.a2'],
    lens: [62, 58], root: [-34, -120],
    target: 'hand', bend: 1,
    avoid: [{ at: [0, -168], r: 40 }] },
  { aim: 'head.rot', root: [0, -168],
    target: 'hand', limit: [-0.5, 0.5] },
]

Steps that come from the animation

The walker moves only when a foot lands. Each step is an event keyed in the clip, fired once at any frame rate. Hold the pointer down to run: the stride is handed over, never doubled.

clips: { walk: { dur: 0.8, loop: true, tracks: { … },
  events: [[0.2, 'step', { foot: 'L' }],
           [0.6, 'step', { foot: 'R' }]] } },
sync: [['walk', 'run']],

for (const e of rig.events)
  if (e.name === 'step') moveBy(stride);

A crowd that watches

Click anywhere. Each watcher hears of it when the news reaches it, looks that way, and plays a take it did not just play. Keep clicking: they remember and grow wary.

on: { gasp: { take: ['hop', 'squash',
  { clip: 'wide', when: (d) => d.dist > 120 }] } },

const crowd = Rig.audience({ speed: 420,
  memory: { life: 6, full: 4, input: 'wary' } });
crowd.add(rig, x, y);
crowd.react(x, y, 'gasp');

One picture, bent by bones

A single drawing on a mesh of points. Five bones bend it and a bulge runs down the body. Bones are plain channels, so clips, springs or your own code move them. Click to show the mesh.

meshes: { eel: Object.assign(
  Rig.mesh.grid(0, 0, 320, 56, 16, 2),
  { bones: [{ name: 'b0', at: [0, 28], to: [64, 28] }, … ] }) },

Rig.mesh.draw(ctx, picture, rig, 'eel');

Use it

Copy one file or install from GitHub. The same file works as a script tag, with require, and as an ES module with types.

Script tag
<script src="rig.js"></script>
<script>
  const rig = Rig.create(def);
</script>
Install
npm install github:Kodoque1/rig
Import
import Rig from '@kodoque1/rig';

const rig = Rig.create(def, seed);
rig.update(dt);
rig.v('body.sy');