@anime-studio/kit 0.0.0-stage → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +58 -2
  3. package/dist/cartoon.d.ts +368 -0
  4. package/dist/cartoon.d.ts.map +1 -0
  5. package/dist/cartoon.js +2815 -0
  6. package/dist/cartoon.js.map +1 -0
  7. package/dist/human.d.ts +165 -0
  8. package/dist/human.d.ts.map +1 -0
  9. package/dist/human.js +433 -0
  10. package/dist/human.js.map +1 -0
  11. package/dist/humanFront.d.ts +18 -0
  12. package/dist/humanFront.d.ts.map +1 -0
  13. package/dist/humanFront.js +137 -0
  14. package/dist/humanFront.js.map +1 -0
  15. package/dist/index.d.ts +9 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +9 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/info.d.ts +96 -0
  20. package/dist/info.d.ts.map +1 -0
  21. package/dist/info.js +277 -0
  22. package/dist/info.js.map +1 -0
  23. package/dist/proportions.d.ts +37 -0
  24. package/dist/proportions.d.ts.map +1 -0
  25. package/dist/proportions.js +115 -0
  26. package/dist/proportions.js.map +1 -0
  27. package/dist/rig.d.ts +538 -0
  28. package/dist/rig.d.ts.map +1 -0
  29. package/dist/rig.js +1381 -0
  30. package/dist/rig.js.map +1 -0
  31. package/dist/room.d.ts +124 -0
  32. package/dist/room.d.ts.map +1 -0
  33. package/dist/room.js +217 -0
  34. package/dist/room.js.map +1 -0
  35. package/dist/util.d.ts +27 -0
  36. package/dist/util.d.ts.map +1 -0
  37. package/dist/util.js +29 -0
  38. package/dist/util.js.map +1 -0
  39. package/dist/views.d.ts +68 -0
  40. package/dist/views.d.ts.map +1 -0
  41. package/dist/views.js +92 -0
  42. package/dist/views.js.map +1 -0
  43. package/dist/volume.d.ts +105 -0
  44. package/dist/volume.d.ts.map +1 -0
  45. package/dist/volume.js +234 -0
  46. package/dist/volume.js.map +1 -0
  47. package/package.json +45 -3
package/dist/rig.js ADDED
@@ -0,0 +1,1381 @@
1
+ /**
2
+ * Building blocks for storybook cartoon characters (profile view, facing right, feet at y = 0).
3
+ *
4
+ * Characters built with the kit share bone names — root, ground, hips, body, head, pupils,
5
+ * armF1/armF2/handF (near arm), armB1/armB2/handB (far arm), legF1/legF2/footF and
6
+ * legB1/legB2/footB — so one set of gesture clips (`characterClips`), emotion poses and the
7
+ * director's staging work for every character. Colours use palette tokens; `palette(ink)` is the
8
+ * outline colour.
9
+ */
10
+ import { makeTrack, parsePath, pathPoints, pathToString, sampleTrack, withPoints } from "@anime-studio/core";
11
+ export const r = (n) => Math.round(n * 100) / 100;
12
+ export function mouthPath(L, R, open, smile, round, o = {}) {
13
+ const w = R[0] - L[0];
14
+ const k = o.lift ?? 1;
15
+ const l = [L[0] + w * round * 0.22, L[1] - smile * (o.symmetric ? 5 : 4) * k];
16
+ const rr = [R[0] - w * round * 0.22, R[1] - smile * (o.symmetric ? 5 : 6) * k];
17
+ const mx = (l[0] + rr[0]) / 2;
18
+ const topY = (l[1] + rr[1]) / 2 + smile * 3 * k - open * 0.15;
19
+ const botY = (l[1] + rr[1]) / 2 + smile * 4 * k + open;
20
+ return `M${r(l[0])} ${r(l[1])} Q${r(mx)} ${r(topY)} ${r(rr[0])} ${r(rr[1])} Q${r(mx)} ${r(botY + open * 0.4)} ${r(l[0])} ${r(l[1])} Z`;
21
+ }
22
+ /** Viseme mouths + expression shapes for a mouth with corners L/R. */
23
+ export function mouthShapes(L, R, scale = 1, style = {}) {
24
+ const m = (o, s, rd) => mouthPath(L, R, o * scale, s, rd, style);
25
+ return {
26
+ base: m(1.5, 0.7, 0),
27
+ shapes: {
28
+ A: m(0.8, 0.4, 0.05),
29
+ B: m(6, 0.6, 0.05),
30
+ C: m(12, 0.5, 0.1),
31
+ D: m(18, 0.4, 0),
32
+ E: m(13, 0.2, 0.45),
33
+ F: m(7, 0, 0.75),
34
+ G: m(4, 0.5, 0.1),
35
+ H: m(10, 0.4, 0.15),
36
+ smile: m(4, 2.2, 0),
37
+ frown: m(1.5, -1.4, 0.1),
38
+ grin: m(14, 2.4, 0),
39
+ },
40
+ };
41
+ }
42
+ /** A round storybook eye (white + outline). */
43
+ export const eyeWhite = (c, rad, extra = "") => `<circle cx="${c[0]}" cy="${c[1]}" r="${rad}" fill="#fff" stroke="palette(ink)" stroke-width="3"/>${extra}`;
44
+ export const eyeArc = (c, rad, up) => `<path d="M${c[0] - rad} ${c[1]} Q${c[0]} ${c[1] + (up ? -rad * 1.1 : rad * 0.9)} ${c[0] + rad} ${c[1]}" fill="none" stroke="palette(ink)" stroke-width="3.6" stroke-linecap="round"/>`;
45
+ /** Eye with the top covered by a lid of the skin colour (half-closed / sleepy). */
46
+ export const eyeLid = (c, rad, k, skin, id) => {
47
+ const y = c[1] - rad + 2 * rad * k;
48
+ return (`<clipPath id="${id}"><circle cx="${c[0]}" cy="${c[1]}" r="${rad}"/></clipPath>` +
49
+ `<rect x="${c[0] - rad - 2}" y="${c[1] - rad - 2}" width="${rad * 2 + 4}" height="${r(y - c[1] + rad + 2)}" fill="${skin}" clip-path="url(#${id})"/>` +
50
+ `<path d="M${c[0] - rad + 1} ${r(y)} H${c[0] + rad - 1}" stroke="palette(ink)" stroke-width="3" stroke-linecap="round"/>` +
51
+ `<circle cx="${c[0]}" cy="${c[1]}" r="${rad}" fill="none" stroke="palette(ink)" stroke-width="3"/>`);
52
+ };
53
+ export function eyeArt(e) {
54
+ const both = (fn) => e.eyes.map(fn).join("");
55
+ const rest = (c, i) => (e.restLid ? eyeLid(c, e.rad, e.restLid, e.skin, `lid${i}`) : "");
56
+ return {
57
+ open: both((c, i) => eyeWhite(c, e.rad)),
58
+ wide: both((c) => eyeWhite(c, e.rad + 2.5)),
59
+ closed: both((c) => eyeArc(c, e.rad * 0.85, false)),
60
+ happy: both((c) => eyeArc(c, e.rad * 0.85, true)),
61
+ half: both((c, i) => eyeWhite(c, e.rad)),
62
+ lids: both(rest),
63
+ halfLids: both((c, i) => eyeLid(c, e.rad, Math.max(0.45, e.restLid ?? 0), e.skin, `hl${i}`)),
64
+ pupils: both((c) => `<circle cx="${r(c[0] + e.rad * 0.3)}" cy="${r(c[1] + 1)}" r="${e.pupil}" fill="palette(ink)"/><circle cx="${r(c[0] + e.rad * 0.3 + e.pupil * 0.35)}" cy="${r(c[1] - e.pupil * 0.4)}" r="${r(Math.max(1.5, e.pupil * 0.3))}" fill="#fff"/>`),
65
+ };
66
+ }
67
+ /** Brows as one morph path (two strokes), with raised / sad / cross / smug shapes. */
68
+ export function browShapes(eyes, rad, lift = 10) {
69
+ const brow = (c, dy, tiltIn) => {
70
+ const y = c[1] - rad - lift + dy;
71
+ return `M${r(c[0] - rad * 0.7)} ${r(y + tiltIn)} Q${r(c[0])} ${r(y - 4)} ${r(c[0] + rad * 0.7)} ${r(y - tiltIn)}`;
72
+ };
73
+ const both = (dy, tilt) => brow(eyes[0], dy, tilt) + " " + brow(eyes[1], dy, tilt);
74
+ return { base: both(0, 0), shapes: { up: both(-8, 0), sad: both(-2, -5), cross: both(3, 5), smug: brow(eyes[0], 2, 2) + " " + brow(eyes[1], -6, -2) } };
75
+ }
76
+ /** Bones every cast member has (near/far arm and leg chains, feet that stay flat). */
77
+ export function limbBones(o) {
78
+ return [
79
+ { id: "armB1", parent: "body", from: o.shoulderB, to: o.elbowB, mass: 0.5 },
80
+ { id: "armB2", parent: "armB1", from: o.elbowB, to: o.handB, mass: 0.4 },
81
+ { id: "handB", parent: "armB2", from: o.handB },
82
+ { id: "armF1", parent: "body", from: o.shoulderF, to: o.elbowF, mass: 0.5 },
83
+ { id: "armF2", parent: "armF1", from: o.elbowF, to: o.handF, mass: 0.4 },
84
+ { id: "handF", parent: "armF2", from: o.handF },
85
+ { id: "legB1", parent: "hips", from: o.hipB, to: o.kneeB, mass: 0.6 },
86
+ { id: "legB2", parent: "legB1", from: o.kneeB, to: o.footB, mass: 0.5 },
87
+ { id: "footB", parent: "legB2", from: o.footB, to: [o.footB[0] + o.toeB, o.footB[1]], inheritRotation: false },
88
+ { id: "legF1", parent: "hips", from: o.hipF, to: o.kneeF, mass: 0.6 },
89
+ { id: "legF2", parent: "legF1", from: o.kneeF, to: o.footF, mass: 0.5 },
90
+ { id: "footF", parent: "legF2", from: o.footF, to: [o.footF[0] + o.toeF, o.footF[1]], inheritRotation: false },
91
+ ];
92
+ }
93
+ export const limbIk = [
94
+ // Knees bend forward, in the walking direction (like the rest pose).
95
+ { id: "footF", bones: ["legF1", "legF2"], bend: 1 },
96
+ { id: "footB", bones: ["legB1", "legB2"], bend: 1 },
97
+ // Off by default; `hold_hands` turns them on and aims the hand at the partner's hand.
98
+ { id: "handF", bones: ["armF1", "armF2"], bend: 1, mix: 0 },
99
+ { id: "handB", bones: ["armB1", "armB2"], bend: 1, mix: 0 },
100
+ ];
101
+ /** Rounds a key time (four decimals: eighths of a short cycle stay exact enough). */
102
+ const rt = (t) => Math.round(t * 1e4) / 1e4;
103
+ /**
104
+ * A smooth loop: `base` at phase `shift` (fraction of the cycle) and `peak` half a period later,
105
+ * `n` periods per cycle — keys at the extremes, wrapped round the loop, plus keys at both ends with
106
+ * the same value (seamless).
107
+ */
108
+ function osc(d, base, peak, n, shift = 0) {
109
+ const mid = (base + peak) / 2;
110
+ const at = (p) => r(mid + (base - mid) * Math.cos(2 * Math.PI * n * (p - shift)));
111
+ const keys = new Map();
112
+ for (let k = 0; k < 2 * n; k++) {
113
+ const p = (((shift + k / (2 * n)) % 1) + 1) % 1;
114
+ keys.set(rt(p * d), r(k % 2 ? peak : base));
115
+ }
116
+ const ends = keys.get(0) ?? at(0);
117
+ keys.set(0, ends);
118
+ keys.set(rt(d), ends);
119
+ return [...keys].sort((x, y) => x[0] - y[0]).map(([t, v]) => [t, v]);
120
+ }
121
+ const wrap1 = (p) => ((p % 1) + 1) % 1;
122
+ /**
123
+ * A loop through values at phases (fractions of the cycle, wrapped round it): keys at those phases
124
+ * plus equal keys at 0 and `d`, the value at the seam taken from the loop's own spline (tangents
125
+ * across the seam, as a looping clip plays it) when no phase falls on it.
126
+ */
127
+ function loopKeys(d, pts) {
128
+ const ks = new Map();
129
+ // (A phase that rounds to the end of the cycle is its start.)
130
+ for (const [p, v] of pts) {
131
+ const t = rt(wrap1(p) * d);
132
+ ks.set(t >= rt(d) ? 0 : t, r(v));
133
+ }
134
+ const sorted = [...ks].sort((x, y) => x[0] - y[0]);
135
+ if (!ks.has(0)) {
136
+ // The spline of the loop starting at its first key; the seam is `d − t0` into it.
137
+ const t0 = sorted[0][0];
138
+ const tr = makeTrack([...sorted.map(([t, v]) => ({ t: t - t0, v })), { t: d, v: sorted[0][1] }], { loop: { duration: d } });
139
+ const v = r(sampleTrack(tr, d - t0));
140
+ sorted.unshift([0, v]);
141
+ }
142
+ sorted.push([rt(d), sorted[0][1]]);
143
+ return sorted.map(([t, v]) => [t, v]);
144
+ }
145
+ /** Distance the feet travel while planted in a cycle (the `linear` key segments of their x tracks). */
146
+ function plantedTravel(...xs) {
147
+ let s = 0;
148
+ for (const keys of xs)
149
+ for (let i = 0; i + 1 < keys.length; i++)
150
+ if (keys[i + 1][2] === "linear")
151
+ s += Math.abs(keys[i][1] - keys[i + 1][1]);
152
+ return s;
153
+ }
154
+ /**
155
+ * Locomotion cycle: feet glide linearly while planted (no sliding), body bobs twice per cycle.
156
+ * In the walk cycle the near foot is planted for the first half (its x from +a to −a), the far foot
157
+ * for the second, and the hips are lowest as a foot lands. With `flight` (or `heel`, `paw`) it is
158
+ * a run's cycle instead: each foot planted for (1 − flight)/2 of it with both feet in the air
159
+ * between the steps, the hips lowest at mid-stance and highest in mid-air, the swinging foot kicked
160
+ * up behind, driven through by the knee and reaching out (see `Gait`). `stride` is the body's travel
161
+ * per cycle — the planted foot's speed × the cycle (2a ÷ the planted fraction; the planted travel
162
+ * itself when a foot is planted half the cycle) — what a `walkTo` and the director match the actor's
163
+ * speed to, so the planted feet stay put. The optional fields make personality walks (a sneak, a
164
+ * strut, a tired shuffle, a march, a scared run) from the same cycle.
165
+ */
166
+ export function gait(g, arms = true) {
167
+ if (g.creep)
168
+ return creepGait(g, arms);
169
+ if (g.flight !== undefined || g.heel !== undefined || g.paw !== undefined)
170
+ return runGait(g, arms);
171
+ const d = g.dur;
172
+ const q = d / 4;
173
+ const knee = g.knee ?? 0, drag = g.drag ?? 0, slump = g.slump ?? 0, lag = g.headLag ?? 0;
174
+ const n = g.doubleBounce ? 4 : 2;
175
+ // Bobs: the original quarter keys, or `osc` (doubled, or delayed).
176
+ const wav = (base, peak, shift = 0) => n === 2 && !shift ? [[0, base], [q, peak], [2 * q, base], [3 * q, peak], [d, base]] : osc(d, base, peak, n, shift);
177
+ // On tiptoe the foot turns toe down about the ball of the foot (0.7 of its length): the ankle
178
+ // rises and comes forward by a constant amount, so a planted foot still does not slide.
179
+ const toe = g.tiptoe ? 40 : 0;
180
+ const ball = 0.7 * (g.foot ?? g.a * 0.9);
181
+ const h = g.tiptoe ? -r(ball * Math.sin((toe * Math.PI) / 180)) : 0;
182
+ const dx = g.tiptoe ? r(ball * (1 - Math.cos((toe * Math.PI) / 180))) : 0;
183
+ // The swing: higher with `knee` (and the foot ahead at the top), lower and trailing with `drag`.
184
+ const leg = g.leg ?? g.a * 7;
185
+ const lift = (g.lift + knee * 0.25 * leg) * (1 - 0.75 * drag);
186
+ const px = g.a * (0.45 * knee - 0.5 * drag);
187
+ const crouch = (g.crouch ?? 0) * leg;
188
+ const t = {
189
+ "ik.footF.x": [[0, g.a + dx], [2 * q, -g.a + dx, "linear"], [3 * q, px + dx], [d, g.a + dx]],
190
+ "ik.footF.y": [[0, h], [2 * q, h, "linear"], [3 * q, h - lift], [d, h]],
191
+ "ik.footB.x": [[0, -g.a + dx], [q, px + dx], [2 * q, g.a + dx], [d, -g.a + dx, "linear"]],
192
+ "ik.footB.y": [[0, h], [q, h - lift], [2 * q, h], [d, h, "linear"]],
193
+ "bones.hips.y": wav(g.bob + crouch, -g.bob + crouch),
194
+ "bones.body.rotation": wav(g.lean + slump, g.lean + slump + 1.5),
195
+ // The head looks ahead through a lean, less so through a slump (it hangs with the torso).
196
+ "bones.head.rotation": wav(-g.lean * 0.6 - slump * 0.35, -g.lean * 0.6 - slump * 0.35 + 2, lag),
197
+ };
198
+ if (toe || drag || knee) {
199
+ // Feet toe down on tiptoe; the toe trails (drag) or points (a high knee) in the swing.
200
+ const sw = r(toe + 18 * drag + 12 * knee);
201
+ t["bones.footF.rotation"] = [[0, toe], [2 * q, toe, "linear"], [3 * q, sw], [d, toe]];
202
+ t["bones.footB.rotation"] = [[0, toe], [q, sw], [2 * q, toe], [d, toe, "linear"]];
203
+ }
204
+ if (g.sway) {
205
+ // The hip over the planted foot rises (`spread`, a roll about the forward axis: seen from the
206
+ // front or three-quarters on a 2.5D rig); the torso rolls back to stay upright.
207
+ const s = g.sway;
208
+ t["bones.hips.spread"] = [[0, 0], [q, s], [2 * q, 0], [3 * q, -s], [d, 0]];
209
+ t["bones.body.spread"] = [[0, 0], [q, -s], [2 * q, 0], [3 * q, s], [d, 0]];
210
+ // The legs roll back as well: the hips rise and fall over them, the feet stay planted under the
211
+ // body (a leg rolled out with the hips would swing its foot sideways: a slide in three-quarters).
212
+ for (const leg of ["legF1", "legB1"])
213
+ t[`bones.${leg}.spread`] = [[0, 0], [q, -s], [2 * q, 0], [3 * q, s], [d, 0]];
214
+ }
215
+ if (arms)
216
+ Object.assign(t, gaitArms(g, 0.5));
217
+ return { duration: d, loop: true, stride: plantedTravel(t["ik.footF.x"], t["ik.footB.x"]), tracks: t };
218
+ }
219
+ /**
220
+ * The run's cycle (`gait` with `flight`, `heel` or `paw`). Key poses of the near foot, as phases
221
+ * of the cycle (the far foot is the same half a cycle later): contact at 0 (heel first, the toe up,
222
+ * the foot ahead by `a`), flat at once, planted back to −a at β = (1 − flight)/2 — the last part
223
+ * rolling onto the ball, the ankle rising and coming forward as the heel does so the ball stays put
224
+ * (no toe dug into the ground) — then the swing: trailing up behind in mid-air, the heel at its
225
+ * highest (`heel`), the knee drive under and ahead of the hips (`knee`), the reach past the landing
226
+ * point in the other step's mid-air (`paw`), and pawing back into the contact. The hips are lowest
227
+ * at mid-stance and highest in mid-air, crouched so the planted leg never locks straight.
228
+ */
229
+ function runGait(g, arms) {
230
+ const d = g.dur, a = g.a;
231
+ const flight = Math.min(0.6, Math.max(0, g.flight ?? 0));
232
+ const beta = (1 - flight) / 2;
233
+ const leg = g.leg ?? a * 7, knee = g.knee ?? 0, slump = g.slump ?? 0, lag = g.headLag ?? 0;
234
+ const heel = Math.min(0.85, Math.max(0, g.heel ?? 0.4)) * leg;
235
+ const paw = g.paw ?? 0.15, toeOff = g.toeOff ?? 35;
236
+ const ball = 0.7 * (g.foot ?? a * 0.9);
237
+ const lift = g.lift + knee * 0.25 * leg;
238
+ const crouch = (g.crouch ?? 0) * leg;
239
+ const strike = Math.min(12, toeOff * 0.3);
240
+ const rad = Math.PI / 180;
241
+ // Phases on the keys' time grid (a key's value is the one at its rounded time).
242
+ const on = (p) => rt(p * d) / d;
243
+ // Planted: the flat foot's ankle goes from +a to −a at the ground's speed.
244
+ const ground = (p) => a - (2 * a * p) / beta;
245
+ const keys = [{ p: 0, x: a, y: 0, rot: -strike }];
246
+ const flat = on(Math.min(0.2 * beta, 0.06)), roll = on(0.55 * beta);
247
+ keys.push({ p: flat, x: 0, y: 0, rot: 0, turn: true }, { p: roll, x: ground(roll), y: 0, rot: 0, planted: true });
248
+ for (const u of [1 / 3, 2 / 3, 1]) {
249
+ const p = on(roll + u * (beta - roll)), th = toeOff * u ** 1.5;
250
+ keys.push({ p, x: ground(p) + ball * (1 - Math.cos(th * rad)), y: -ball * Math.sin(th * rad), rot: th, planted: true });
251
+ }
252
+ // The swing, at fractions of the swing time.
253
+ const at = (s) => on(beta + s * (1 - beta));
254
+ const trail = { x: -a - (0.12 + 0.25 * (heel / leg)) * leg, y: -0.7 * heel };
255
+ const drive = { x: a * (1.2 * knee - 0.2), y: -lift };
256
+ keys.push({ p: at(0.14), x: trail.x, y: trail.y, rot: toeOff + 12 }, { p: at(0.43), x: trail.x + 0.3 * (drive.x - trail.x), y: -heel, rot: 20 + 60 * (heel / leg) }, { p: at(0.69), x: drive.x, y: drive.y, rot: -4 }, { p: at(0.86), x: a * (1 + paw), y: -0.55 * lift, rot: -strike }, { p: 1, x: a, y: 0, rot: -strike });
257
+ // Tracks of the near foot; the far foot's are the same shifted by half a cycle, so both need a
258
+ // key at the half (the far foot's seam): taken from the near foot's own spline.
259
+ const near = (f, pose) => keys.filter((k) => pose || !k.turn).map((k) => (!pose && k.planted ? [k.p, r(f(k)), "linear"] : [k.p, r(f(k))]));
260
+ const half = (ks) => {
261
+ const mid = on(0.5);
262
+ if (ks.some((k) => Math.abs(k[0] - mid) < 1e-9))
263
+ return ks;
264
+ const tr = makeTrack(ks.map((k) => ({ t: k[0], v: k[1], ease: k[2] })), { loop: { duration: 1 } });
265
+ return [...ks, [mid, r(sampleTrack(tr, mid))]].sort((x, y) => x[0] - y[0]);
266
+ };
267
+ const xs = half(near((k) => k.x, false)), ys = half(near((k) => k.y, true)), rots = half(near((k) => k.rot, true));
268
+ const timed = (ks, shift) => {
269
+ const out = ks.filter((k) => k[0] < 1 - 1e-9).map((k) => {
270
+ const p = wrap1(k[0] + shift);
271
+ return k.length > 2 ? [p, k[1], k[2]] : [p, k[1]];
272
+ });
273
+ out.sort((x, y) => x[0] - y[0]);
274
+ out.push([1, out[0][1]]);
275
+ return out.map((k) => [rt(k[0] * d), ...k.slice(1)]);
276
+ };
277
+ const t = {
278
+ "ik.footF.x": timed(xs, 0),
279
+ "ik.footF.y": timed(ys, 0),
280
+ "ik.footB.x": timed(xs, 0.5),
281
+ "ik.footB.y": timed(ys, 0.5),
282
+ "bones.footF.rotation": timed(rots, 0),
283
+ "bones.footB.rotation": timed(rots, 0.5),
284
+ // Lowest at mid-stance (the down), highest in mid-air.
285
+ "bones.hips.y": osc(d, crouch + g.bob, crouch - g.bob, 2, beta / 2),
286
+ // Leaning in a little more in the down; the head counter-turned to keep the eyes on the way ahead.
287
+ "bones.body.rotation": osc(d, g.lean + slump + 2, g.lean + slump - 1, 2, beta / 2),
288
+ "bones.head.rotation": osc(d, -0.85 * (g.lean + 2) - 0.35 * slump, -0.85 * (g.lean - 1) - 0.35 * slump, 2, beta / 2 + lag),
289
+ };
290
+ if (g.sway) {
291
+ t["bones.hips.spread"] = osc(d, g.sway, -g.sway, 1, beta / 2);
292
+ t["bones.body.spread"] = osc(d, -g.sway, g.sway, 1, beta / 2);
293
+ for (const leg of ["legF1", "legB1"])
294
+ t[`bones.${leg}.spread`] = osc(d, -g.sway, g.sway, 1, beta / 2);
295
+ }
296
+ if (arms)
297
+ Object.assign(t, gaitArms(g, beta));
298
+ // The body's travel a cycle: the planted foot's speed (its flat stretch, as keyed) × the cycle —
299
+ // 2a ÷ the planted fraction, to the keys' rounding.
300
+ const [k0, k1] = t["ik.footF.x"];
301
+ return { duration: d, loop: true, stride: ((k0[1] - k1[1]) / k1[0]) * d, tracks: t };
302
+ }
303
+ /** The part of the creep's cycle each foot is planted (over half: both feet down as the weight moves over). */
304
+ const CREEP_PLANT = 0.55;
305
+ /**
306
+ * The creep (`gait` with `creep`): a cartoon sneak, after the animators' key poses. Key poses of the
307
+ * near foot, as phases of the cycle (the far foot the same half a cycle later): contact at 0, ahead
308
+ * by `a`, toe first (turned 18° toe down, on the ball); rolled flat about the ball by 0.06 (the ball
309
+ * stays put: the ankle comes down and back as the heel does); flat to 0.46; rolled onto the ball to
310
+ * lift by `CREEP_PLANT` (`toeOff`, default 35°). The swing: the foot peeled up behind, toe down; the
311
+ * knee raised high (`knee`: the ankle `lift` + up to a quarter of the leg up) just ahead of the
312
+ * hips and held there (two keys close in value: an ease at the top of the step); the foot reaching
313
+ * out ahead, toe down, and set down. The hips (crouched) lowest as the weight comes over the front
314
+ * foot and highest as the knee comes up; the torso leaning in more at the low point, the head
315
+ * counter-turned to keep the eyes ahead. Flat-footed the planted ankle glides linearly (`linear`
316
+ * keys) at the body's speed — 2a ÷ (`CREEP_PLANT` × the cycle) — which `stride` is.
317
+ */
318
+ function creepGait(g, arms) {
319
+ const d = g.dur, a = g.a, beta = CREEP_PLANT;
320
+ const leg = g.leg ?? a * 7, knee = g.knee ?? 0, slump = g.slump ?? 0, lag = g.headLag ?? 0;
321
+ const ball = 0.7 * (g.foot ?? a * 0.9), toeOff = g.toeOff ?? 35, contact = 18;
322
+ const lift = g.lift + knee * 0.25 * leg, crouch = (g.crouch ?? 0) * leg;
323
+ const rad = Math.PI / 180;
324
+ const on = (p) => rt(p * d) / d;
325
+ const ground = (p) => a - (2 * a * p) / beta;
326
+ // The ankle of a foot turned `th` toe down about its ball, the ball on the flat foot's.
327
+ const tip = (x, th) => ({ x: x + ball * (1 - Math.cos(th * rad)), y: -ball * Math.sin(th * rad), rot: th });
328
+ const flat = on(0.06), lift0 = on(0.46);
329
+ const keys = [{ p: 0, ...tip(a, contact) }];
330
+ // Toe first, rolled flat (eased onto the heel; the ankle's spline through these keys, sub-pixel).
331
+ for (const u of [1 / 3, 2 / 3]) {
332
+ const p = on(u * flat);
333
+ keys.push({ p, ...tip(ground(p), contact * (1 - u) ** 1.5) });
334
+ }
335
+ keys.push({ p: flat, ...tip(ground(flat), 0) }, { p: lift0, ...tip(ground(lift0), 0), lin: true });
336
+ // (One key at half the cycle: the far foot's seam.)
337
+ for (const u of [(0.5 - lift0) / (beta - lift0), 0.75, 1]) {
338
+ const p = on(lift0 + u * (beta - lift0));
339
+ keys.push({ p, ...tip(ground(p), toeOff * u ** 1.5), lin: true });
340
+ }
341
+ // The swing, at fractions of the swing time.
342
+ const at = (s) => on(beta + s * (1 - beta));
343
+ const reach = tip(a * 1.08, contact + 6);
344
+ keys.push({ p: at(0.2), x: -a * 0.85, y: -0.4 * lift, rot: toeOff + 20 }, { p: at(0.45), x: a * 0.15, y: -lift, rot: 34 }, { p: at(0.65), x: a * 0.5, y: -0.96 * lift, rot: 32 }, { p: at(0.86), x: reach.x, y: reach.y - 0.28 * lift, rot: reach.rot }, { p: 1, ...tip(a, contact) });
345
+ // A foot's track, `shift` of the cycle on. The far foot's starts rolling onto its ball: that first
346
+ // stretch is eased (not `linear`), so each foot's first linear stretch is the flat one (its speed
347
+ // is the body's); the stretch into the seam stays linear.
348
+ const track = (f, shift, flags) => {
349
+ const out = keys.filter((k) => k.p < 1 - 1e-9).map((k) => [rt(wrap1(k.p + shift) * d), r(f(k)), flags && !!k.lin]);
350
+ out.sort((x, y) => x[0] - y[0]);
351
+ const end = [rt(d), out[0][1], out[0][2]];
352
+ out[0][2] = false;
353
+ for (let i = 1; i < out.length && out[i][2]; i++)
354
+ out[i][2] = false;
355
+ return [...out, end].map(([tt, v, lin]) => (lin ? [tt, v, "linear"] : [tt, v]));
356
+ };
357
+ const t = {
358
+ "ik.footF.x": track((k) => k.x, 0, true),
359
+ "ik.footF.y": track((k) => k.y, 0, false),
360
+ "ik.footB.x": track((k) => k.x, 0.5, true),
361
+ "ik.footB.y": track((k) => k.y, 0.5, false),
362
+ "bones.footF.rotation": track((k) => k.rot, 0, false),
363
+ "bones.footB.rotation": track((k) => k.rot, 0.5, false),
364
+ // Lowest as the weight comes over the front foot, highest as the knee comes up.
365
+ "bones.hips.y": osc(d, crouch + g.bob, crouch - g.bob, 2, 0.06),
366
+ "bones.body.rotation": osc(d, g.lean + slump + 2, g.lean + slump - 1, 2, 0.06),
367
+ "bones.head.rotation": osc(d, -0.8 * (g.lean + 2) - 0.35 * slump, -0.8 * (g.lean - 1) - 0.35 * slump, 2, 0.06 + lag),
368
+ };
369
+ if (arms)
370
+ Object.assign(t, gaitArms(g, beta));
371
+ // The body's travel a cycle: the flat foot's speed × the cycle.
372
+ const xs = t["ik.footF.x"], i = xs.findIndex((k) => k[2] === "linear");
373
+ return { duration: d, loop: true, stride: ((xs[i - 1][1] - xs[i][1]) / (xs[i][0] - xs[i - 1][0])) * d, tracks: t };
374
+ }
375
+ const wrapDeg = (a) => ((((a + 180) % 360) + 360) % 360) - 180;
376
+ /**
377
+ * The channel values (degrees) pointing an arm's upper bone along `u` and its lower bone along `f`:
378
+ * `[upper rotation, upper spread, lower rotation, lower turn]`. Directions are in the torso's frame,
379
+ * x away from the body's middle, y down, z forward; the bones hang along +y at rest. A 2.5D rig
380
+ * composes a bone as turn · spread · rotation (about the vertical, forward and sideways axes), so:
381
+ * the upper bone's rotation swings it forward or back (within ±90°: the one solution continuous
382
+ * through every pose but straight ahead or behind, so the keys' in-betweens never swing the arm
383
+ * through the front of the body) and its spread raises it out to the side; the lower bone's
384
+ * rotation bends the elbow by the angle between the bones and its turn, about the upper arm's own
385
+ * axis, carries the forearm out of the upper arm's swing plane (a hand brought in front of the
386
+ * chest). `far`: the arm on the +x side (the spread and turn mirrored). `flat`: a 2D rig, which has
387
+ * the rotations only — the arm as seen from the side (no spread or turn).
388
+ */
389
+ function armAim(u, f, far, flat) {
390
+ const D = 180 / Math.PI, m = far ? 1 : -1;
391
+ if (flat) {
392
+ const su = Math.atan2(-u[2], u[1]), sf = Math.atan2(-f[2], f[1]);
393
+ return [r(su * D), 0, r(wrapDeg((sf - su) * D)), 0];
394
+ }
395
+ // Engine axes: the near arm's outside is −x.
396
+ const [ux, uy, uz] = [m * u[0], u[1], u[2]], [fx, fy, fz] = [m * f[0], f[1], f[2]];
397
+ // Upper: (−sin s · cos r, cos s · cos r, −sin r) = u.
398
+ const r1 = Math.asin(Math.max(-1, Math.min(1, -uz)));
399
+ const c = Math.cos(r1), s1 = Math.abs(c) < 1e-9 ? 0 : Math.atan2(-ux / c, uy / c);
400
+ // Lower, in the upper bone's frame (Rᵀ f, R = rz(s1) · rx(r1)): (−sin r2 · sin t2, cos r2, −sin r2 · cos t2).
401
+ const cs = Math.cos(s1), ss = Math.sin(s1), cr = Math.cos(r1), sr = Math.sin(r1);
402
+ const a = cs * fx + ss * fy, b = -ss * fx + cs * fy;
403
+ const lx = a, ly = cr * b - sr * fz, lz = sr * b + cr * fz;
404
+ const r2 = -Math.acos(Math.max(-1, Math.min(1, ly)));
405
+ const t2 = Math.abs(Math.sin(r2)) < 1e-6 ? 0 : Math.atan2(lx, lz);
406
+ return [r(r1 * D), r(s1 * D), r(r2 * D), r(t2 * D)];
407
+ }
408
+ /**
409
+ * A direction in the torso's frame (see `armAim`) from its angle as seen from the side (degrees:
410
+ * 0 hanging, negative forward, −90 straight ahead) tilted `out` degrees away from the body's middle.
411
+ */
412
+ function sideDir(side, out) {
413
+ const a = (side * Math.PI) / 180, o = (out * Math.PI) / 180;
414
+ return [Math.sin(o), Math.cos(o) * Math.cos(a), -Math.cos(o) * Math.sin(a)];
415
+ }
416
+ /** The sneak's arms (degrees; see `gaitArms`): upper arm forward of hanging and out, elbow bend, forearm in, wrist bent. */
417
+ const SNEAK_ARM = { fwd: 40, out: 22, elbow: 100, in: -30, wrist: -70 };
418
+ /** Keys of the `run` and `reach` arms: eight poses a cycle, solved from the design (see `gaitArms`). */
419
+ const ARM_KEYS = 8;
420
+ /**
421
+ * The arms of a gait whose feet are each planted for `beta` of the cycle (0.5: a walk). The
422
+ * walk-made styles (`swing`, `pump`, `hang`, `up`) keep their quarter-cycle keys. `run` and `reach`
423
+ * are designed in 3D as the directions of the upper arm and the forearm through the cycle — smooth
424
+ * pendulums of the arm's phase — and keyed at `ARM_KEYS` phases of it, each solved to the rig's
425
+ * channels (`armAim`): the keys' in-betweens then stay on the designed path (four poses keyed per
426
+ * channel drift off it: a hand swinging in across the body between them). The near arm reaches its
427
+ * forward extreme in mid-air after the near foot pushed off (against its own leg), `armLag` late;
428
+ * the far arm half a cycle on and a little later, a little less (uneven, never mirrored).
429
+ */
430
+ function gaitArms(g, beta) {
431
+ const d = g.dur, q = d / 4, s = g.armSwing, slump = g.slump ?? 0;
432
+ const style = g.armStyle ?? "swing";
433
+ const t = {};
434
+ if (style === "swing") {
435
+ t["bones.armF1.rotation"] = [[0, s], [2 * q, -s], [d, s]];
436
+ t["bones.armF2.rotation"] = [[0, -6], [2 * q, -s * 0.8], [d, -6]];
437
+ t["bones.armB1.rotation"] = [[0, -s], [2 * q, s], [d, -s]];
438
+ t["bones.armB2.rotation"] = [[0, -s * 0.8], [2 * q, -6], [d, -s * 0.8]];
439
+ }
440
+ else if (style === "pump") {
441
+ // Elbows bent near a right angle, the arms driven forward and back (fists).
442
+ t["bones.armF1.rotation"] = [[0, r(s * 0.9)], [2 * q, r(-s * 1.1)], [d, r(s * 0.9)]];
443
+ t["bones.armF2.rotation"] = [[0, -75], [2 * q, -105], [d, -75]];
444
+ t["bones.armB1.rotation"] = [[0, r(-s * 1.1)], [2 * q, r(s * 0.9)], [d, r(-s * 1.1)]];
445
+ t["bones.armB2.rotation"] = [[0, -105], [2 * q, -75], [d, -105]];
446
+ }
447
+ else if (style === "hang") {
448
+ // Limp: hanging plumb whatever the torso does, a small swing dragged behind the legs.
449
+ const plumb = -(g.lean + slump) * 0.9;
450
+ t["bones.armF1.rotation"] = osc(d, r(plumb + s), r(plumb - s), 1, 0.12);
451
+ t["bones.armF2.rotation"] = osc(d, -3, -10, 1, 0.2);
452
+ t["bones.armB1.rotation"] = osc(d, r(plumb - s), r(plumb + s), 1, 0.12);
453
+ t["bones.armB2.rotation"] = osc(d, -10, -3, 1, 0.2);
454
+ }
455
+ else if (style === "up") {
456
+ // Up: both arms flailing overhead in turn (nearly straight, clear of the face).
457
+ const f = r(Math.min(28, s * 0.5));
458
+ t["bones.armF1.rotation"] = [[0, -168 + f], [2 * q, -168 - f], [d, -168 + f]];
459
+ t["bones.armF2.rotation"] = [[0, -12], [2 * q, 12], [d, -12]];
460
+ t["bones.armB1.rotation"] = [[0, 168 - f], [2 * q, 168 + f], [d, 168 - f]];
461
+ t["bones.armB2.rotation"] = [[0, 12], [2 * q, -12], [d, 12]];
462
+ }
463
+ else {
464
+ const lag = g.armLag ?? 0.05;
465
+ const pf = (beta + 0.5) / 2 + lag;
466
+ const cos = (w) => Math.cos(2 * Math.PI * w);
467
+ const sin = (w) => Math.sin(2 * Math.PI * w);
468
+ // One arm's pose at `w`, the phase of its own cycle (0: its forward extreme).
469
+ let pose;
470
+ let farShift;
471
+ if (style === "run") {
472
+ // A runner's arms. The upper arm swings s forward and 0.9 s back, its elbow out (more as it
473
+ // comes forward); the elbow near a right angle, closing as the arm comes forward (the hand
474
+ // goes round an oval); the forearm turned in at the front — the hand in front of the chest,
475
+ // short of its middle, the bent elbow showing from the front (not a straight arm bowed out)
476
+ // — and a little out at the back, the hand beside the hip (never over the crotch).
477
+ pose = (w, k) => {
478
+ const c = cos(w), side = -s * k * (0.05 + 0.95 * c), elbow = 83 + 8 * c - 11.5 * sin(w);
479
+ return [sideDir(side, 17 + 7 * c), sideDir(side - elbow, -12 - 20 * c)];
480
+ };
481
+ farShift = 0.5 + lag * 0.8;
482
+ }
483
+ else if (style === "sneak") {
484
+ // The sneak's hands, raised in front of the chest: each upper arm ~SNEAK_ARM.out° out to its
485
+ // own side (the elbows up and out) and forward of hanging in the picture (the torso's lean
486
+ // added back), the elbow bent sharply, the forearm rising forward and in to the hand at about
487
+ // chest height, ahead of the shoulder — short of the body's middle, below the chin — the
488
+ // wrist bent down (the "tiptoe fingers"). The hands creep up and down a little twice a cycle
489
+ // (a step's beat), the far one a quarter of a cycle (half a step) apart, a little lower.
490
+ const lean = g.lean + slump;
491
+ pose = (w, k, far) => {
492
+ const c2 = cos(2 * w), up = SNEAK_ARM.fwd - (far ? 4 : 0) + k * s * 0.5 * c2, elbow = SNEAK_ARM.elbow - k * 4 * c2;
493
+ return [sideDir(-lean - up, SNEAK_ARM.out), sideDir(-lean - up - elbow, SNEAK_ARM.in)];
494
+ };
495
+ farShift = 0.25 + lag * 0.8;
496
+ }
497
+ else {
498
+ // Panic, flung up and out ahead: each upper arm out to its own side ~46° from straight ahead
499
+ // and ~35° forward of hanging in the picture (the torso's lean added back), the elbow low and
500
+ // bent 56–76°, the forearm about level or rising to an open hand about shoulder height — below
501
+ // the chin, so from no side is a hand drawn over the head (a hand up beside the head would
502
+ // be, seen straight along the arm from some angle). From the front and the back both arms
503
+ // open out on their own sides, elbows bent (a W); from three-quarters the near hand is out
504
+ // beside the shoulder, outside the chest, the far one out ahead; from the side both reach
505
+ // ahead, hands up. A forearm never lies across the chest, the crotch or the face. They
506
+ // flutter up and down twice a cycle (a step's beat), the elbow opening a little late as the
507
+ // arm rises, and pump a little with the legs once a cycle; the far arm a quarter of a cycle
508
+ // (half a step) apart, lower and a little less.
509
+ const lean = g.lean + slump;
510
+ pose = (w, k, far) => {
511
+ const c1 = cos(w), f2 = cos(2 * w), up = 34 - (far ? 5 : 0) + k * (8 * f2 + 4 * c1), elbow = 66 - 10 * cos(2 * (w - 0.04));
512
+ // (A torso leaning in carries its hanging arm back: the picture's angle plus the lean.)
513
+ return [sideDir(-lean - up, 46 + 4 * c1), sideDir(-lean - up - elbow, 52 + 4 * c1)];
514
+ };
515
+ farShift = 0.25 + lag * 0.8;
516
+ }
517
+ const keysOf = (sh, k, far) => {
518
+ const aim = Array.from({ length: ARM_KEYS }, (_, i) => armAim(...pose(i / ARM_KEYS, k, far), far, !!g.flat));
519
+ return [0, 1, 2, 3].map((j) => loopKeys(d, aim.map((v, i) => [sh + i / ARM_KEYS, v[j]])));
520
+ };
521
+ for (const [side, keys] of [["F", keysOf(pf, 1, false)], ["B", keysOf(pf + farShift, 0.9, true)]]) {
522
+ t[`bones.arm${side}1.rotation`] = keys[0];
523
+ t[`bones.arm${side}2.rotation`] = keys[2];
524
+ if (g.flat)
525
+ continue;
526
+ t[`bones.arm${side}1.spread`] = keys[1];
527
+ t[`bones.arm${side}2.turn`] = keys[3];
528
+ }
529
+ // The sneak's wrists bent down (its fingers hanging from the raised hands).
530
+ if (style === "sneak")
531
+ for (const side of ["F", "B"])
532
+ t[`bones.hand${side}.rotation`] = osc(d, SNEAK_ARM.wrist, SNEAK_ARM.wrist - 8, 2, pf);
533
+ }
534
+ return t;
535
+ }
536
+ /**
537
+ * A run cut to a skirt. A skirt is one cloth spanning the legs (at the thighs, the knees and, long,
538
+ * the hem): a run's knee drive, heel kick and long flight would stretch it into a sack between them.
539
+ * To the knees, the shins are free (the heel still kicks up behind) but the thighs part less: half
540
+ * the knee drive, shorter steps, a little less flight. To the ankles, a shuffle: short steps under
541
+ * the body, the shins near plumb (a small heel kick, no knee drive), a short hop of flight, a smaller
542
+ * bob. Not a run (no `flight`, `heel` or `paw`): unchanged.
543
+ */
544
+ export function skirtRun(g, skirt) {
545
+ if (!skirt || (g.flight === undefined && g.heel === undefined && g.paw === undefined))
546
+ return g;
547
+ const heel = g.heel ?? 0.4, knee = g.knee ?? 0, flight = g.flight ?? 0;
548
+ if (skirt === "short")
549
+ return { ...g, a: r(g.a * 0.85), flight: r(flight * 0.75), heel: Math.min(heel, 0.5), knee: r(knee * 0.5), crouch: Math.min(g.crouch ?? 0, 0.1) };
550
+ return {
551
+ ...g, a: r(g.a * 0.55), lift: r(g.lift * 0.5), bob: r(g.bob * 0.6), flight: Math.min(flight, 0.14), heel: Math.min(heel, 0.16), knee: 0,
552
+ paw: Math.min(g.paw ?? 0.15, 0.08), crouch: Math.min(g.crouch ?? 0, 0.05),
553
+ };
554
+ }
555
+ /**
556
+ * Personality walks built from a character's walk and run (looping, with their own `stride`, so the
557
+ * director's stride matching keeps the feet planted): `sneak` (a classic cartoon creep, `creep`:
558
+ * crouched and leaning in, big slow steps set down toe first and rolled flat, the knee held high at
559
+ * the top of each step, the hands raised in front of the chest with the wrists bent: `sneak` arms), `strut` (chest out, hips swaying, bouncing twice a step, fists pumping), `tired` (slumped,
560
+ * feet dragging, arms hanging), `march` (knees high, upright, fists pumping) and `scaredRun` (a panic
561
+ * sprint: its own run cycle, not the run's — leaning hard in, crouched deep, the heels kicked high
562
+ * behind and the knees pumping, long flights at a faster cadence (never under 0.3 s a cycle:
563
+ * nine frames at 30 fps), the arms flung up and out ahead with bent elbows and open hands, fluttering out
564
+ * of step: `reach`). With `skirt`, cut to it (`skirtRun`).
565
+ */
566
+ export function personalityGaits(walk, run, o = {}) {
567
+ const leg = o.leg ?? walk.a * 7, foot = o.foot ?? walk.a * 0.9;
568
+ const w = (k, dur, extra) => ({ ...walk, a: r(walk.a * k), dur, leg, foot, ...extra });
569
+ return {
570
+ // (In a long skirt — one cloth to the ankles — shorter steps, half the knee lift, a shallower crouch.)
571
+ sneak: w(o.skirt === "long" ? 1.2 : 1.9, 2, {
572
+ lift: r(walk.lift * 0.8), bob: r(walk.bob * 0.6), lean: walk.lean + 26, armSwing: r(walk.armSwing * 0.25), crouch: o.skirt === "long" ? 0.1 : 0.18, knee: o.skirt === "long" ? 0.5 : 1,
573
+ creep: true, armStyle: "sneak", headLag: 0.08, ...(run.flat ? { flat: true } : {}),
574
+ }),
575
+ strut: w(1.1, 0.6, { bob: r(walk.bob * 0.8), armSwing: r(walk.armSwing * 1.2), slump: -4, sway: 6, armStyle: "pump", doubleBounce: true, headLag: 0.12 }),
576
+ tired: w(0.8, 0.95, { bob: 1, armSwing: r(walk.armSwing * 0.4), slump: 12, drag: 0.7, armStyle: "hang", headLag: 0.2 }),
577
+ march: w(1, 0.5, { lean: 0, armSwing: r(walk.armSwing * 1.6), knee: 1, sway: 0, armStyle: "pump" }),
578
+ scaredRun: skirtRun({
579
+ ...run, a: r(run.a * 1.3), dur: Math.max(0.3, r(run.dur * 0.68)), bob: r(run.bob * 1.5), lean: 24, crouch: 0.12, leg, foot,
580
+ flight: 0.44, heel: 0.62, knee: 1, paw: 0.2, toeOff: 40, armStyle: "reach", headLag: 0.1,
581
+ }, o.skirt),
582
+ };
583
+ }
584
+ /**
585
+ * Hand shapes drawn in setup space around the wrist position `at`: `open`, `fist`, `point` (index
586
+ * finger out) and `grip` (curled around something), as switch variants for a `handF` / `handB`
587
+ * part. The fingers follow the forearm (`dir`, degrees in setup space: 90 = down, a hanging arm),
588
+ * so a pointing finger points wherever the arm points. `r` is the palm radius, `fill` its colour.
589
+ */
590
+ export function handShapes(at, o) {
591
+ const [x, y] = at, R = o.r, ink = o.ink ?? "palette(ink)", w = o.stroke ?? 4;
592
+ const st = `stroke="${ink}" stroke-width="${w}" stroke-linejoin="round" stroke-linecap="round"`;
593
+ const palm = `<circle cx="${x}" cy="${y}" r="${R}" fill="${o.fill}" ${st}/>`;
594
+ const finger = (dx, dy, len, ang) => {
595
+ const a = (ang * Math.PI) / 180;
596
+ return `<path d="M${r(x + dx)} ${r(y + dy)} l${r(Math.cos(a) * len)} ${r(Math.sin(a) * len)}" stroke="${ink}" stroke-width="${r(R * 0.62 + w)}" stroke-linecap="round"/>` +
597
+ `<path d="M${r(x + dx)} ${r(y + dy)} l${r(Math.cos(a) * len)} ${r(Math.sin(a) * len)}" stroke="${o.fill}" stroke-width="${r(R * 0.62 - w * 0.4)}" stroke-linecap="round"/>`;
598
+ };
599
+ const knuckles = `<path d="M${r(x + R * 0.2)} ${r(y - R * 0.55)} v${r(R * 1.1)} M${r(x + R * 0.55)} ${r(y - R * 0.45)} v${r(R * 0.9)}" fill="none" stroke="${ink}" stroke-width="${r(w * 0.6)}"/>`;
600
+ const turn = (art) => `<g transform="rotate(${o.dir ?? 90} ${x} ${y})">${art}</g>`;
601
+ return {
602
+ open: turn(finger(R * 0.3, -R * 0.5, R * 1.2, -25) + finger(R * 0.5, 0, R * 1.3, 0) + finger(R * 0.3, R * 0.5, R * 1.1, 25) + finger(-R * 0.3, -R * 0.6, R * 0.9, -80) + palm),
603
+ fist: turn(`<rect x="${r(x - R * 0.9)}" y="${r(y - R * 0.95)}" width="${r(R * 1.9)}" height="${r(R * 1.9)}" rx="${r(R * 0.7)}" fill="${o.fill}" ${st}/>` + knuckles),
604
+ point: turn(finger(R * 0.4, -R * 0.35, R * 1.5, -8) + `<rect x="${r(x - R * 0.9)}" y="${r(y - R * 0.8)}" width="${r(R * 1.8)}" height="${r(R * 1.7)}" rx="${r(R * 0.7)}" fill="${o.fill}" ${st}/>`),
605
+ grip: turn(palm + `<path d="M${r(x + R * 0.2)} ${r(y - R)} q${r(R * 1.2)} ${r(R * 0.2)} ${r(R * 0.6)} ${r(R * 1.6)}" fill="none" ${st}/>`),
606
+ };
607
+ }
608
+ export function characterClips(o) {
609
+ const e = o.energy ?? 1;
610
+ const k = (v) => r(v * e);
611
+ const B = !o.busyFarHand; // far arm free?
612
+ const fl = (v) => r(v * (o.forearmLift ?? 1));
613
+ const farArm = (t) => (B ? t : {});
614
+ // Hand shapes over a clip (only for characters with switch hands).
615
+ const hand = (v, dur, both = false) => o.hands ? { "parts.handF.variant": [[0, v], [dur, v]], ...(both && B ? { "parts.handB.variant": [[0, v], [dur, v]] } : {}) } : {};
616
+ const clips = {
617
+ // A quick turn (profile ↔ front/back): squeezed for a few frames around the switch, hiding the
618
+ // change of drawing (played centred on the moment the view changes).
619
+ turn: {
620
+ duration: 0.24,
621
+ tracks: { "bones.root.scaleX": [[0, 1], [0.1, 0.62, "easeIn"], [0.14, 0.62], [0.24, 1, "easeOut"]], "bones.root.scaleY": [[0, 1], [0.12, 1.04], [0.24, 1]] },
622
+ },
623
+ idle: {
624
+ duration: 3.2,
625
+ clear: true,
626
+ loop: true,
627
+ tracks: {
628
+ "bones.body.rotation": [[0, 0], [1.6, 1.2], [3.2, 0]],
629
+ "bones.head.rotation": [[0, 0], [1.1, -2.5], [2.2, 1.5], [3.2, 0]],
630
+ "bones.armF1.rotation": [[0, 0], [1.6, -3], [3.2, 0]],
631
+ ...farArm({ "bones.armB1.rotation": [[0, 0], [1.6, 2.5], [3.2, 0]] }),
632
+ },
633
+ },
634
+ // Locomotion is `clear`: its arms are kept off the face in the picture (2.5D rigs) like a pose's.
635
+ walk: { ...gait(o.walk), clear: true },
636
+ // The run: loose fists; the leg and foot lengths for its crouch, heel kick and push-off.
637
+ run: (() => {
638
+ const c = gait(skirtRun({ leg: o.leg, foot: o.foot, ...o.run }, o.skirt));
639
+ return { ...c, clear: true, tracks: { ...c.tracks, ...hand("fist", c.duration, true) } };
640
+ })(),
641
+ // Personality walks (sneak, strut, tired, march, scaredRun) with their hands.
642
+ ...Object.fromEntries(Object.entries(personalityGaits(o.walk, o.run, o)).map(([name, g]) => {
643
+ const c = gait(g);
644
+ if (!B)
645
+ for (const k of Object.keys(c.tracks))
646
+ if (/^bones\.(armB|handB)/.test(k))
647
+ delete c.tracks[k];
648
+ const shape = name === "strut" || name === "march" ? "fist" : "open";
649
+ return [name, { ...c, clear: true, tracks: { ...c.tracks, ...hand(shape, c.duration, true) } }];
650
+ })),
651
+ // Talking gesture: the near hand opens out on stressed beats, head nods.
652
+ talk: {
653
+ duration: 1.6,
654
+ clear: true,
655
+ loop: true,
656
+ tracks: {
657
+ "bones.head.rotation": [[0, 0], [0.3, -4], [0.6, 2], [0.95, -3], [1.3, 1], [1.6, 0]],
658
+ "bones.body.rotation": [[0, 0], [0.8, -1.5], [1.6, 0]],
659
+ "bones.armF1.rotation": [[0, k(-18)], [0.4, k(-38)], [0.8, k(-24)], [1.2, k(-42)], [1.6, k(-18)]],
660
+ "bones.armF2.rotation": [[0, fl(-30)], [0.4, fl(-55)], [0.8, fl(-35)], [1.2, fl(-60)], [1.6, fl(-30)]],
661
+ },
662
+ },
663
+ wave: {
664
+ duration: 1.2,
665
+ clear: true,
666
+ loop: true,
667
+ tracks: {
668
+ ...hand("open", 1.2),
669
+ "bones.armF1.rotation": [[0, k(-92)], [0.6, k(-98)], [1.2, k(-92)]],
670
+ "bones.armF2.rotation": [[0, fl(-18)], [0.3, fl(-58)], [0.6, fl(-14)], [0.9, fl(-58)], [1.2, fl(-18)]],
671
+ "bones.armF1.squash": [[0, 0.2], [1.2, 0.2]],
672
+ "bones.armF2.squash": [[0, 0.25], [1.2, 0.25]],
673
+ "bones.head.rotation": [[0, -3], [0.6, -6], [1.2, -3]],
674
+ "bones.body.rotation": [[0, -1], [0.6, -3], [1.2, -1]],
675
+ },
676
+ },
677
+ point: {
678
+ duration: 1,
679
+ clear: true,
680
+ loop: true,
681
+ tracks: {
682
+ ...hand("point", 1),
683
+ "bones.armF1.rotation": [[0, -88], [0.5, -92], [1, -88]],
684
+ "bones.armF2.rotation": [[0, -4], [1, -4]],
685
+ "bones.body.rotation": [[0, 3], [1, 3]],
686
+ "bones.head.rotation": [[0, -2], [1, -2]],
687
+ },
688
+ },
689
+ present: {
690
+ duration: 1.4,
691
+ clear: true,
692
+ loop: true,
693
+ tracks: {
694
+ ...hand("open", 1.4),
695
+ "bones.armF1.rotation": [[0, -70], [0.7, -78], [1.4, -70]],
696
+ "bones.armF2.rotation": [[0, -40], [0.7, -48], [1.4, -40]],
697
+ "bones.head.rotation": [[0, -4], [1.4, -4]],
698
+ },
699
+ },
700
+ clap: {
701
+ duration: 0.5,
702
+ clear: true,
703
+ loop: true,
704
+ tracks: {
705
+ ...hand("open", 0.5, true),
706
+ "bones.armF1.rotation": [[0, -55], [0.25, -62], [0.5, -55]],
707
+ "bones.armF2.rotation": [[0, -70], [0.25, -40, "easeIn"], [0.5, -70]],
708
+ ...farArm({
709
+ "bones.armB1.rotation": [[0, -70], [0.25, -66], [0.5, -70]],
710
+ "bones.armB2.rotation": [[0, -10], [0.25, -48, "easeIn"], [0.5, -10]],
711
+ }),
712
+ "bones.head.rotation": [[0, -4], [0.25, -6], [0.5, -4]],
713
+ "bones.hips.y": [[0, 0], [0.25, 2], [0.5, 0]],
714
+ },
715
+ },
716
+ cheer: {
717
+ duration: 0.8,
718
+ clear: true,
719
+ loop: true,
720
+ tracks: {
721
+ ...hand("fist", 0.8, true),
722
+ "bones.armF1.rotation": [[0, k(-155)], [0.4, k(-165)], [0.8, k(-155)]],
723
+ "bones.armF2.rotation": [[0, -10], [0.4, 10], [0.8, -10]],
724
+ ...farArm({ "bones.armB1.rotation": [[0, k(155)], [0.4, k(165)], [0.8, k(155)]], "bones.armB2.rotation": [[0, 10], [0.4, -10], [0.8, 10]] }),
725
+ "bones.root.y": [[0, 0], [0.12, 0], [0.4, -o.jump * 0.5, "sineOut"], [0.68, 0, "sineIn"], [0.8, 0]],
726
+ "bones.head.rotation": [[0, -8], [0.8, -8]],
727
+ },
728
+ },
729
+ dance: {
730
+ duration: 1.2,
731
+ clear: true,
732
+ loop: true,
733
+ tracks: {
734
+ "bones.body.rotation": [[0, -6], [0.6, 6], [1.2, -6]],
735
+ "bones.head.rotation": [[0, 6], [0.6, -6], [1.2, 6]],
736
+ "bones.hips.y": [[0, 0], [0.3, 5], [0.6, 0], [0.9, 5], [1.2, 0]],
737
+ "bones.armF1.rotation": [[0, k(-120)], [0.6, k(-40)], [1.2, k(-120)]],
738
+ "bones.armF2.rotation": [[0, -30], [0.6, -60], [1.2, -30]],
739
+ ...farArm({ "bones.armB1.rotation": [[0, k(40)], [0.6, k(120)], [1.2, k(40)]], "bones.armB2.rotation": [[0, -60], [0.6, -30], [1.2, -60]] }),
740
+ "ik.footF.y": [[0, 0], [0.3, -10], [0.6, 0], [1.2, 0]],
741
+ "ik.footB.y": [[0, 0], [0.6, 0], [0.9, -10], [1.2, 0]],
742
+ },
743
+ },
744
+ sing: {
745
+ duration: 2,
746
+ clear: true,
747
+ loop: true,
748
+ tracks: {
749
+ "bones.body.rotation": [[0, -3], [1, 3], [2, -3]],
750
+ "bones.head.rotation": [[0, -6], [1, -2], [2, -6]],
751
+ "bones.armF1.rotation": [[0, k(-60)], [1, k(-80)], [2, k(-60)]],
752
+ "bones.armF2.rotation": [[0, -50], [1, -30], [2, -50]],
753
+ },
754
+ },
755
+ // Drinking: the near hand (a glass, a bottle) up to the mouth, the head tips back, a long sip.
756
+ drink: {
757
+ duration: 2.2,
758
+ tracks: {
759
+ "bones.armF1.rotation": [[0, 0], [0.4, -62, "easeOut"], [1.7, -66], [2.2, 0]],
760
+ "bones.armF2.rotation": [[0, 0], [0.4, -112, "easeOut"], [1.7, -118], [2.2, 0]],
761
+ // The wrist tips what is held (a bottle, a glass) towards the mouth.
762
+ "bones.handF.rotation": [[0, 0], [0.4, 50, "easeOut"], [1.7, 48], [2.2, 0]],
763
+ "bones.head.rotation": [[0, 0], [0.45, -16], [1.7, -20], [2.2, 0]],
764
+ "bones.body.rotation": [[0, 0], [0.45, -5], [1.7, -6], [2.2, 0]],
765
+ },
766
+ },
767
+ // Smoking: the near hand (a cigarette) to the mouth for a puff and back.
768
+ smoke: {
769
+ duration: 1.8,
770
+ tracks: {
771
+ "bones.armF1.rotation": [[0, 0], [0.35, -48, "easeOut"], [1.1, -50], [1.5, -8], [1.8, 0]],
772
+ "bones.armF2.rotation": [[0, 0], [0.35, -128, "easeOut"], [1.1, -130], [1.5, -30], [1.8, 0]],
773
+ "bones.head.rotation": [[0, 0], [0.4, -4], [1.1, -6], [1.4, -10], [1.8, 0]],
774
+ },
775
+ },
776
+ // On the phone: the near hand holds it at the ear (looped while talking).
777
+ phone: {
778
+ duration: 2.4,
779
+ loop: true,
780
+ tracks: {
781
+ // The elbow up and forward, the hand back at the ear.
782
+ "bones.armF1.rotation": [[0, -42], [1.2, -44], [2.4, -42]],
783
+ "bones.armF2.rotation": [[0, -150], [1.2, -148], [2.4, -150]],
784
+ // The wrist turns the phone along the jaw: the top at the ear, the bottom by the mouth.
785
+ "bones.handF.rotation": [[0, 165], [2.4, 165]],
786
+ "bones.head.rotation": [[0, 6], [1.2, 8], [2.4, 6]],
787
+ },
788
+ },
789
+ think: {
790
+ duration: 2.4,
791
+ loop: true,
792
+ tracks: {
793
+ "bones.armF1.rotation": [[0, -30], [2.4, -30]],
794
+ "bones.armF2.rotation": [[0, -128], [1.2, -122], [2.4, -128]],
795
+ "bones.head.rotation": [[0, -8], [1.2, -11], [2.4, -8]],
796
+ "bones.body.rotation": [[0, -2], [2.4, -2]],
797
+ },
798
+ },
799
+ shrug: {
800
+ duration: 1.2,
801
+ clear: true,
802
+ tracks: {
803
+ ...hand("open", 1.2, true),
804
+ "bones.armF1.rotation": [[0, 0], [0.3, -40, "backOut"], [0.9, -40], [1.2, 0]],
805
+ "bones.armF2.rotation": [[0, 0], [0.3, -80, "backOut"], [0.9, -80], [1.2, 0]],
806
+ ...farArm({ "bones.armB1.rotation": [[0, 0], [0.3, 30, "backOut"], [0.9, 30], [1.2, 0]], "bones.armB2.rotation": [[0, 0], [0.3, -70, "backOut"], [0.9, -70], [1.2, 0]] }),
807
+ "bones.head.rotation": [[0, 0], [0.3, 10], [0.9, 10], [1.2, 0]],
808
+ "bones.hips.y": [[0, 0], [0.3, -3], [0.9, -3], [1.2, 0]],
809
+ },
810
+ },
811
+ laugh: {
812
+ duration: 0.6,
813
+ clear: true,
814
+ loop: true,
815
+ tracks: {
816
+ "bones.head.rotation": [[0, -12], [0.15, -6], [0.3, -13], [0.45, -6], [0.6, -12]],
817
+ "bones.body.rotation": [[0, -3], [0.3, -1], [0.6, -3]],
818
+ "bones.hips.y": [[0, 0], [0.15, 2], [0.3, 0], [0.45, 2], [0.6, 0]],
819
+ "bones.armF1.rotation": [[0, -30], [0.6, -30]],
820
+ "bones.armF2.rotation": [[0, -90], [0.3, -95], [0.6, -90]],
821
+ },
822
+ },
823
+ cry: {
824
+ duration: 0.8,
825
+ loop: true,
826
+ tracks: {
827
+ "bones.head.rotation": [[0, 14], [0.2, 11], [0.4, 14], [0.6, 11], [0.8, 14]],
828
+ "bones.body.rotation": [[0, 4], [0.8, 4]],
829
+ "bones.armF1.rotation": [[0, -40], [0.8, -40]],
830
+ "bones.armF2.rotation": [[0, -110], [0.4, -115], [0.8, -110]],
831
+ "bones.hips.y": [[0, 1], [0.2, 3], [0.4, 1], [0.6, 3], [0.8, 1]],
832
+ },
833
+ },
834
+ scared: {
835
+ duration: 0.24,
836
+ clear: true,
837
+ loop: true,
838
+ tracks: {
839
+ ...hand("open", 0.24, true),
840
+ "bones.body.rotation": [[0, -3], [0.06, -5], [0.12, -3], [0.18, -5], [0.24, -3]],
841
+ "bones.root.x": [[0, -1.5], [0.06, 1.5], [0.12, -1.5], [0.18, 1.5], [0.24, -1.5]],
842
+ "bones.armF1.rotation": [[0, -60], [0.24, -60]],
843
+ "bones.armF2.rotation": [[0, -90], [0.24, -90]],
844
+ "bones.hips.y": [[0, 3], [0.24, 3]],
845
+ },
846
+ },
847
+ jump: {
848
+ duration: 0.8,
849
+ clear: true,
850
+ tracks: {
851
+ "bones.root.y": [[0, 0], [0.14, 0], [0.4, -o.jump, "sineOut"], [0.66, 0, "sineIn"], [0.8, 0]],
852
+ "ik.footF.y": [[0, 0], [0.14, 0], [0.4, -12], [0.66, 0], [0.8, 0]],
853
+ "ik.footB.y": [[0, 0], [0.14, 0], [0.4, -16], [0.66, 0], [0.8, 0]],
854
+ "bones.hips.y": [[0, 0], [0.12, 8, "easeOut"], [0.24, -3], [0.62, 0], [0.7, 7, "easeOut"], [0.8, 0, "backOut"]],
855
+ "bones.body.squash": [[0, 0], [0.12, -0.08, "easeOut"], [0.26, 0.08], [0.55, 0], [0.7, -0.07, "easeOut"], [0.8, 0, "backOut"]],
856
+ "bones.armF1.rotation": [[0, 0], [0.12, 20], [0.34, k(-150)], [0.66, k(-60)], [0.8, 0]],
857
+ ...farArm({ "bones.armB1.rotation": [[0, 0], [0.12, -20], [0.34, k(150)], [0.66, k(60)], [0.8, 0]] }),
858
+ },
859
+ },
860
+ // Bouncing a ball with the near hand (the scene animates the ball in sync, period 0.6 s).
861
+ dribble: {
862
+ duration: 0.6,
863
+ clear: true,
864
+ loop: true,
865
+ tracks: {
866
+ // The hand meets the ball at the top (t = 0), rides it down, lets go, rises and waits.
867
+ "bones.armF1.rotation": [[0, -62], [0.12, -40, "easeOut"], [0.45, -64], [0.6, -62]],
868
+ "bones.armF2.rotation": [[0, -40], [0.12, -22, "easeOut"], [0.45, -42], [0.6, -40]],
869
+ "bones.body.rotation": [[0, 3], [0.3, 5], [0.6, 3]],
870
+ "bones.head.rotation": [[0, 6], [0.3, 9], [0.6, 6]],
871
+ "bones.hips.y": [[0, 0], [0.3, 2], [0.6, 0]],
872
+ },
873
+ },
874
+ // Throwing something up high with both hands.
875
+ toss: {
876
+ duration: 0.9,
877
+ clear: true,
878
+ tracks: {
879
+ "bones.hips.y": [[0, 0], [0.2, 6, "easeOut"], [0.4, -4, "backOut"], [0.9, 0]],
880
+ "bones.armF1.rotation": [[0, -30], [0.2, -10], [0.4, -160, "backOut"], [0.9, -40]],
881
+ ...farArm({ "bones.armB1.rotation": [[0, 20], [0.2, 10], [0.4, 160, "backOut"], [0.9, 40]] }),
882
+ "bones.head.rotation": [[0, 0], [0.4, -14], [0.9, -10]],
883
+ },
884
+ },
885
+ // Stopping right at the kerb: leaning forward, arms windmilling.
886
+ teeter: {
887
+ duration: 1.1,
888
+ clear: true,
889
+ tracks: {
890
+ "bones.body.rotation": [[0, 0], [0.15, 12, "easeOut"], [0.55, 8], [1.1, 0, "backOut"]],
891
+ "bones.armF1.rotation": [[0, 0], [0.15, -150], [0.35, -60], [0.55, -150], [0.75, -60], [1.1, 0]],
892
+ ...farArm({ "bones.armB1.rotation": [[0, 0], [0.15, 150], [0.35, 60], [0.55, 150], [0.75, 60], [1.1, 0]] }),
893
+ "bones.hips.y": [[0, 0], [0.15, 3], [1.1, 0]],
894
+ },
895
+ },
896
+ // Pointing up and forward (at a traffic light across the road).
897
+ pointUp: {
898
+ duration: 1.2,
899
+ clear: true,
900
+ loop: true,
901
+ tracks: {
902
+ ...hand("point", 1.2),
903
+ "bones.armF1.rotation": [[0, -104], [0.6, -108], [1.2, -104]],
904
+ "bones.armF2.rotation": [[0, -20], [1.2, -20]],
905
+ "bones.head.rotation": [[0, -10], [1.2, -10]],
906
+ },
907
+ },
908
+ // Flapping both arms (wings) fast: arriving in a hurry, excitement, a bird taking off.
909
+ flap: {
910
+ duration: 0.28,
911
+ clear: true,
912
+ loop: true,
913
+ tracks: {
914
+ "bones.armF1.rotation": [[0, k(-40)], [0.14, k(-120), "easeOut"], [0.28, k(-40), "easeIn"]],
915
+ "bones.armF2.rotation": [[0, -10], [0.14, 20], [0.28, -10]],
916
+ ...farArm({ "bones.armB1.rotation": [[0, k(40)], [0.14, k(120), "easeOut"], [0.28, k(40), "easeIn"]], "bones.armB2.rotation": [[0, 10], [0.14, -20], [0.28, 10]] }),
917
+ "bones.hips.y": [[0, 0], [0.14, -3], [0.28, 0]],
918
+ "bones.head.rotation": [[0, -4], [0.28, -4]],
919
+ },
920
+ },
921
+ // Fighting stance: knees bent, fists up.
922
+ stance: {
923
+ duration: 1.6,
924
+ clear: true,
925
+ loop: true,
926
+ tracks: {
927
+ ...hand("fist", 1.6, true),
928
+ "bones.hips.y": [[0, 6], [0.8, 9], [1.6, 6]],
929
+ "bones.body.rotation": [[0, 6], [0.8, 8], [1.6, 6]],
930
+ "bones.armF1.rotation": [[0, -70], [0.8, -66], [1.6, -70]],
931
+ "bones.armF2.rotation": [[0, -110], [1.6, -110]],
932
+ ...farArm({ "bones.armB1.rotation": [[0, -40], [1.6, -40]], "bones.armB2.rotation": [[0, -120], [1.6, -120]] }),
933
+ },
934
+ },
935
+ // Powering up, eyes closed: arms down and back, fists clenched, chin down.
936
+ power: {
937
+ duration: 1.2,
938
+ clear: true,
939
+ loop: true,
940
+ tracks: {
941
+ ...hand("fist", 1.2, true),
942
+ "bones.hips.y": [[0, 5], [0.6, 7], [1.2, 5]],
943
+ "bones.head.rotation": [[0, 8], [1.2, 8]],
944
+ "bones.armF1.rotation": [[0, 22], [0.6, 24], [1.2, 22]],
945
+ "bones.armF2.rotation": [[0, -30], [1.2, -30]],
946
+ ...farArm({ "bones.armB1.rotation": [[0, 26], [1.2, 26]], "bones.armB2.rotation": [[0, -30], [1.2, -30]] }),
947
+ },
948
+ },
949
+ // A straight punch: wind-up, thrust (fast), hold, recover — the fist lands at 0.28 s.
950
+ punch: {
951
+ duration: 0.9,
952
+ clear: true,
953
+ tracks: {
954
+ ...hand("fist", 0.9, true),
955
+ "bones.body.rotation": [[0, 0], [0.18, -10, "easeOut"], [0.28, 16, "easeIn"], [0.6, 14], [0.9, 0]],
956
+ "bones.hips.y": [[0, 0], [0.18, 6], [0.28, 3], [0.9, 0]],
957
+ "bones.armF1.rotation": [[0, 0], [0.18, 50, "easeOut"], [0.28, -84, "easeIn"], [0.6, -82], [0.9, 0]],
958
+ "bones.armF2.rotation": [[0, 0], [0.18, -120, "easeOut"], [0.28, -4, "easeIn"], [0.6, -4], [0.9, 0]],
959
+ ...farArm({ "bones.armB1.rotation": [[0, 0], [0.28, 40], [0.9, 0]], "bones.armB2.rotation": [[0, 0], [0.28, -100], [0.9, 0]] }),
960
+ },
961
+ },
962
+ // Knocked down: `knocked` in the air, `down` lying (the scene's `fall` action plays them).
963
+ ...knockDownClips(o),
964
+ // Riding (a bicycle…): hands and feet are held on the vehicle by the scene (`reach`); the
965
+ // torso leans into the ride, the head stays up, a small bob with each pedal stroke.
966
+ ride: {
967
+ duration: 1,
968
+ loop: true,
969
+ tracks: {
970
+ "bones.body.rotation": [[0, 15], [0.5, 17], [1, 15]],
971
+ "bones.head.rotation": [[0, -14], [0.5, -15], [1, -14]],
972
+ "bones.hips.y": [[0, 0], [0.25, 1.5], [0.5, 0], [0.75, 1.5], [1, 0]],
973
+ },
974
+ },
975
+ // Hand in hand (the IK target is set by the scene); a gentle sway.
976
+ hold: {
977
+ duration: 2,
978
+ loop: true,
979
+ tracks: {
980
+ ...hand("grip", 2),
981
+ "bones.body.rotation": [[0, 0], [1, 1.5], [2, 0]],
982
+ "bones.head.rotation": [[0, -3], [1, -5], [2, -3]],
983
+ },
984
+ },
985
+ };
986
+ return clips;
987
+ }
988
+ /**
989
+ * The knock-down clips, timed for the scene's `fall` action (which moves and turns the whole body —
990
+ * thrown back, the hips landing first, the shoulders slapping down — these only pose the limbs):
991
+ *
992
+ * - `knocked` (0.85 s, from the hit to the hips touching down; the fall plays it at the speed that
993
+ * lands it with the body, its end pose held): no anticipation — a hit is a surprise. The head
994
+ * whips back first, the chest a few frames later (whiplash), then the body folds at the hips
995
+ * (a jackknife: thighs forward and up, knees bent) as it flies; the arms lag behind the torso
996
+ * snapping back, so they fling forward and up with soft elbows, opening out (`turn`) so they
997
+ * never cross the face from the front; the far arm and leg a few frames behind the near ones.
998
+ * The foot IK lets go at once (planted feet would hold the body up) and the feet flex, toes up.
999
+ * - `down` (from the hips touching down, played once and held: the K.O. lying pose): the shoulders
1000
+ * slap down, the legs keep rising for two frames and drop back with gravity — the near one
1001
+ * straight, a bounce of the heel, then the far one into a knee up (staggered) — the far arm
1002
+ * flops overhead and out along the floor, the near one out to the side, the head rolls towards
1003
+ * the camera; then a slow breath and a twitch of the foot. Posed in 3D for the body lying on its
1004
+ * back: the arms go out along the floor (`spread`), never across the face or into the floor, in
1005
+ * every view (the far arm overhead is behind the head from the side).
1006
+ */
1007
+ export function knockDownClips(o = {}) {
1008
+ const B = !o.busyFarHand;
1009
+ const farArm = (t) => (B ? t : {});
1010
+ const hand = (v, dur) => o.hands ? { "parts.handF.variant": [[0, v], [dur, v]], ...(B ? { "parts.handB.variant": [[0, v], [dur, v]] } : {}) } : {};
1011
+ const hold = (v, d) => [[0, v], [d, v]];
1012
+ // Into a contact (a limb or the back landing): faster and faster, like a fall; out of it: slowing.
1013
+ const IN = "sineIn", OUT = "sineOut";
1014
+ const F = 0.85, D = 3.6, R = 1.2;
1015
+ return {
1016
+ knocked: {
1017
+ duration: F,
1018
+ tracks: {
1019
+ ...hand("open", F),
1020
+ "ik.footF.mix": [[0, 1], [0.1, 0], [F, 0]],
1021
+ "ik.footB.mix": [[0, 1], [0.12, 0], [F, 0]],
1022
+ "bones.hips.y": hold(0, F),
1023
+ // Whiplash: the head snaps back first, the chest arches after it, then the body folds.
1024
+ "bones.head.rotation": [[0, 0], [0.05, -28, OUT], [0.13, -40], [0.3, -18], [0.5, -6], [0.7, 3], [F, 6]],
1025
+ "bones.body.rotation": [[0, 0], [0.06, -6], [0.16, -20, OUT], [0.32, -6], [0.52, 6], [F, 8]],
1026
+ // The arms lag the torso thrown back: forward and up relative to it (towards the hitter),
1027
+ // elbows bent and limp, opening out — never over the face, from any side.
1028
+ "bones.armF1.rotation": [[0, 0], [0.06, -10], [0.16, -36], [0.32, -60], [0.52, -68], [F, -70]],
1029
+ "bones.armF1.turn": [[0, 0], [0.12, -10], [0.4, -15], [F, -15]],
1030
+ "bones.armF1.spread": [[0, 0], [0.14, 10], [F, 12]],
1031
+ // (Soft but nearly straight: lying, the neck tucks the chin towards the chest, and a bent
1032
+ // forearm would fold the hand over the face.)
1033
+ "bones.armF2.rotation": [[0, 0], [0.06, -10], [0.16, -18], [0.34, -16], [F, -12]],
1034
+ ...farArm({
1035
+ "bones.armB1.rotation": [[0, 0], [0.1, -8], [0.2, -40], [0.36, -84], [0.56, -98], [F, -100]],
1036
+ "bones.armB1.turn": [[0, 0], [0.2, 8], [0.44, 26], [F, 30]],
1037
+ "bones.armB1.spread": [[0, 0], [0.3, 20], [F, 35]],
1038
+ "bones.armB2.rotation": [[0, 0], [0.1, -14], [0.2, -36], [0.4, -32], [0.6, -34], [F, -30]],
1039
+ }),
1040
+ // The jackknife: the near thigh leads, the far one a few frames behind; knees bent, feet flexed.
1041
+ "bones.legF1.rotation": [[0, 0], [0.06, -4], [0.16, -24], [0.32, -60], [0.52, -86], [0.7, -98], [F, -104]],
1042
+ "bones.legF2.rotation": [[0, 0], [0.06, 12], [0.16, 30], [0.32, 56], [0.52, 60], [F, 50]],
1043
+ "bones.legB1.rotation": [[0, 0], [0.1, 0], [0.2, -14], [0.38, -50], [0.58, -78], [F, -92]],
1044
+ "bones.legB2.rotation": [[0, 0], [0.1, 6], [0.2, 22], [0.38, 50], [0.6, 64], [F, 58]],
1045
+ "bones.footF.rotation": [[0, 0], [0.16, -12], [F, -15]],
1046
+ "bones.footB.rotation": [[0, 0], [0.2, -10], [F, -14]],
1047
+ },
1048
+ },
1049
+ down: {
1050
+ duration: D,
1051
+ tracks: {
1052
+ ...hand("open", D),
1053
+ "ik.footF.mix": hold(0, D),
1054
+ "ik.footB.mix": hold(0, D),
1055
+ "bones.hips.y": hold(0, D),
1056
+ // The shoulders slap down (the fall turns the body flat), a little rebound, then a slow breath.
1057
+ "bones.body.rotation": [[0, 8], [0.07, -2, IN], [0.18, 1], [0.32, 0], [1.9, -1.2], [D, 0]],
1058
+ "bones.head.rotation": [[0, 6], [0.1, -6, IN], [0.2, 1], [0.34, -3], [0.6, -4], [D, -4]],
1059
+ "bones.head.turn": [[0, 0], [0.35, 0], [0.9, -26], [D, -26]],
1060
+ // The near arm flops down along the body (closing in at once: from three quarters or the front
1061
+ // its side is the floor's), the far one over the head and out (clear of it), nearly
1062
+ // straight: an elbow bent with the arm overhead folds the forearm behind the head.
1063
+ // The far arm opens wide (`spread`) as it goes overhead: lying, the neck tucks the head
1064
+ // towards the chest, and a big cartoon head would otherwise meet the arm going past it.
1065
+ // (Lying turns a drawn view onto its back: from the side the floor is behind the body, from
1066
+ // the front it is the near side, so only moves along the body or towards its front — the
1067
+ // sky, or the camera — stay off the floor in every view.)
1068
+ "bones.armF1.rotation": [[0, -70], [0.08, -72], [0.32, -2, IN], [0.4, -14, OUT], [0.5, -6], [0.66, -8], [D, -8]],
1069
+ "bones.armF1.turn": [[0, -15], [0.12, 0], [D, 0]],
1070
+ "bones.armF1.spread": [[0, 12], [0.12, 0], [D, 0]],
1071
+ "bones.armF2.rotation": [[0, -12], [0.32, -6], [0.42, -22], [0.6, -12], [D, -12]],
1072
+ ...farArm({
1073
+ "bones.armB1.rotation": [[0, -100], [0.12, -130], [0.3, -166, IN], [0.38, -154], [0.5, -160], [D, -160]],
1074
+ "bones.armB1.turn": [[0, 30], [0.3, 10], [D, 10]],
1075
+ "bones.armB1.spread": [[0, 35], [0.1, 50], [0.3, 70], [D, 70]],
1076
+ "bones.armB2.rotation": [[0, -30], [0.3, -4], [0.38, -14], [0.5, -6], [D, -6]],
1077
+ }),
1078
+ // The legs keep rising for two frames, then drop: the near one straight (its heel bounces),
1079
+ // the far one a few frames later into a knee up, the foot flat.
1080
+ "bones.legF1.rotation": [[0, -104], [0.07, -112], [0.3, -6, IN], [0.38, -14, OUT], [0.46, -5, IN], [0.62, -8], [D, -8]],
1081
+ "bones.legF2.rotation": [[0, 50], [0.07, 42], [0.3, 6], [0.38, 14], [0.46, 5], [D, 7]],
1082
+ "bones.legB1.rotation": [[0, -92], [0.1, -104], [0.4, -54, IN], [0.48, -64, OUT], [0.58, -58], [0.72, -62], [D, -62]],
1083
+ "bones.legB2.rotation": [[0, 58], [0.1, 54], [0.4, 100], [0.48, 108], [0.58, 103], [D, 105]],
1084
+ "bones.footF.rotation": [[0, -15], [0.3, -4], [0.6, 0], [2.4, 0], [2.48, -16], [2.58, 5], [2.72, 0], [D, 0]],
1085
+ "bones.footB.rotation": [[0, -14], [0.4, 6], [0.7, 10], [D, 10]],
1086
+ },
1087
+ },
1088
+ rise: {
1089
+ duration: R,
1090
+ tracks: {
1091
+ // Timed for the scene's `getUp` (played over it): the first half sits up — the scene turns
1092
+ // the body up about the hips and lowers them, so it ends upright in a deep crouch —
1093
+ // the second half stands, the hips rising over the feet. (The planned crouch lowers the
1094
+ // hips; the foot IK takes the feet early, so they stay on the floor.) The head leads (chin in), the
1095
+ // legs fold in (the feet find the floor and the IK takes them), the near hand reaches for
1096
+ // the knees (clear of the floor as the body crouches), the far arm comes down from overhead,
1097
+ // then both swing forward for the momentum and back down; the torso folds
1098
+ // forward over the knees and straightens past upright.
1099
+ "ik.footF.mix": [[0, 0], [0.06 * R, 0], [0.24 * R, 1], [R, 1]],
1100
+ "ik.footB.mix": [[0, 0], [0.1 * R, 0], [0.28 * R, 1], [R, 1]],
1101
+ "bones.body.rotation": [[0, 0], [0.12 * R, -4], [0.42 * R, 24], [0.6 * R, 30], [0.84 * R, 8], [0.95 * R, -3], [R, 0]],
1102
+ "bones.head.rotation": [[0, -4], [0.12 * R, 16], [0.45 * R, 10], [0.72 * R, -6], [0.92 * R, 2], [R, 0]],
1103
+ "bones.head.turn": [[0, -26], [0.4 * R, 0], [R, 0]],
1104
+ "bones.armF1.rotation": [[0, -8], [0.12 * R, -10], [0.3 * R, -34], [0.45 * R, -22], [0.6 * R, -14], [0.8 * R, -42], [0.97 * R, -6], [R, 0]],
1105
+ "bones.armF2.rotation": [[0, -12], [0.4 * R, -8], [0.8 * R, -36], [R, 0]],
1106
+ ...farArm({
1107
+ "bones.armB1.rotation": [[0, -160], [0.18 * R, -120], [0.42 * R, 16], [0.62 * R, -4], [0.82 * R, -36], [R, 0]],
1108
+ "bones.armB1.turn": [[0, 10], [0.4 * R, 0], [R, 0]],
1109
+ "bones.armB1.spread": [[0, 70], [0.4 * R, 36], [0.7 * R, 8], [R, 0]],
1110
+ "bones.armB2.rotation": [[0, -6], [0.4 * R, -12], [0.82 * R, -36], [R, 0]],
1111
+ }),
1112
+ // (A leg's rotations add to its IK's: they let go as the IK takes it.)
1113
+ "bones.legF1.rotation": [[0, -8], [0.24 * R, 0], [R, 0]],
1114
+ "bones.legF2.rotation": [[0, 7], [0.24 * R, 0], [R, 0]],
1115
+ "bones.legB1.rotation": [[0, -62], [0.28 * R, 0], [R, 0]],
1116
+ "bones.legB2.rotation": [[0, 105], [0.28 * R, 0], [R, 0]],
1117
+ "bones.footF.rotation": [[0, 0], [0.3 * R, 0], [R, 0]],
1118
+ "bones.footB.rotation": [[0, 10], [0.3 * R, 0], [R, 0]],
1119
+ },
1120
+ },
1121
+ };
1122
+ }
1123
+ export const blinkAndBreathe = (period = 3.2) => [
1124
+ { type: "blink", id: "blink", part: "eyes", open: "open", closed: "closed", interval: [2, 4.6], duration: 0.14 },
1125
+ { type: "breathe", id: "breathe", bone: "body", amount: 0.012, period },
1126
+ ];
1127
+ /** Emotion poses shared by the cast (mouth/brow morph weights, eye variants, head tilt). */
1128
+ export const emotions = (extra = {}) => ({
1129
+ type: "pose",
1130
+ poses: {
1131
+ neutral: { "parts.eyes.variant": "open" },
1132
+ sleep: { "parts.eyes.variant": "closed", "parts.mouth.morph.smile": 0.3, "bones.head.rotation": 6 },
1133
+ happy: { "parts.eyes.variant": "open", "parts.mouth.morph.smile": 0.9, "parts.brows.morph.up": 0.35, "bones.head.rotation": -3 },
1134
+ joy: { "parts.eyes.variant": "happy", "parts.mouth.morph.grin": 0.8, "parts.brows.morph.up": 0.5, "bones.head.rotation": -6 },
1135
+ surprised: { "parts.eyes.variant": "wide", "parts.mouth.morph.E": 0.5, "parts.brows.morph.up": 1, "bones.head.rotation": -5 },
1136
+ sad: { "parts.eyes.variant": "open", "parts.mouth.morph.frown": 1, "parts.brows.morph.sad": 1, "bones.head.rotation": 8 },
1137
+ smug: { "parts.eyes.variant": "half", "parts.mouth.morph.smile": 0.7, "parts.brows.morph.smug": 1, "bones.head.rotation": -4 },
1138
+ scared: { "parts.eyes.variant": "wide", "parts.mouth.morph.frown": 0.7, "parts.brows.morph.sad": 1 },
1139
+ angry: { "parts.eyes.variant": "open", "parts.mouth.morph.frown": 0.9, "parts.brows.morph.cross": 1, "bones.head.rotation": 4 },
1140
+ ...extra,
1141
+ },
1142
+ });
1143
+ export const eyeSwitch = (bone) => ({
1144
+ id: "eyes",
1145
+ type: "switch",
1146
+ bone,
1147
+ variants: { open: "eyeOpen", wide: "eyeWide", closed: "eyeClosed", happy: "eyeHappy", half: "eyeOpen" },
1148
+ default: "open",
1149
+ });
1150
+ /**
1151
+ * Hand-drawn timing for gesture clips (traditional animation): **anticipation** — a small move the
1152
+ * other way before a big one — and **overshoot** — past the pose, then settling back. Applied to
1153
+ * bone rotations that change by at least `min` degrees; loops (walk, run, idle, talk…), the
1154
+ * `turn` squeeze and the knock-down clips (`knockDownClips`: a hit is a surprise, and their motion is
1155
+ * timed by the fall's physics, not acted) are left alone. The splines through the keys keep
1156
+ * everything smooth.
1157
+ */
1158
+ export function fluid(clips, o = {}) {
1159
+ const A = o.anticipation ?? 0.15, O = o.overshoot ?? 0.12, min = o.min ?? 20;
1160
+ const out = {};
1161
+ for (const [name, c] of Object.entries(clips)) {
1162
+ const clip = c;
1163
+ if (!clip?.tracks || clip.loop || name === "turn" || name === "knocked" || name === "down" || name === "rise") {
1164
+ out[name] = c;
1165
+ continue;
1166
+ }
1167
+ const tracks = {};
1168
+ for (const [ch, keys] of Object.entries(clip.tracks)) {
1169
+ if (!/^bones\.[^.]+\.rotation$/.test(ch) || keys.length < 2 || keys.some((k) => typeof k[1] !== "number")) {
1170
+ tracks[ch] = keys;
1171
+ continue;
1172
+ }
1173
+ const ks = [...keys].sort((a, b) => a[0] - b[0]);
1174
+ const v = (i) => ks[i][1], t = (i) => ks[i][0];
1175
+ const res = [ks[0]];
1176
+ let settled = -Infinity; // time of the last settle key added
1177
+ for (let i = 0; i + 1 < ks.length; i++) {
1178
+ const d = v(i + 1) - v(i), dt = t(i + 1) - t(i);
1179
+ const big = Math.abs(d) >= min && dt >= 0.12;
1180
+ const before = i > 0 ? v(i) - v(i - 1) : 0;
1181
+ const after = i + 2 < ks.length ? v(i + 2) - v(i + 1) : 0;
1182
+ const fromRest = Math.abs(before) < min * 0.3 || Math.sign(before) !== Math.sign(d);
1183
+ const toRest = Math.abs(after) < min * 0.3 || Math.sign(after) !== Math.sign(d);
1184
+ const at = t(i) + dt * 0.3;
1185
+ if (big && fromRest && at > settled + 0.02)
1186
+ res.push([r(at), r(v(i) - d * A)]);
1187
+ const room = (i + 2 < ks.length ? t(i + 2) : clip.duration) - t(i + 1);
1188
+ if (big && toRest && room >= 0.16) {
1189
+ const end = [...ks[i + 1]];
1190
+ end[1] = r(v(i + 1) + d * O);
1191
+ settled = r(t(i + 1) + Math.min(0.14, room * 0.45));
1192
+ res.push(end, [settled, v(i + 1)]);
1193
+ }
1194
+ else
1195
+ res.push(ks[i + 1]);
1196
+ }
1197
+ tracks[ch] = res;
1198
+ }
1199
+ out[name] = { ...clip, tracks };
1200
+ }
1201
+ return out;
1202
+ }
1203
+ /**
1204
+ * Inside of a `mouthShapes` mouth, with the same shape names (they morph together): the upper
1205
+ * `teeth` (a white band under the top lip) and the `tongue` (at the bottom). Hidden when closed.
1206
+ */
1207
+ export function mouthInside(L, R, scale = 1, style = {}) {
1208
+ const table = mouthTable();
1209
+ const shape = (kind, open, smile, round) => {
1210
+ const o = open * scale;
1211
+ const w = R[0] - L[0];
1212
+ const k = style.lift ?? 1;
1213
+ // Closed: nothing inside shows (collapsed in the middle of the mouth, same commands).
1214
+ if (open < 3) {
1215
+ const c = `${r((L[0] + R[0]) / 2)} ${r((L[1] + R[1]) / 2)}`;
1216
+ return `M${c} Q${c} ${c} Q${c} ${c} Z`;
1217
+ }
1218
+ const l = [L[0] + w * round * 0.22, L[1] - smile * (style.symmetric ? 5 : 4) * k];
1219
+ const rr = [R[0] - w * round * 0.22, R[1] - smile * (style.symmetric ? 5 : 6) * k];
1220
+ const mx = (l[0] + rr[0]) / 2;
1221
+ const topY = (l[1] + rr[1]) / 2 + smile * 3 * k - o * 0.15;
1222
+ const botY = (l[1] + rr[1]) / 2 + smile * 4 * k + o;
1223
+ if (kind === "teeth") {
1224
+ // Along the top lip, as deep as a third of the opening (at most a tooth's height).
1225
+ const t = Math.min(o * 0.34, 7 * scale);
1226
+ const a = [l[0] + w * 0.08, l[1] + 1], b = [rr[0] - w * 0.08, rr[1] + 1];
1227
+ return `M${r(a[0])} ${r(a[1])} Q${r(mx)} ${r(topY + 1)} ${r(b[0])} ${r(b[1])} Q${r(mx)} ${r(topY + 1 + t * 1.6)} ${r(a[0])} ${r(a[1])} Z`;
1228
+ }
1229
+ // The tongue: a mound on the bottom lip, two thirds of the mouth wide.
1230
+ const h = Math.min(o * 0.45, 9 * scale);
1231
+ const a = [l[0] + w * 0.2, (l[1] + botY) / 2 + o * 0.25], b = [rr[0] - w * 0.2, (rr[1] + botY) / 2 + o * 0.25];
1232
+ return `M${r(a[0])} ${r(a[1])} Q${r(mx)} ${r(botY + o * 0.4 - h * 2)} ${r(b[0])} ${r(b[1])} Q${r(mx)} ${r(botY + o * 0.4)} ${r(a[0])} ${r(a[1])} Z`;
1233
+ };
1234
+ const make = (kind) => ({
1235
+ base: shape(kind, ...table.base),
1236
+ shapes: Object.fromEntries(Object.entries(table.shapes).map(([k, v]) => [k, shape(kind, ...v)])),
1237
+ });
1238
+ return { teeth: make("teeth"), tongue: make("tongue") };
1239
+ }
1240
+ /**
1241
+ * A cartoon **gape** (a jaw dropped to swallow something whole, a scream, a big yawn) as the three
1242
+ * paths of a mouth wide open, in the same commands as `mouthShapes`/`mouthInside` (two curves: they
1243
+ * morph with them): the `mouth` from corner `l` over the top lip (its middle at height `top`) to
1244
+ * corner `rr`, back under a round lower edge (its lowest point at `bottom`, bulging `out` past the
1245
+ * corners); the upper `teeth`, a row along the top lip (`teeth` deep in the middle); the `tongue`
1246
+ * low, a mound on the bottom. Draw the inside dark (the mouth's fill); the lower teeth stay hidden.
1247
+ */
1248
+ export function gapeMouth(l, rr, top, bottom, o = {}) {
1249
+ const w = rr[0] - l[0], mx = (l[0] + rr[0]) / 2, out = o.out ?? w * 0.12, h = bottom - top;
1250
+ // A cubic whose controls sit at one height passes through `y` in its middle: c = (8y − y0 − y1) / 6.
1251
+ const ctl = (y, a, b) => (8 * y - a[1] - b[1]) / 6;
1252
+ const C = (...q) => q.map(r).join(" ");
1253
+ const cT = ctl(top, l, rr), cB = ctl(bottom, rr, l);
1254
+ const mouth = `M${C(...l)} C${C(l[0] + w * 0.22, cT, rr[0] - w * 0.22, cT)} ${C(...rr)} C${C(rr[0] + out, cB, l[0] - out, cB)} ${C(...l)} Z`;
1255
+ // The teeth: the top lip again, and under it the same curve lowered (clipped by the mouth).
1256
+ const t = (o.teeth ?? h * 0.14) / 0.75;
1257
+ const ta = [l[0] + w * 0.03, l[1] + 1], tb = [rr[0] - w * 0.03, rr[1] + 1], tT = ctl(top + 1, ta, tb);
1258
+ const teeth = `M${C(...ta)} C${C(ta[0] + w * 0.2, tT - 3, tb[0] - w * 0.2, tT - 3)} ${C(...tb)} C${C(tb[0] - w * 0.2, tT + t, ta[0] + w * 0.2, tT + t)} ${C(...ta)} Z`;
1259
+ // The tongue: a mound across the bottom, two thirds of the mouth wide, down past the lower edge.
1260
+ const ga = [mx - w * 0.36, bottom - h * 0.2], gb = [mx + w * 0.36, bottom - h * 0.2];
1261
+ const gT = ctl(bottom - h * 0.36, ga, gb), gB = ctl(bottom + h * 0.25, gb, ga);
1262
+ const tongue = `M${C(...ga)} C${C(ga[0] + w * 0.08, gT, gb[0] - w * 0.08, gT)} ${C(...gb)} C${C(gb[0], gB, ga[0], gB)} ${C(...ga)} Z`;
1263
+ return { mouth, teeth, tongue };
1264
+ }
1265
+ /**
1266
+ * A morph shape that reaches `target` at weight `at` (default 2) from `base` — a morph is linear
1267
+ * (`base + w·(shape − base)`), so the shape is the point `1/at` of the way: weight 1 is a gape,
1268
+ * weight 2 the unhinged jaw, every weight in between a shape between them. Both paths must have the
1269
+ * same segments once made cubic (the same commands, or `Q` against `C`).
1270
+ */
1271
+ export function morphTowards(base, target, at = 2) {
1272
+ const b = parsePath(base), bp = pathPoints(b), tp = pathPoints(parsePath(target));
1273
+ if (bp.length !== tp.length)
1274
+ throw new Error(`morphTowards: ${bp.length} points against ${tp.length}`);
1275
+ return pathToString(withPoints(b, bp.map((p, i) => [r(p[0] + (tp[i][0] - p[0]) / at), r(p[1] + (tp[i][1] - p[1]) / at)])));
1276
+ }
1277
+ /** Open / smile / round of each `mouthShapes` shape. */
1278
+ function mouthTable() {
1279
+ return {
1280
+ base: [1.5, 0.7, 0],
1281
+ shapes: {
1282
+ A: [0.8, 0.4, 0.05], B: [6, 0.6, 0.05], C: [12, 0.5, 0.1], D: [18, 0.4, 0], E: [13, 0.2, 0.45], F: [7, 0, 0.75],
1283
+ G: [4, 0.5, 0.1], H: [10, 0.4, 0.15], smile: [4, 2.2, 0], frown: [1.5, -1.4, 0.1], grin: [14, 2.4, 0],
1284
+ },
1285
+ };
1286
+ }
1287
+ /**
1288
+ * Cartoon hands with fingers (four fingers and a thumb, drawn around the wrist `at`), as switch
1289
+ * variants `open`, `fist`, `point` and `grip` — the same names as `handShapes`. `dir` is the
1290
+ * direction of the fingers in setup space (90 = down, a hanging arm); `line` the outline colour.
1291
+ */
1292
+ /**
1293
+ * A closed outline through points (a Catmull-Rom curve as cubic Béziers); a point flagged `true` is a
1294
+ * corner (the line turns sharply there: the notch between two fingers).
1295
+ */
1296
+ function smoothLoop(p, at) {
1297
+ const n = p.length;
1298
+ const q = (i) => p[(i + n) % n];
1299
+ let d = `M${at(p[0][0], p[0][1])}`;
1300
+ for (let i = 0; i < n; i++) {
1301
+ const a = q(i), b = q(i + 1), a0 = q(i - 1), b1 = q(i + 2);
1302
+ const ta = a[2] ? [0, 0] : [(b[0] - a0[0]) / 6, (b[1] - a0[1]) / 6];
1303
+ const tb = b[2] ? [0, 0] : [(b1[0] - a[0]) / 6, (b1[1] - a[1]) / 6];
1304
+ d += ` C${at(a[0] + ta[0], a[1] + ta[1])} ${at(b[0] - tb[0], b[1] - tb[1])} ${at(b[0], b[1])}`;
1305
+ }
1306
+ return d + " Z";
1307
+ }
1308
+ /**
1309
+ * Hands drawn as one silhouette (a modern TV cartoon's): a palm a little wider than the wrist, three
1310
+ * slim fingers and a thumb in one outline — the notches between the fingers the only lines inside it
1311
+ * — that goes on back into the forearm as wide as it (`wrist`: the forearm's half width there) with
1312
+ * no line across the wrist: the arm's outline runs on into the hand's. Fingers along +x, then turned.
1313
+ */
1314
+ function silhouetteHands(at, o) {
1315
+ const [x, y] = at, R = o.r, ink = o.line ?? "palette(ink)", w = o.stroke ?? 2.6;
1316
+ const P_ = (dx, dy) => `${r(x + dx * R)} ${r(y + dy * R)}`;
1317
+ // The wrist's half width (hand radii) where the hand's outline starts: on the forearm's outline.
1318
+ const ww = Math.min(0.4, o.wrist / R);
1319
+ // (Filled back into the forearm, inside its outline, over the round end of the arm's drawing.)
1320
+ const ws = Math.max(0.05, (o.wrist - w * 0.5) / R);
1321
+ const draw = (pts, inner = "") => {
1322
+ const body = [[-0.1, -ww, true], ...pts, [-0.1, ww, true]];
1323
+ const stub = `<path d="M${P_(0.15, -ws)} L${P_(-0.6, -ws)} L${P_(-0.6, ws)} L${P_(0.15, ws)} Z" fill="${o.fill}"/>`;
1324
+ // The outline open at the wrist: from one side of the forearm round the hand to the other.
1325
+ const d = smoothLoop(body, P_);
1326
+ // (Out of a sleeve, `cuff`: closed — the sleeve's edge is a line where the colour changes.)
1327
+ const open = o.cuff ? d : d.replace(/ C[^C]*Z$/, "");
1328
+ return `<path d="${d}" fill="${o.fill}"/>${o.cuff ? "" : stub}<path d="${open}${inner}" fill="none" stroke="${ink}" stroke-width="${w}" stroke-linejoin="round" stroke-linecap="round"/>`;
1329
+ };
1330
+ // (Relaxed: the thumb along the fingers, a little apart; the fingers' tips in a gentle curve.)
1331
+ const thumbOpen = [[0.12, -0.36], [0.32, -0.47], [0.56, -0.57], [0.74, -0.57], [0.8, -0.48], [0.7, -0.4], [0.62, -0.37, true]];
1332
+ const fingers = [
1333
+ [1.08, -0.38], [1.2, -0.32], [1.2, -0.2], [1.06, -0.14], [0.92, -0.12, true],
1334
+ [1.24, -0.13], [1.36, -0.06], [1.35, 0.07], [1.22, 0.13], [0.92, 0.13, true],
1335
+ [1.1, 0.15], [1.2, 0.22], [1.17, 0.34], [1.02, 0.39], [0.62, 0.41], [0.18, 0.36],
1336
+ ];
1337
+ // Closed: the curled fingers a round front, the thumb laid along them (its one line).
1338
+ const fistPts = [[0.14, -0.4], [0.42, -0.54], [0.74, -0.52], [0.92, -0.34], [0.96, -0.02], [0.88, 0.3], [0.64, 0.46], [0.3, 0.44], [0.06, 0.34]];
1339
+ const thumbLine = ` M${P_(0.36, -0.36)} Q${P_(0.62, -0.32)} ${P_(0.74, -0.14)}`;
1340
+ const pointPts = [[0.14, -0.4], [0.42, -0.54], [0.74, -0.54], [1.36, -0.5], [1.46, -0.42], [1.38, -0.31], [0.92, -0.28, true], [0.96, -0.02], [0.88, 0.3], [0.64, 0.46], [0.3, 0.44], [0.06, 0.34]];
1341
+ const gripPts = [[0.14, -0.42], [0.44, -0.56], [0.86, -0.52], [1.06, -0.3], [1.04, 0.08], [0.9, 0.42], [0.62, 0.56], [0.3, 0.5], [0.06, 0.36]];
1342
+ // (`mirror`: the other hand — the thumb on the other side of the fingers' axis.)
1343
+ const turn = (art) => `<g transform="rotate(${o.dir ?? 90} ${x} ${y})${o.mirror ? ` translate(0 ${r(2 * y)}) scale(1 -1)` : ""}">${art}</g>`;
1344
+ return {
1345
+ open: turn(draw([...thumbOpen, ...fingers])),
1346
+ fist: turn(draw(fistPts, thumbLine)),
1347
+ point: turn(draw(pointPts, thumbLine)),
1348
+ grip: turn(draw(gripPts, ` M${P_(0.36, -0.38)} Q${P_(0.7, -0.36)} ${P_(0.86, -0.12)}`)),
1349
+ };
1350
+ }
1351
+ export function cartoonHands(at, o) {
1352
+ // `wrist` (the forearm's half width at the wrist): one silhouette welded into the forearm (`cuff`:
1353
+ // out of a sleeve, a line across the wrist; `mirror`: drawn as the other hand, the thumb across the
1354
+ // fingers' axis).
1355
+ if (o.wrist !== undefined)
1356
+ return silhouetteHands(at, { ...o, wrist: o.wrist });
1357
+ // (`detail`: the knuckle lines' weight, thinner than the outline unless a style draws every line alike.)
1358
+ const [x, y] = at, R = o.r, ink = o.line ?? "palette(ink)", w = o.stroke ?? 2.6, wd = o.detail ?? w * 0.7;
1359
+ const st = `stroke="${ink}" stroke-width="${w}" stroke-linejoin="round" stroke-linecap="round"`;
1360
+ // Drawn with the fingers along +x, then turned.
1361
+ const P_ = (dx, dy) => `${r(x + dx * R)} ${r(y + dy * R)}`;
1362
+ const finger = (y0, len, bend = 0) => `<path d="M${P_(0.35, y0 - 0.15)} L${P_(0.35 + len, y0 - 0.15 + bend)} Q${P_(0.55 + len, y0 + bend)} ${P_(0.35 + len, y0 + 0.15 + bend)} L${P_(0.35, y0 + 0.15)} Z" fill="${o.fill}" ${st}/>`;
1363
+ const palm = `<path d="M${P_(-0.55, -0.5)} Q${P_(-0.6, 0)} ${P_(-0.55, 0.5)} L${P_(0.45, 0.58)} Q${P_(0.62, 0)} ${P_(0.45, -0.58)} Z" fill="${o.fill}" ${st}/>`;
1364
+ const thumb = (a) => `<g transform="rotate(${a} ${P_(-0.2, -0.45).replace(" ", " ")})"><path d="M${P_(-0.3, -0.62)} L${P_(0.25, -0.62)} Q${P_(0.45, -0.48)} ${P_(0.25, -0.34)} L${P_(-0.3, -0.34)} Z" fill="${o.fill}" ${st}/></g>`;
1365
+ const curled = `<path d="M${P_(0.3, -0.55)} Q${P_(0.85, -0.6)} ${P_(0.85, 0)} Q${P_(0.85, 0.6)} ${P_(0.3, 0.55)}" fill="${o.fill}" ${st}/>` +
1366
+ `<path d="M${P_(0.55, -0.18)} H${P_(0.84, -0.18).split(" ")[0]} M${P_(0.55, 0.18)} H${P_(0.84, 0.18).split(" ")[0]}" stroke="${ink}" stroke-width="${r(wd)}" stroke-linecap="round"/>`;
1367
+ // `merged`: one silhouette — every piece's outline drawn first, twice as wide, then every fill over
1368
+ // it, so the fingers, palm and thumb read as one mitten-like hand with a single outline (a modern
1369
+ // TV cartoon's), short lines between the fingers; otherwise each piece is outlined on its own.
1370
+ const under = `stroke="${ink}" stroke-width="${r(w * 2)}" stroke-linejoin="round" stroke-linecap="round"`;
1371
+ const one = (art, gaps = "") => (o.merged ? art.split(st).join(under) + art.split(st).join(`stroke="none"`) + gaps : art);
1372
+ const gap = (y0, len) => `<path d="M${P_(0.5, y0)} L${P_(0.5 + len, y0)}" stroke="${ink}" stroke-width="${r(wd)}" stroke-linecap="round" fill="none"/>`;
1373
+ const turn = (art) => `<g transform="rotate(${o.dir ?? 90} ${x} ${y})">${art}</g>`;
1374
+ return {
1375
+ open: turn(one(finger(0.38, 0.62, 0.06) + finger(0.12, 0.78) + finger(-0.14, 0.82) + finger(-0.4, 0.7, -0.04) + palm + thumb(-35), gap(0.25, 0.35) + gap(-0.01, 0.45) + gap(-0.27, 0.4))),
1376
+ fist: turn(one(palm + curled + thumb(0))),
1377
+ point: turn(one(finger(-0.36, 0.95) + palm + curled + thumb(10))),
1378
+ grip: turn(one(palm + `<path d="M${P_(0.3, -0.55)} Q${P_(1.05, -0.5)} ${P_(0.95, 0.15)} Q${P_(0.8, 0.6)} ${P_(0.3, 0.55)}" fill="${o.fill}" ${st}/>` + thumb(-15))),
1379
+ };
1380
+ }
1381
+ //# sourceMappingURL=rig.js.map