Art and animation, written as code. Illustrations, loops, interactive web art, launch-videos and scored films in dozens of styles, identical on every render.
TypeScript agent-skillsanimationcanvasclaude-codeclaude-pluginclaude-skillscodex-plugincreative-codecreative-codingcreative-toolsdeterministicgenerative-arthand-drawnillustrationlaunch-videomotion-designmotion-graphicspluginprocedural-generationskill
cheetah, run: the dusk loop turns its head, stretches and runs; a signed cut; a recorded drum pinned to a note plays it
Alex Greenshpundda15541a1ef
diff --git a/skills/anidoodle/engine/src/canvas-core/cheetahBody.ts b/skills/anidoodle/engine/src/canvas-core/cheetahBody.tsnew file mode 100644index 0000000..203d050--- /dev/null+++ b/skills/anidoodle/engine/src/canvas-core/cheetahBody.ts@@ -0,0 +1,187 @@+// THE CHEETAH, SIDE VIEW. One character module: a pose goes in, the animal is drawn. Shots pose it,+// they never redraw it. It is the same animal as the face in cheetahDusk and is made the same way:+// tapered tufts over a mass, solid spots, the tear line as the one big black shape.+// Reference opened (Wikimedia Commons), for structure only:+// - "Sarah Sprints" (CC BY 2.0): the build side-on, deep chest, tucked waist, spots following the form+// - "Acinonyxrunningrespiration.png" (CC BY-SA 3.0): the two flight phases, where the spine folds and opens+// - "Speed 2 - Running Cheetah" bronze (CC BY-SA 4.0): the extended phase, how the hind legs trail+// - de Blainville's skeleton plate (public domain): scapula, the long metatarsus, the hock high off the foot+// The anatomy it commits to: a small domed head held level; a long neck in line with the back; the+// scapula riding over the ribs; chest twice as deep as the waist; hind legs longer than fore, with a+// knee, a hock and a long metatarsus; a tail two thirds of the body, ringed at the end, white-tipped;+// claws that show; dark pads on a trailing hind foot.+// Everything is authored FACING RIGHT, y down, in the animal's own units (about 920 nose-paw to tail tip).+import { rng, type Ctx, type P } from "./core";+import { mix } from "./gallery";++const NIGHT = "#0d1a22", OCHRE = "#e3a23a", GOLD = "#efb84e", CREAM = "#f2e4c5", PALE = "#fff5de", DARK = "#0a1216";+const ROWS = ["#a9601f", "#cf8a2c", OCHRE, GOLD, "#f2c263", "#f4cd7e", CREAM]; // back to belly+const clamp = (v: number, a = 0, b = 1) => Math.max(a, Math.min(b, v));+const lerp = (a: number, b: number, t: number) => a + (b - a) * t;++export type Pose = {+ spine: P[]; // 10 stations: rump, hip, loin, waist, rear ribs, chest, shoulder, neck base, neck, head join+ tail: P[]; // 6, from its root to its tip+ foreN: P[]; foreF: P[]; // scapula, shoulder, elbow, wrist, toes (near side, far side)+ hindN: P[]; hindF: P[]; // hip, knee, hock, ball, toes+ head: number; // the head's own angle, radians: 0 is level, positive is nose down+};++// the build: how thick the animal is at each station. A pose moves points; it never changes these.+const SPINE_UP = [14, 38, 40, 44, 54, 66, 66, 50, 38, 34], SPINE_DN = [22, 52, 44, 42, 68, 90, 76, 48, 36, 32];+const TAIL_W = [15, 13, 12, 12, 13, 11], FORE_W = [20, 30, 19, 12, 8], HIND_W = [44, 30, 13, 12, 8];++// the extended flight phase: every foot off the ground, the spine opened out, the forelegs reaching+export const EXTENDED: Pose = {+ spine: [[322, 500], [365, 505], [440, 512], [505, 516], [570, 522], [640, 524], [700, 518], [752, 503], [800, 492], [838, 486]],+ tail: [[326, 486], [262, 474], [196, 476], [132, 458], [84, 424], [56, 392]],+ foreN: [[690, 494], [708, 545], [798, 584], [898, 610], [956, 626]],+ foreF: [[698, 498], [716, 548], [812, 566], [916, 574], [974, 582]],+ hindN: [[372, 520], [292, 584], [188, 572], [112, 594], [80, 606]],+ hindF: [[380, 522], [310, 604], [212, 612], [140, 642], [110, 656]],+ head: 0.1,+};++// a tuft: a tapered leaf from radius r0 to r1 along angle a, darker at its root (the mark cheetahDusk is made of)+const tuft = (c: Ctx, o: P, a: number, r0: number, r1: number, w: number, col: string, vein = 0.22, root = 0.6) => {+ if (r1 - r0 < 1.5 || w < 0.6) return;+ const ca = Math.cos(a), sa = Math.sin(a), b: P = [o[0] + ca * r0, o[1] + sa * r0], t: P = [o[0] + ca * r1, o[1] + sa * r1], m = r0 + (r1 - r0) * 0.42, mx = o[0] + ca * m, my = o[1] + sa * m;+ const g = c.createLinearGradient(b[0], b[1], t[0], t[1]); g.addColorStop(0, mix(col, DARK, root)); g.addColorStop(0.45, col); g.addColorStop(1, mix(col, "#ffffff", 0.16));+ c.fillStyle = g; c.beginPath(); c.moveTo(b[0], b[1]); c.quadraticCurveTo(mx - sa * w, my + ca * w, t[0], t[1]); c.quadraticCurveTo(mx + sa * w, my - ca * w, b[0], b[1]); c.fill();+ if (vein > 0) { c.strokeStyle = mix(col, DARK, 0.7); c.globalAlpha = vein; c.lineWidth = 1; c.beginPath(); c.moveTo(b[0], b[1]); c.lineTo(o[0] + ca * (r1 - 3), o[1] + sa * (r1 - 3)); c.stroke(); c.globalAlpha = 1; }+};++// a ribbon: a smooth curve through the points with its own thickness on each side. Side a is on the+// left hand of travel (the top, for a chain running to the right); side b is the other.+type Rib = { c: P[]; a: P[]; b: P[]; ang: number[]; len: number };+const cr = (p0: number, p1: number, p2: number, p3: number, t: number) => 0.5 * (2 * p1 + (p2 - p0) * t + (2 * p0 - 5 * p1 + 4 * p2 - p3) * t * t + (3 * p1 - p0 - 3 * p2 + p3) * t * t * t);+const ribbon = (pts: P[], wa: number[], wb: number[] = wa, per = 12): Rib => {+ const g = (i: number) => pts[clamp(i, 0, pts.length - 1)], cs: P[] = [], ua: number[] = [], ub: number[] = [];+ for (let i = 0; i < pts.length - 1; i++) for (let k = 0; k < per + (i === pts.length - 2 ? 1 : 0); k++) {+ const t = k / per, e = t * t * (3 - 2 * t);+ cs.push([cr(g(i - 1)[0], g(i)[0], g(i + 1)[0], g(i + 2)[0], t), cr(g(i - 1)[1], g(i)[1], g(i + 1)[1], g(i + 2)[1], t)]); ua.push(lerp(wa[i], wa[i + 1], e)); ub.push(lerp(wb[i], wb[i + 1], e));+ }+ const a: P[] = [], b: P[] = [], ang: number[] = []; let len = 0;+ cs.forEach((p, i) => { const q0 = cs[Math.max(0, i - 1)], q1 = cs[Math.min(cs.length - 1, i + 1)], th = Math.atan2(q1[1] - q0[1], q1[0] - q0[0]), nx = Math.sin(th), ny = -Math.cos(th); ang.push(th); a.push([p[0] + nx * ua[i], p[1] + ny * ua[i]]); b.push([p[0] - nx * ub[i], p[1] - ny * ub[i]]); if (i) len += Math.hypot(p[0] - cs[i - 1][0], p[1] - cs[i - 1][1]); });+ return { c: cs, a, b, ang, len };+};+// a place on the ribbon: s along it (0-1), v across it (-1 is side a, +1 is side b)+const on = (r: Rib, s: number, v: number) => {+ const x = clamp(s) * (r.c.length - 1), i = Math.min(r.c.length - 2, Math.floor(x)), f = x - i, e = v < 0 ? r.a : r.b, k = Math.abs(v), L = (q: P[], d: 0 | 1) => lerp(q[i][d], q[i + 1][d], f);+ return { p: [lerp(L(r.c, 0), L(e, 0), k), lerp(L(r.c, 1), L(e, 1), k)] as P, ang: r.ang[i], w: Math.hypot(L(r.a, 0) - L(r.b, 0), L(r.a, 1) - L(r.b, 1)) };+};+const fillRib = (c: Ctx, r: Rib, col: string) => { c.fillStyle = col; c.beginPath(); r.a.forEach((p, i) => (i ? c.lineTo(p[0], p[1]) : c.moveTo(p[0], p[1]))); [...r.b].reverse().forEach((p) => c.lineTo(p[0], p[1])); c.closePath(); c.fill(); };+const blob = (c: Ctx, pts: P[], per = 8) => { const n = pts.length, g = (i: number) => pts[((i % n) + n) % n]; c.beginPath(); for (let i = 0; i < n; i++) for (let k = 0; k < per; k++) { const t = k / per, x = cr(g(i - 1)[0], g(i)[0], g(i + 1)[0], g(i + 2)[0], t), y = cr(g(i - 1)[1], g(i)[1], g(i + 1)[1], g(i + 2)[1], t); if (i + k) c.lineTo(x, y); else c.moveTo(x, y); } c.closePath(); };+const spot = (c: Ctx, p: P, rx: number, ry: number, a: number, col: string) => { c.fillStyle = col; c.beginPath(); c.ellipse(p[0], p[1], rx, ry, a, 0, Math.PI * 2); c.fill(); };++// a foot: four toes, each its own mark, each with its claw (a cheetah's never fully sheathe). A+// trailing hind foot shows its pads.+const paw = (c: Ctx, r: Rib, shade: number, hind: boolean) => {+ const sh = (col: string) => (shade ? mix(col, NIGHT, shade) : col), { p, ang } = on(r, 0.88, 0), pads = hind && Math.cos(ang) < -0.35; // a hind foot trailing behind shows its sole+ for (const k of [-1.5, 1.5, -0.5, 0.5]) {+ const a = ang + k * 0.15, L = 34 - Math.abs(k) * 3;+ tuft(c, p, a, 0, L, 6.6, sh(Math.abs(k) > 1 ? CREAM : PALE), 0.3, 0.35);+ tuft(c, [p[0] + Math.cos(a) * (L - 3), p[1] + Math.sin(a) * (L - 3)], a + 0.5, 0, 8, 1.9, sh("#2a1a12"), 0);+ }+ if (pads) { const q = on(r, 0.93, 0.8); spot(c, q.p, 5, 2.3, ang, sh("#3a2418")); const t = on(r, 1, 0.7); spot(c, [t.p[0] + Math.cos(ang) * 6, t.p[1] + Math.sin(ang) * 6], 2.4, 1.7, ang, sh("#3a2418")); }+};++// a leg: its mass, the fur lying toward the foot (laid from the foot upward, so each tuft overlaps+// the one below it), the spots thinning out toward the wrist, the fold where it meets the body.+const limb = (c: Ctx, joints: P[], widths: number[], seed: number, shade: number, hind: boolean) => {+ // the leg starts inside the body: the haunch and the point of the shoulder taper back into it, never a cut end+ const d = Math.hypot(joints[1][0] - joints[0][0], joints[1][1] - joints[0][1]) || 1, ext = widths[0] * 0.85, pts: P[] = [[joints[0][0] - ((joints[1][0] - joints[0][0]) / d) * ext, joints[0][1] - ((joints[1][1] - joints[0][1]) / d) * ext], ...joints], w = [widths[0] * 0.45, ...widths];+ const r = ribbon(pts, w, w, 10), L = Math.round(ext + (hind ? 320 : 315)), q = rng(seed), sh = (col: string) => (shade ? mix(col, NIGHT, shade) : col);+ fillRib(c, r, sh("#c2832a"));+ for (let i = Math.floor((L * 0.88) / 7); i >= 0; i--) {+ const s = (i * 7) / L, w0 = on(r, s, 0).w, rows = Math.max(2, Math.round(w0 / 11));+ for (let j = 0; j < rows; j++) {+ const v = lerp(-0.78, 0.78, rows === 1 ? 0.5 : j / (rows - 1)) + (q() - 0.5) * 0.3, at = on(r, s + (q() - 0.5) * (6 / L), v), under = hind ? v < -0.35 : v > 0.35, pale = clamp(s * 1.5 - 0.5) * 0.55;+ tuft(c, at.p, at.ang + (q() - 0.5) * 0.22, 0, clamp(w0 * 0.8, 16, 34) * (0.75 + q() * 0.5), clamp(w0 * 0.2, 2.6, 7), sh(mix(under ? "#f4cd7e" : q() < 0.5 ? GOLD : OCHRE, CREAM, pale)), 0.08, 0.26);+ }+ }+ for (let i = 1; i * 15 < L * 0.8; i++) {+ const s = (i * 15) / L, w0 = on(r, s, 0).w, n = Math.max(1, Math.round(w0 / 19));+ for (let j = 0; j < n; j++) { const v = (n === 1 ? 0 : lerp(-0.62, 0.62, j / (n - 1))) + (q() - 0.5) * 0.3, at = on(r, s + (q() - 0.5) * 0.02 + (j % 2) * (7 / L), v), z = clamp(w0 * 0.075, 1.7, 6.2) * (0.8 + q() * 0.4); spot(c, at.p, z * 1.15, z * 0.85 * Math.sqrt(1 - v * v * 0.6), at.ang, sh(DARK)); }+ }+ // the fold: the stifle under the thigh, the back of the shoulder. Thinner than an outline, and it fades.+ const e = hind ? r.a : r.b, m = Math.floor(e.length * 0.3); c.strokeStyle = sh(DARK); c.lineWidth = 2.2; c.lineCap = "round";+ for (let i = 1; i < m; i++) { c.globalAlpha = 0.34 * (1 - i / m); c.beginPath(); c.moveTo(e[i - 1][0], e[i - 1][1]); c.lineTo(e[i][0], e[i][1]); c.stroke(); }+ c.globalAlpha = 1;+ paw(c, r, shade, hind);+};++const tail = (c: Ctx, pts: P[]) => {+ const r = ribbon(pts, TAIL_W, TAIL_W, 10), L = 299, q = rng(23);+ fillRib(c, r, "#c2832a");+ for (let i = 0; i * 11 < L; i++) { const s = (i * 11) / L; for (const v of [0.6, -0.6, 0]) { const at = on(r, s, v); tuft(c, at.p, at.ang + (q() - 0.5) * 0.25 - v * 0.12, 0, 26 + q() * 8, 6.5, v > 0.3 ? "#f4cd7e" : v < -0.3 ? "#cf8a2c" : OCHRE, 0.18); } }+ // spots that close up into rings toward the end, then the white tip+ for (let i = 1; i * 17 < L * 0.62; i++) for (const v of [-0.5, 0.25]) { const at = on(r, (i * 17) / L + (v > 0 ? 8 / L : 0), v + (q() - 0.5) * 0.2); spot(c, at.p, 5, 3.8, at.ang, DARK); }+ for (const [s, wd] of [[0.68, 9], [0.78, 11], [0.88, 12]] as const) { const k = ribbon([on(r, s - 0.006, -1.05).p, on(r, s, 0).p, on(r, s + 0.006, 1.05).p], [wd / 2, wd / 2 + 1.5, wd / 2], undefined, 6); fillRib(c, k, DARK); }+ const end = on(r, 0.95, 0); for (let j = -3; j <= 3; j++) tuft(c, end.p, end.ang + j * 0.2, 0, 30 - Math.abs(j) * 3, 6.5, j % 2 ? PALE : CREAM, 0.15);+};++// the head in profile, authored point by point in its own frame (origin mid-skull, nose toward +x).+// The coat still opens in rings from the bridge of the nose, as it does on the face seen from the front.+const SKULL: P[] = [[-37, 2], [-33, -16], [-19, -30], [2, -33], [20, -27], [33, -17], [45, -9], [57, -5], [61, 3], [57, 11], [50, 22], [34, 28], [10, 31], [-16, 28], [-33, 17]];+const head = (c: Ctx, at: P, ang: number) => {+ c.save(); c.translate(at[0], at[1]); c.rotate(ang); c.scale(1.18, 1.18);+ // the far ear's tip, then the skull+ blob(c, [[-12, -28], [-17, -43], [-27, -45], [-30, -30]]); c.fillStyle = mix("#a9601f", NIGHT, 0.45); c.fill();+ blob(c, SKULL); c.fillStyle = "#c98a2e"; c.fill();+ c.save(); blob(c, SKULL); c.clip();+ const q = rng(61), N: P = [44, -8];+ for (let ring = 6; ring >= 0; ring--) { const rad = 6 + ring * 14, n = 9 + ring * 3; for (let j = 0; j < n; j++) { const a = Math.PI + lerp(-1.25, 1.35, (j + (ring % 2) * 0.5) / n), low = clamp((Math.sin(a) * -1 - 0.2) * 2.2); tuft(c, N, a + (q() - 0.5) * 0.06, rad, rad + 21, 5.2, mix(ROWS[Math.min(5, 5 - Math.min(5, ring)) ] , CREAM, low * 0.85), 0.18); } }+ // muzzle, chin and throat: pale+ blob(c, [[58, 5], [47, 1], [35, 6], [26, 16], [24, 30], [44, 32], [54, 20]]); c.fillStyle = PALE; c.fill();+ for (let j = 0; j < 9; j++) tuft(c, [50, 6], Math.PI * 0.5 + 0.25 + j * 0.16, 2, 24, 4.4, j % 2 ? CREAM : PALE, 0.14);+ c.restore();+ // crown and cheek spots, small, none on the muzzle+ for (let i = 0; i < 46; i++) { const x = -32 + q() * 56, y = -30 + q() * 54; if (Math.hypot(x - 22, y + 11) < 12 || (x > 12 && y > -4) || y > 20 - (x + 32) * 0.1) continue; spot(c, [x, y], 1.7 + q() * 1.1, 1.4 + q() * 0.8, q() * 3, DARK); }+ // the near ear, laid back by the wind: dark behind, pale inside, a fur rim+ blob(c, [[-5, -27], [-12, -44], [-27, -49], [-35, -37], [-29, -22]]); c.fillStyle = DARK; c.fill();+ for (let j = 0; j < 7; j++) tuft(c, [-14, -25], -Math.PI / 2 - 0.25 - j * 0.13, 3, 17 - Math.abs(j - 3) * 1.6, 3.2, j % 2 ? CREAM : "#e9c98a", 0.12);+ for (let j = 0; j < 9; j++) tuft(c, [-18, -31], -0.5 - j * 0.3, 13, 20, 3, j % 3 ? "#a9601f" : OCHRE, 0);+ // the tear line: from the inner corner of the eye, round the muzzle, to the corner of the mouth+ fillRib(c, ribbon([[27, -7], [30, 2], [36, 10], [37, 16]], [1.6, 3.6, 3.8, 1.4], undefined, 8), DARK);+ // the eye under its brow: lids, amber iris, pupil, the light in it+ blob(c, [[11, -10], [18, -17], [28, -15], [31, -8], [23, -5], [15, -6]]); c.fillStyle = DARK; c.fill();+ blob(c, [[14, -10], [19, -14.5], [26, -13], [28, -8.5], [22, -7], [16, -7.5]]); c.fillStyle = "#f39a1e"; c.fill();+ spot(c, [21.5, -10.5], 2.9, 3.1, 0, "#070b0e"); spot(c, [22.8, -11.8], 1, 1, 0, PALE);+ c.strokeStyle = DARK; c.lineCap = "round"; c.lineWidth = 2.4; c.beginPath(); c.moveTo(9, -15); c.quadraticCurveTo(20, -22, 31, -17); c.stroke();+ // nose pad, nostril, the mouth and the split of the lip, the chin's edge+ blob(c, [[52, -6], [58, -6], [62, 1], [60, 7], [54, 5]]); c.fillStyle = "#1a0f0c"; c.fill();+ c.strokeStyle = "rgba(255,245,222,0.4)"; c.lineWidth = 1.2; c.beginPath(); c.moveTo(54, -3); c.lineTo(59, -2); c.stroke();+ c.strokeStyle = "#1a0f0c"; c.lineWidth = 2; c.beginPath(); c.moveTo(57, 7); c.quadraticCurveTo(55, 13, 48, 14); c.quadraticCurveTo(42, 16, 36, 15); c.stroke();+ c.lineWidth = 1.2; c.globalAlpha = 0.5; c.beginPath(); c.moveTo(50, 22); c.quadraticCurveTo(40, 30, 24, 30); c.stroke(); c.globalAlpha = 1;+ for (let row = 0; row < 3; row++) for (let k = 0; k < 3; k++) spot(c, [50 - k * 5 - row, 6.5 + row * 2.6 - k * 0.6], 0.8, 0.8, 0, "#2a1a12");+ // whiskers, swept back+ c.strokeStyle = PALE; c.lineWidth = 1.1; c.globalAlpha = 0.85;+ for (let k = 0; k < 5; k++) { c.beginPath(); c.moveTo(47 - k * 2, 8 + k * 1.6); c.quadraticCurveTo(26, 14 + k * 5, -2 + k * 3, 24 + k * 7); c.stroke(); }+ c.globalAlpha = 1; c.restore();+};++/** Where her head sits on this pose: the middle of the skull. A head drawn another way (cheetahTurn's) goes here. */+export const headAnchor = (pose: Pose): P => { const end = on(ribbon(pose.spine, SPINE_UP, SPINE_DN, 14), 1, 0); return [end.p[0] + Math.cos(end.ang) * 14, end.p[1] + Math.sin(end.ang) * 14]; };+export const drawCheetah = (c: Ctx, pose: Pose, o: { head?: false } = {}) => {+ tail(c, pose.tail);+ limb(c, pose.hindF, HIND_W, 31, 0.36, true); limb(c, pose.foreF, FORE_W, 32, 0.36, false);+ // the body: its mass, then the coat in seven rows from the belly up, each row laid from the tail forward+ const r = ribbon(pose.spine, SPINE_UP, SPINE_DN, 14), L = 517, q = rng(7);+ fillRib(c, r, "#b9731f");+ c.fillStyle = "#e9d6ad"; c.beginPath(); for (let i = 0; i <= 60; i++) { const at = on(r, 0.14 + (i / 60) * 0.86, 0.98).p; if (i) c.lineTo(at[0], at[1]); else c.moveTo(at[0], at[1]); } for (let i = 60; i >= 0; i--) { const e = i / 60, at = on(r, 0.14 + e * 0.86, lerp(0.62, 0.42, e)).p; c.lineTo(at[0], at[1]); } c.closePath(); c.fill();+ const VS = [-0.84, -0.58, -0.3, 0, 0.28, 0.52, 0.74, 0.93], RC = [...ROWS, CREAM];+ for (let j = VS.length - 1; j >= 0; j--) for (let i = 0; i * 19 < L; i++) {+ const s = (i * 19 + (j % 2) * 9 + (q() - 0.5) * 7) / L; if (s > 1 || s < 0.05) continue;+ const at = on(r, s, VS[j]), k = clamp(at.w / 120, 0.55, 1), neck = clamp((s - 0.72) * 5), rump = 1 - clamp((s - 0.04) * 9), col = mix(mix(RC[j], CREAM, j >= 4 ? neck * 0.5 : 0), ROWS[2], j >= 4 ? rump : 0);+ tuft(c, at.p, at.ang + Math.PI - Math.max(0, VS[j]) * 0.22 + (q() - 0.5) * 0.1, 0, (40 + q() * 12) * k * (j >= 6 ? 0.62 : 1), 12 * k * (j >= 6 ? 0.8 : 1), j >= 6 ? mix(col, OCHRE, 0.1 + q() * 0.12) : mix(col, DARK, q() * 0.08), 0.22, j >= 5 ? 0.3 : 0.6);+ }+ // spots: solid, round, following the surface (they narrow toward the top and the belly line), none on the belly+ const sr = rng(5);+ for (let i = 0; i * 21 < L - 8; i++) for (let j = 0; j < 8; j++) {+ const v = -0.93 + j * 0.2 + (sr() - 0.5) * 0.09, s = (i * 21 + (j % 2) * 10.5 + (sr() - 0.5) * 6 + 8) / L, at = on(r, s, v), k = clamp(at.w / 130, 0.5, 1), z = (5.4 + sr() * 2.4) * k * (1 - clamp(v) * 0.25);+ spot(c, at.p, z * 1.12, z * 0.92 * Math.sqrt(1 - v * v * 0.72), at.ang, DARK);+ }+ limb(c, pose.hindN, HIND_W, 33, 0, true); limb(c, pose.foreN, FORE_W, 34, 0, false);+ if (o.head !== false) head(c, headAnchor(pose), pose.head);+};diff --git a/skills/anidoodle/engine/src/canvas-core/cheetahDusk.ts b/skills/anidoodle/engine/src/canvas-core/cheetahDusk.tsindex d3693ae..280e506 100644--- a/skills/anidoodle/engine/src/canvas-core/cheetahDusk.ts+++ b/skills/anidoodle/engine/src/canvas-core/cheetahDusk.ts@@ -101,6 +101,7 @@ const tear = (c: Ctx, side: number, prog: number) => {for (let i = 0; i <= m; i++) { const t = i / n, a = bz(t), b = bz(Math.min(1, t + 0.01)), d = Math.hypot(b[0] - a[0], b[1] - a[1]) || 1, nx = -(b[1] - a[1]) / d, ny = (b[0] - a[0]) / d, w = (7 + 15 * Math.sin(Math.PI * Math.pow(t, 0.6)) * (1 - t * 0.35)) * (i === m && prog < 1 ? 0.3 : 1); L.push([a[0] + nx * w, a[1] + ny * w]); R.push([a[0] - nx * w, a[1] - ny * w]); }c.fillStyle = DARK; c.beginPath(); [...L, ...R.reverse()].forEach(([x, y], i) => (i ? c.lineTo(x, y) : c.moveTo(x, y))); c.closePath(); c.fill();};+export const duskGrass = PARTS.grass, FACE: P = F;const grass = (c: Ctx, f: number, grow: number) => { for (const g of PARTS.grass) tuft(c, [g.x, H + 20], -Math.PI / 2 + g.a + Math.sin(f * (Math.PI / 45) + g.ph) * 0.07, 0, g.h * grow, g.w, g.col, 0); };// the night's stars: they are out while it is night, at the start and again at the end, never still@@ -108,7 +109,7 @@ const STARS = (() => { const r = rng(19); return Array.from({ length: 46 }, () =const stars = (c: Ctx, f: number, k: number) => { if (k <= 0) return; for (const st of STARS) { c.globalAlpha = k * (0.35 + 0.65 * (0.5 + 0.5 * Math.sin((f / N) * Math.PI * 2 * st.cyc + st.ph))); c.fillStyle = PALE; c.beginPath(); c.arc(st.x, st.y, st.s, 0, Math.PI * 2); c.fill(); } c.globalAlpha = 1; };// ---------------------------------------------------------------- the camera-const camAt = (f: number) => {+export const camAt = (f: number) => {const a = inOut(ramp(f, 180, 290)), b = ramp(f, 290, 338), z = Math.pow(2, lerp(0, Math.log2(1.14), a) + inOut(b) * (Math.log2(52) - Math.log2(1.14))), lk = out3(b * 1.5);return { z, look: [lerp(lerp(540, 506, a), EYE_L[0], lk), lerp(lerp(540, 532, a), EYE_L[1] - 2, lk)] as P };};@@ -122,18 +123,13 @@ const grainLayer = (env: Env): Layer => { // the paper's tooth and the dusk settreturn L;};-const draw = (ctx: Ctx, f: number, env: Env) => {- const c = ctx, s = env.scale, cam = camAt(f), pupilCol = mix(PUPIL, NIGHT, ramp(f, 312, 336));- c.setTransform(s, 0, 0, s, 0, 0); c.fillStyle = NIGHT; c.fillRect(0, 0, W, H);- if (f < 60) stars(c, f, 1 - ramp(f, 14, 52));- c.setTransform(s * cam.z, 0, 0, s * cam.z, s * (540 - cam.look[0] * cam.z), s * (540 - cam.look[1] * cam.z));- // the moon, setting; the sun, rising behind the grass with its rays opening- const set = ramp(f, 4, 52); if (set < 1) crescent(c, MOON[0] - set * set * 160, MOON[1] + set * set * 1000, MOON_R, PALE);- const rise = out3(ramp(f, 8, 74)), sy = lerp(1700, 430, rise), rays = back(ramp(f, 40, 86));- for (let j = 0; j < 96; j++) tuft(c, [540, sy], (j / 96) * Math.PI * 2 + f * 0.0016, 440, 440 + (80 + (j % 2) * 26) * rays, 12, j % 2 ? RUST : "#8f3a26", 0);- const sg = c.createRadialGradient(540, sy, 80, 540, sy, 470); sg.addColorStop(0, "#ff8a4a"); sg.addColorStop(1, CORAL); c.fillStyle = sg; c.beginPath(); c.arc(540, sy, 470, 0, Math.PI * 2); c.fill();+// The face on its own, for a caller that has set the transform: all of it, or one part. cheetahRun+// turns this head in space, so it takes the parts as layers: the neck, the head with no features (its back), the whole head.+export const duskFace = (c: Ctx, f: number, part: "all" | "neck" | "coat" | "head" = "all") => {+ const pupilCol = mix(PUPIL, NIGHT, ramp(f, 312, 336));// neck and shoulders rise- for (const n of PARTS.neck) { const g = back(ramp(f, n.t0, n.t0 + 16)); tuft(c, [n.x, n.y - 70 + (1 - g) * 60], Math.PI / 2, 0, 150 * g, 52 * g, n.col, 0.3); }+ if (part === "all" || part === "neck") for (const n of PARTS.neck) { const g = back(ramp(f, n.t0, n.t0 + 16)); tuft(c, [n.x, n.y - 70 + (1 - g) * 60], Math.PI / 2, 0, 150 * g, 52 * g, n.col, 0.3); }+ if (part === "neck") return;ear(c, [262, 300], -1, f); ear(c, [818, 300], 1, f);// the head's own mass, then the coat over it: rings of tufts from the nose outward, breathing once it is wholeconst mass = ramp(f, 70, 132); // it spreads just under the coat as the rings open, never a bare disc@@ -143,6 +139,7 @@ const draw = (ctx: Ctx, f: number, env: Env) => {for (const t of PARTS.coat) { const g = back(ramp(f, t.t0, t.t0 + 15)); if (g <= 0) continue; const br = 1 + 0.045 * alive * Math.sin(f * (Math.PI / 45) + t.ring * 0.9); tuft(c, F, t.a, t.r0, t.r0 + (t.r1 - t.r0) * g * br, t.w * Math.min(1, g), t.col); }c.restore();for (const p of PARTS.spots) { const g = back(ramp(f, p.t0, p.t0 + 9)); if (g <= 0) continue; c.fillStyle = DARK; c.beginPath(); c.ellipse(p.x, p.y, p.s * 1.12 * g, p.s * 0.94 * g, p.a, 0, Math.PI * 2); c.fill(); }+ if (part === "coat") return;// the bridge of the nose: a pale fan falling from between the eyesfor (let i = -4; i <= 4; i++) { const g = back(ramp(f, 108 + Math.abs(i) * 2, 126 + Math.abs(i) * 2)); tuft(c, [540, 452], Math.PI / 2 + i * 0.075, 20, 20 + (194 - Math.abs(i) * 12) * g, 13 * g, i % 2 ? "#f4cd7e" : "#f8dca0", 0.18); }tear(c, 1, inOut(ramp(f, 134, 162))); tear(c, -1, inOut(ramp(f, 134, 162)));@@ -160,15 +157,41 @@ const draw = (ctx: Ctx, f: number, env: Env) => {const mo = ramp(f, 152, 166); if (mo > 0) { c.strokeStyle = "#1a0f0c"; c.lineWidth = 5; c.lineCap = "round"; c.beginPath(); c.moveTo(540, 732); c.lineTo(540, 732 + 36 * mo); if (mo >= 1) { const q = ramp(f, 166, 176); c.moveTo(540, 768); c.quadraticCurveTo(540 - 28 * q, 768 + 32 * q, 540 - 70 * q, 768 + 18 * q); c.moveTo(540, 768); c.quadraticCurveTo(540 + 28 * q, 768 + 32 * q, 540 + 70 * q, 768 + 18 * q); } c.stroke(); }const wh = out3(ramp(f, 160, 184)); if (wh > 0) for (const sd of [-1, 1]) for (let k = 0; k < 5; k++) { const sway = Math.sin(f * (Math.PI / 60) + k) * 6 * alive, a: P = [540 + sd * 96, 742 + k * 9], m: P = [540 + sd * 250, 720 + k * 22 + sway * 0.5], e: P = [540 + sd * (400 + k * 14), 690 + k * 46 + sway]; c.strokeStyle = PALE; c.globalAlpha = 0.85; c.lineWidth = 2; c.beginPath(); c.moveTo(a[0], a[1]); for (let i = 1; i <= 16; i++) { const t = (i / 16) * wh, u = 1 - t; c.lineTo(u * u * a[0] + 2 * u * t * m[0] + t * t * e[0], u * u * a[1] + 2 * u * t * m[1] + t * t * e[1]); } c.stroke(); }c.globalAlpha = 1;- grass(c, f, 1);+};+// Another film may borrow this one's world (cheetahRun does): its own camera, a clock for the sun's+// rays and the grass that runs on while the face's own time is held, the face moved aside or left+// out, the grass and the grain left for the borrower to lay. With no options it is this film, exactly.+export type DuskOpts = { cam?: { z: number; look: P }; world?: number; face?: false; grass?: false; grain?: false };+export const drawDusk = (ctx: Ctx, f: number, env: Env, o: DuskOpts = {}) => {+ const c = ctx, s = env.scale, cam = o.cam ?? camAt(f), wf = o.world ?? f;+ c.setTransform(s, 0, 0, s, 0, 0); c.fillStyle = NIGHT; c.fillRect(0, 0, W, H);+ if (f < 60) stars(c, f, 1 - ramp(f, 14, 52));+ c.setTransform(s * cam.z, 0, 0, s * cam.z, s * (540 - cam.look[0] * cam.z), s * (540 - cam.look[1] * cam.z));+ // the moon, setting; the sun, rising behind the grass with its rays opening+ const set = ramp(f, 4, 52); if (set < 1) crescent(c, MOON[0] - set * set * 160, MOON[1] + set * set * 1000, MOON_R, PALE);+ const rise = out3(ramp(f, 8, 74)), sy = lerp(1700, 430, rise), rays = back(ramp(f, 40, 86));+ for (let j = 0; j < 96; j++) tuft(c, [540, sy], (j / 96) * Math.PI * 2 + wf * 0.0016, 440, 440 + (80 + (j % 2) * 26) * rays, 12, j % 2 ? RUST : "#8f3a26", 0);+ const sg = c.createRadialGradient(540, sy, 80, 540, sy, 470); sg.addColorStop(0, "#ff8a4a"); sg.addColorStop(1, CORAL); c.fillStyle = sg; c.beginPath(); c.arc(540, sy, 470, 0, Math.PI * 2); c.fill();+ if (o.face !== false) duskFace(c, f);+ if (o.grass !== false) grass(c, wf, 1);// screen space: the crescent carried out of the pupil to its place in the sky, and the grass coming backc.setTransform(s, 0, 0, s, 0, 0);if (f >= 290) { const u = ramp(f, 290, 338), e = inOut(u), from: P = [540 + (EYE_L[0] - 10 - cam.look[0]) * Math.min(cam.z, 1.6), 540 + (EYE_L[1] - 12 - cam.look[1]) * Math.min(cam.z, 1.6)]; crescent(c, lerp(from[0], MOON[0], e), lerp(from[1], MOON[1], e) - Math.sin(Math.PI * u) * 40, 9.5 * Math.pow(MOON_R / 9.5, e), PALE); }if (f >= 322) { stars(c, f, ramp(f, 326, 352)); grass(c, f, inOut(ramp(f, 322, 359))); }- c.setTransform(1, 0, 0, 1, 0, 0); c.globalAlpha = 1; c.drawImage(grainLayer(env).canvas, 0, 0);+ c.setTransform(1, 0, 0, 1, 0, 0); c.globalAlpha = 1; if (o.grain !== false) c.drawImage(grainLayer(env).canvas, 0, 0);};+const draw = (ctx: Ctx, f: number, env: Env) => drawDusk(ctx, f, env);// the score: four bars at 80 bpm are the film's 12 s exactly, so the loop is the music's own+/** The night the film ends on, held: the stars, the crescent in its place, the grass. At t = 359 it is the film's last frame, and it runs on from there. */+export const duskNight = (ctx: Ctx, t: number, env: Env) => {+ const c = ctx, s = env.scale;+ c.setTransform(s, 0, 0, s, 0, 0); c.fillStyle = NIGHT; c.fillRect(0, 0, W, H);+ crescent(c, MOON[0], MOON[1], MOON_R, PALE); stars(c, t, 1); grass(c, t, 1);+ c.setTransform(1, 0, 0, 1, 0, 0); c.globalAlpha = 1; c.drawImage(grainLayer(env).canvas, 0, 0);+};+export const DUSK_PALE = PALE;+const audio = (sr: number): [Float32Array, Float32Array] => {const n = Math.round((N / 30) * sr), L = new Float32Array(n), R = new Float32Array(n), m = renderLoop(cheetahDuskScore(), sr); // its tail folded back onto its start: no cut at the seamfor (let i = 0; i < n && i < m.L.length; i++) { L[i] = m.L[i]; R[i] = m.R[i]; }diff --git a/skills/anidoodle/engine/src/canvas-core/cheetahRig.ts b/skills/anidoodle/engine/src/canvas-core/cheetahRig.tsnew file mode 100644index 0000000..21a61c4--- /dev/null+++ b/skills/anidoodle/engine/src/canvas-core/cheetahRig.ts@@ -0,0 +1,91 @@+// THE CHEETAH'S SKELETON. A rig is a set of angles; fk() turns it into the points cheetahBody draws.+// Bones never change length, so a leg swings in arcs and folds at its joints, it never stretches.+// Angles are in degrees, absolute, y down: 0 points forward (the animal faces right), 90 down, 180 back.+// The gallop is the rotary gallop of the reference: two flight phases a stride, the spine folding+// (gathered, hind feet reaching past the fore) and opening (extended), the forelegs landing one+// after the other, then the hind legs. The head is held level at its own height whatever the body does.+import type { P } from "./core";+import type { Pose } from "./cheetahBody";++type Leg = [number, number, number, number];+export type Rig = {+ pitch: number; lumb: number; thor: number; // the body's tilt (nose down is positive); how far the loin and the chest fold+ headH: number; head: number; // the head's height above the ground; its own angle (nose down is positive)+ fore: Leg; foreF: Leg; // scapula, upper arm, forearm, hand+ hind: Leg; hindF: Leg; // thigh, shank, metatarsus, toes+ tail: number[]; // its five segments+};++const D = Math.PI / 180, clamp = (v: number, a = 0, b = 1) => Math.max(a, Math.min(b, v)), lerp = (a: number, b: number, t: number) => a + (b - a) * t;+const step = (p: P, deg: number, len: number): P => [p[0] + Math.cos(deg * D) * len, p[1] + Math.sin(deg * D) * len];+const off = (p: P, deg: number, x: number, y: number): P => { const c = Math.cos(deg * D), s = Math.sin(deg * D); return [p[0] + x * c - y * s, p[1] + x * s + y * c]; };+export const FORE_BONES = [54, 98, 103, 60], HIND_BONES = [102, 105, 79, 34], TAIL_BONES = [65, 66, 66, 59, 43];+const REAR = [65.1, 75.3, 43.3], FRONT = [65.3, 70, 60.3], NECK = 138; // spine segments out from the waist; the neck's chord+const chain = (p: P, dirs: number[], bones: number[]): P[] => { const out: P[] = [p]; dirs.forEach((d, i) => out.push(step(out[i], d, bones[i]))); return out; };++/** The points, with the waist at (x, y) and the ground at `ground`. */+export const fk = (r: Rig, x: number, y: number, ground: number): Pose => {+ const waist: P = [x, y], p = r.pitch;+ const fwd = [p + 5 + r.thor * 0.3, p + 2 + r.thor * 0.7, p - 5 + r.thor], back = [p + 3.5 - r.lumb * 0.3, p + 5 - r.lumb * 0.75, p + 7 - r.lumb * 1.1];+ const ribs = step(waist, fwd[0], FRONT[0]), chest = step(ribs, fwd[1], FRONT[1]), shoulder = step(chest, fwd[2], FRONT[2]);+ const loin = step(waist, back[0] + 180, REAR[0]), hip = step(loin, back[1] + 180, REAR[1]), rump = step(hip, back[2] + 180, REAR[2]);+ // the neck reaches whatever is left between the shoulders and a head that keeps its own height+ const dy = clamp(ground - r.headH - shoulder[1], -NECK * 0.82, NECK * 0.82), dx = Math.sqrt(NECK * NECK - dy * dy), join: P = [shoulder[0] + dx, shoulder[1] + dy];+ const nx = dy / NECK, ny = -dx / NECK, bow = (t: number, b: number): P => [lerp(shoulder[0], join[0], t) + nx * b, lerp(shoulder[1], join[1], t) + ny * b];+ return {+ spine: [rump, hip, loin, waist, ribs, chest, shoulder, bow(0.38, 7), bow(0.73, 5), join],+ tail: chain(off(rump, back[2], 4, -14), r.tail, TAIL_BONES),+ foreN: chain(off(shoulder, fwd[2], -10, -24), r.fore, FORE_BONES), foreF: chain(off(shoulder, fwd[2], -2, -20), r.foreF, FORE_BONES),+ hindN: chain(off(hip, back[1], 7, 15), r.hind, HIND_BONES), hindF: chain(off(hip, back[1], 15, 17), r.hindF, HIND_BONES),+ head: r.head * D,+ };+};++const low = (leg: P[]) => Math.max(leg[3][1], leg[4][1]) + 5; // a foot's sole: the lower of its ball and its toes, plus the pad+export const lowest = (q: Pose) => ({ fore: Math.max(low(q.foreN), low(q.foreF)), hind: Math.max(low(q.hindN), low(q.hindF)) });+const blend = (a: number[], b: number[], t: number) => a.map((v, i) => lerp(v, b[i], t));+export const mixRig = (a: Rig, b: Rig, t: number): Rig => ({+ pitch: lerp(a.pitch, b.pitch, t), lumb: lerp(a.lumb, b.lumb, t), thor: lerp(a.thor, b.thor, t), headH: lerp(a.headH, b.headH, t), head: lerp(a.head, b.head, t),+ fore: blend(a.fore, b.fore, t) as Leg, foreF: blend(a.foreF, b.foreF, t) as Leg, hind: blend(a.hind, b.hind, t) as Leg, hindF: blend(a.hindF, b.hindF, t) as Leg, tail: blend(a.tail, b.tail, t),+});++/** A standing pose settles onto the ground: the body tilts until fore and hind feet both bear, and its height follows. */+export const settle = (r: Rig): { rig: Rig; y: number } => {+ let rig = r;+ for (let k = 0; k < 8; k++) { const q = fk(rig, 0, 0, 0), l = lowest(q); rig = { ...rig, pitch: rig.pitch + (Math.atan2(l.hind - l.fore, q.foreN[4][0] - q.hindN[4][0]) / D) * 0.9 }; }+ const l = lowest(fk(rig, 0, 0, 0)); return { rig, y: -Math.max(l.fore, l.hind) };+};++// ---------------------------------------------------------------- the held poses+export const STAND: Rig = { pitch: 0, lumb: 0, thor: 0, headH: 356, head: 6, fore: [80, 118, 88, 68], foreF: [76, 108, 92, 72], hind: [58, 128, 86, 18], hindF: [66, 134, 90, 20], tail: [118, 100, 98, 122, 160] }; // the tail at rest: down behind the hocks, its tip turned up+// the bow: forelegs walked out flat along the ground, chest down between them, the rump left high, the back hollow+export const BOW: Rig = { pitch: 0, lumb: -22, thor: -12, headH: 150, head: -14, fore: [52, 38, 8, 2], foreF: [50, 44, 12, 4], hind: [78, 112, 88, 14], hindF: [84, 118, 92, 16], tail: [250, 262, 285, 318, 350] };+// the crouch before the start: weight back over folded hind legs, the chest low, the head stretched forward+export const CROUCH: Rig = { pitch: 0, lumb: 16, thor: 6, headH: 196, head: 10, fore: [88, 150, 62, 50], foreF: [84, 142, 68, 54], hind: [36, 142, 72, 12], hindF: [44, 148, 78, 14], tail: [176, 170, 176, 190, 204] };+// the stop: forelegs braced out ahead, the rump dropped under, the tail thrown up for balance+export const SKID: Rig = { pitch: 0, lumb: 20, thor: 2, headH: 262, head: -4, fore: [62, 62, 56, 30], foreF: [60, 70, 62, 34], hind: [30, 138, 66, 10], hindF: [40, 146, 74, 12], tail: [228, 246, 262, 280, 300] };++// ---------------------------------------------------------------- the gallop+// One stride. Keys: 0 extended flight · .14 the forefeet reach the ground · .27 over the forelegs ·+// .40 they push off · .52 gathered flight · .66 the hind feet land ahead · .80 over the hind legs · .92 they drive off.+const T = [0, 0.14, 0.27, 0.4, 0.52, 0.66, 0.8, 0.92];+const FORE = [[70, 23, 15, 15], [78, 55, 50, 35], [92, 118, 78, 45], [105, 140, 125, 140], [105, 135, 172, 225], [95, 110, 150, 215], [82, 60, 95, 150], [72, 28, 30, 50]];+const HIND = [[141, 187, 164, 159], [128, 185, 150, 150], [100, 170, 125, 135], [60, 176, 122, 122], [22, 146, 56, 30], [40, 128, 58, 8], [88, 140, 85, 10], [122, 168, 135, 100]];+// lumb thor pitch tail height of the waist in flight+const BODY = [[-12, -4, 0, 188, 262], [-8, -1, 3, 180, 266], [6, 5, 4, 176, 286], [36, 13, 2, 190, 292], [64, 22, 0, 212, 300], [54, 17, -3, 218, 296], [20, 5, -4, 204, 288], [-6, -3, -2, 194, 272]];+const TAIL_CURL = [0, -10, 4, 20, 34];+const cyc = (V: number[][], ph: number): number[] => {+ const n = T.length, f = ((ph % 1) + 1) % 1; let i = n - 1; for (let k = 0; k < n; k++) if (f >= T[k]) i = k;+ const tt = (k: number) => T[((k % n) + n) % n] + Math.floor(k / n), vv = (k: number) => V[((k % n) + n) % n], t1 = tt(i), t2 = tt(i + 1), h = t2 - t1, u = (f - t1) / h, u2 = u * u, u3 = u2 * u;+ return vv(i).map((v1, c) => { const v2 = vv(i + 1)[c], m1 = ((v2 - vv(i - 1)[c]) / (t2 - tt(i - 1))) * h, m2 = ((vv(i + 2)[c] - v1) / (tt(i + 2) - t1)) * h; return (2 * u3 - 3 * u2 + 1) * v1 + (u3 - 2 * u2 + u) * m1 + (-2 * u3 + 3 * u2) * v2 + (u3 - u2) * m2; });+};+// the far legs run the same stride a little later: one forefoot lands, then the other+const FAR_FORE = 0.085, FAR_HIND = 0.07;+export const gallopRig = (ph: number): Rig => {+ const b = cyc(BODY, ph);+ return { pitch: b[2], lumb: b[0], thor: b[1], headH: 300, head: 5 + b[2] * 0.3, fore: cyc(FORE, ph) as Leg, foreF: cyc(FORE, ph - FAR_FORE) as Leg, hind: cyc(HIND, ph) as Leg, hindF: cyc(HIND, ph - FAR_HIND) as Leg, tail: TAIL_CURL.map((k, i) => { const a = cyc(BODY, ph - 0.09 * i)[3]; return 196 + (a - 196) * (1 + i * 0.18) + k; }) }; // each segment follows the one before it, later and wider: the whip of the tail+};+// how firmly the feet are on the ground through the stride: two stances, two flights+const stance = (ph: number) => { const f = ((ph % 1) + 1) % 1, bump = (c: number, w: number) => { const d = Math.min(Math.abs(f - c), 1 - Math.abs(f - c)); return clamp(1 - d / w); }; const s = Math.max(bump(0.27, 0.15), bump(0.8, 0.15)); return s * s * (3 - 2 * s); };+/** The waist's height (negative, above the ground at 0) at this point of the stride: on its feet in the stances, carried through the air between. */+export const gallopY = (ph: number, rig = gallopRig(ph)): number => { const l = lowest(fk(rig, 0, 0, 0)), contact = -Math.max(l.fore, l.hind), air = Math.min(contact, -cyc(BODY, ph)[4]); return lerp(air, contact, stance(ph)); };diff --git a/skills/anidoodle/engine/src/canvas-core/cheetahRun.ts b/skills/anidoodle/engine/src/canvas-core/cheetahRun.tsnew file mode 100644index 0000000..7ce27d5--- /dev/null+++ b/skills/anidoodle/engine/src/canvas-core/cheetahRun.ts@@ -0,0 +1,211 @@+// CHEETAH, RUN. 27 s, looping. It is cheetahDusk with a run opened up in the middle of it, in one+// continuous shot with no cut and no wipe.+// The story in three sentences: She wakes at dusk. She stretches and runs across the whole country+// for the joy of it. She stops, turns back to us, and the night closes over, ready to begin again.+// The token: the low sun, which stays put through every move while everything nearer goes by.+// The two moves the film is for:+// THE TURN. The camera pulls back from the face and comes round to her side in one move. The face+// is the same drawing, wrapped on a head and turned in space (cheetahTurn); her body swings out+// from behind it, edge-on at first, as the ground comes up under her feet. Back the same way.+// THE RUN. She is held in frame and the country goes by in layers at their own speeds, and it+// changes as she covers ground: thorn savanna, a kopje where the birds go up, a shallow pan she+// goes through in spray, grass again (cheetahScenery).+// The picture is timed in frames on a 120 bpm grid (a beat is 15 frames, and so is a stride); the+// score is nine bars at 80 bpm, the same 27 s.+// 0-255 cheetahDusk 0-255: night, the sun, the face making itself, the eyes, alive, a blink+// 255-300 the turn: front to side, pulling back+// 300-390 the stretch: down into the bow (338), held and deepened, up again (390)+// 390-405 she gathers+// 405-575 the run: off the hind legs at 405, at speed by 450, a stride every 15 frames+// 575-630 forefeet braced, the slide, the dust goes past her, she stands+// 648-690 the turn back+// 690-810 cheetahDusk 262-360: the face, the camera into the eye, the night. 809 hands over to 0.+import { rng, type Ctx, type Env, type Layer, type P } from "./core";+import type { Film } from "./film";+import { mix } from "./gallery";+import { drawCheetah, headAnchor, type Pose } from "./cheetahBody";+import { camAt, drawDusk, DUSK_PALE, duskGrass, duskNight, FACE } from "./cheetahDusk";+import { logoCentred, setType } from "./kinetic";+import { cheetahRunScore, cheetahRunSignedScore } from "./music/pieces/cheetahRun";+import { renderLoop, renderPiece } from "./music/render";+import { BOW, CROUCH, fk, gallopRig, gallopY, lowest, mixRig, settle, SKID, STAND, type Rig } from "./cheetahRig";+import { country, drawBurst, drawFar, drawFlock, drawLens, drawPan, inPan, leaf, type Burst } from "./cheetahScenery";+import { HEAD_K, turnedHead, turnedNeck } from "./cheetahTurn";++const W = 1080, H = 1080, N = 810, GROUND = 852, FLOOR = GROUND + 24;+const NIGHT = "#0d1a22", TEAL = "#2a6a70", RUST = "#b8452a", PALE = "#fff5de", DARK = "#0a1216";+const clamp = (v: number, a = 0, b = 1) => Math.max(a, Math.min(b, v));+const ramp = (f: number, a: number, b: number) => clamp((f - a) / (b - a));+const lerp = (a: number, b: number, t: number) => a + (b - a) * t;+const inOut = (t: number) => { const c = clamp(t); return c < 0.5 ? 4 * c * c * c : 1 - Math.pow(-2 * c + 2, 3) / 2; };+const out3 = (t: number) => 1 - Math.pow(1 - clamp(t), 3);+const smooth = (t: number) => { const c = clamp(t); return c * c * (3 - 2 * c); };+const back = (t: number) => { const c = clamp(t) - 1; return 1 + 2.4 * c * c * c + 1.4 * c * c; };++// ---------------------------------------------------------------- the cue table (frames)+const CUE = { swing: 255, side: 300, bow: 338, hold: 362, up: 390, go: 405, speed: 450, brake: 575, slid: 606, stood: 630, swingBack: 648, face: 690, eye: 720 } as const;+const STRIDE = 15, PH0 = 0.82; // a stride in frames; where in the stride she leaves the ground at CUE.go+const phase = (F: number) => PH0 + (F - CUE.go) / STRIDE;+// how fast the ground goes by (0-1), and how far it has gone. The far total is chosen so the near+// grass, which is cheetahDusk's own, is back exactly where that film left it when she stops.+const pace = (F: number) => smooth(ramp(F, CUE.go, CUE.speed)) * (1 - smooth(ramp(F, CUE.brake - 12, CUE.slid)));+const NEAR = 1.6, WRAP = 1400, TOP = 84; // the near grass's depth factor and its wrap; her top speed in px of ground per frame+const TRAVEL = (() => { const sub = 8, acc = [0]; for (let i = 1; i <= N * sub; i++) acc.push(acc[i - 1] + pace((i - 0.5) / sub) / sub); const total = acc[acc.length - 1], want = (Math.round((total * TOP * NEAR) / WRAP) * WRAP) / NEAR, k = want / (total * TOP); return (F: number) => { const x = clamp(F, 0, N) * sub, i = Math.min(N * sub - 1, Math.floor(x)); return lerp(acc[i], acc[i + 1], x - i) * TOP * k; }; })(); // px of ground gone by+const LAND = country(TRAVEL(N));+const frameAt = (g: number) => { let a: number = CUE.go, b: number = CUE.slid; for (let i = 0; i < 24; i++) { const m = (a + b) / 2; if (TRAVEL(m) < g) a = m; else b = m; } return b; };++// ---------------------------------------------------------------- her performance+const S = { stand: settle(STAND), bow: settle(BOW), crouch: settle(CROUCH), skid: settle(SKID) };+const HIND_X = 400, TRACK_X = 548; // where her hind foot stands on screen; where the camera holds her waist as she runs+const idle = (r: Rig, F: number): Rig => ({ ...r, headH: r.headH + Math.sin(F * (Math.PI / 45)) * 2.5, tail: r.tail.map((a, i) => a + Math.sin(F * (Math.PI / 30) - i * 0.7) * (2 + i * 2.2)) });+const held = (F: number): Rig => { // every pose she holds, and the moves between them+ if (F < CUE.side) return S.stand.rig;+ if (F < CUE.bow) return mixRig(S.stand.rig, S.bow.rig, inOut(ramp(F, CUE.side, CUE.bow)));+ if (F < CUE.hold) { const d = Math.sin(ramp(F, CUE.bow, CUE.hold) * Math.PI), q = Math.sin(F * 1.9) * d; return { ...S.bow.rig, lumb: S.bow.rig.lumb - 7 * d, headH: S.bow.rig.headH + 14 * d, head: S.bow.rig.head - 6 * d, tail: S.bow.rig.tail.map((a, i) => a + q * i * 1.6 + d * i * 3) }; }+ if (F < CUE.up) return mixRig(S.bow.rig, S.stand.rig, back(ramp(F, CUE.hold, CUE.up)));+ if (F < CUE.go) return mixRig(S.stand.rig, S.crouch.rig, inOut(ramp(F, CUE.up, CUE.go)));+ if (F < CUE.slid) { const d = Math.sin(ramp(F, CUE.brake + 6, CUE.slid + 10) * Math.PI); return { ...S.skid.rig, lumb: S.skid.rig.lumb + 9 * d, headH: S.skid.rig.headH - 20 * d, head: S.skid.rig.head + 5 * d, tail: S.skid.rig.tail.map((a, i) => a + d * i * 5 + Math.sin(F * 0.9) * i * 1.5 * d) }; }+ return mixRig(S.skid.rig, S.stand.rig, back(ramp(F, CUE.slid, CUE.stood)));+};+const standing = (F: number) => { const s = settle(idle(held(F), F)), q = fk(s.rig, 0, s.y, 0); return { rig: s.rig, y: s.y, x: HIND_X - q.hindN[4][0] }; };+const lead = (F: number) => 46 * Math.sin(ramp(F, CUE.go, CUE.speed + 30) * Math.PI) - 40 * Math.sin(ramp(F, CUE.brake - 6, CUE.stood) * Math.PI);+const poseAt = (F: number): Pose => {+ if (F < CUE.go || F >= CUE.slid) { const s = standing(F); return fk(s.rig, s.x + (F >= CUE.slid ? lerp(60, 0, smooth(ramp(F, CUE.slid, CUE.stood + 20))) + lead(F) : 0), s.y + FLOOR, FLOOR); }+ // running: the stride, taken up out of the crouch and given back to the braced stop+ const ph = phase(F), g = gallopRig(ph), a = standing(CUE.go - 0.001), b = standing(CUE.slid), wIn = out3(ramp(F, CUE.go, CUE.go + 9)), wOut = inOut(ramp(F, CUE.brake, CUE.brake + 13));+ let rig = mixRig(a.rig, g, wIn), y = lerp(a.y, gallopY(ph, g), wIn), x = lerp(a.x, TRACK_X, smooth(ramp(F, CUE.go, CUE.go + 24)));+ if (wOut > 0) { rig = mixRig(rig, b.rig, wOut); y = lerp(y, b.y, wOut); x = lerp(x, b.x + 60, wOut); }+ const l = lowest(fk(rig, 0, y, 0)), sink = Math.max(l.fore, l.hind); if (sink > 0) y -= sink; // never through the ground+ return fk(rig, x + lead(F), y + FLOOR, FLOOR);+};+// what her feet throw up, each where it was made on the ground: a kick at every footfall, a long cloud from the braced stop+const BURSTS = (() => {+ const out: (Burst & { born: number; g: number })[] = [];+ const add = (born: number, x: number, big: number, seed: number) => { const g = TRAVEL(born) + (x - 540); out.push({ born, g, x, y: FLOOR, age: 0, seed, wet: inPan(LAND, g), big }); };+ for (let n = 0; n < 13; n++) for (const [ph, fore] of [[0.2, true], [0.68, false]] as const) { const born = CUE.go + (n + ph - PH0) * STRIDE; if (born < CUE.go + 2 || born > CUE.brake + 2) continue; const q = poseAt(born); add(born, fore ? q.foreN[4][0] : q.hindN[4][0], fore ? 0.85 : 1.25, 300 + n * 2 + (fore ? 1 : 0)); }+ for (let i = 0; i < 9; i++) { const born = CUE.brake + 3 + i * 3.5, q = poseAt(born); add(born, q.foreN[4][0] + 10, 1.9 - i * 0.08, 500 + i); }+ return out;+})();+const bursts = (c: Ctx, F: number, wet: boolean) => { const X = TRAVEL(F); for (const b of BURSTS) if (b.wet === wet && F >= b.born && F < b.born + 20) drawBurst(c, { ...b, x: b.x - (X - TRAVEL(b.born)) * (wet ? 1 : 0.92), age: F - b.born }); };+// the frame takes a small knock as each pair of feet comes down+const knock = (F: number) => { if (F < CUE.go || F > CUE.brake + 20) return 0; const ph = (((phase(F) % 1) + 1) % 1), since = Math.min((ph - 0.2 + 1) % 1, (ph - 0.68 + 1) % 1) * STRIDE; return 4.5 * pace(F) * Math.exp(-since / 2.4) * Math.cos(since * 1.9); };++// ---------------------------------------------------------------- the world+// cheetahDusk's blade, mark for mark, so the grass is one grass across both seams+const blade = (c: Ctx, o: P, a: number, len: number, w: number, col: string) => {+ if (len < 1.5 || w < 0.6) return;+ const ca = Math.cos(a), sa = Math.sin(a), t: P = [o[0] + ca * len, o[1] + sa * len], m = len * 0.42, mx = o[0] + ca * m, my = o[1] + sa * m;+ const g = c.createLinearGradient(o[0], o[1], t[0], t[1]); g.addColorStop(0, mix(col, DARK, 0.6)); g.addColorStop(0.45, col); g.addColorStop(1, mix(col, "#ffffff", 0.16));+ c.fillStyle = g; c.beginPath(); c.moveTo(o[0], o[1]); c.quadraticCurveTo(mx - sa * w, my + ca * w, t[0], t[1]); c.quadraticCurveTo(mx + sa * w, my - ca * w, o[0], o[1]); c.fill();+};+const wrap = (x: number, span: number, lo: number) => ((((x - lo) % span) + span) % span) + lo;+const SCENE = (() => {+ const q = rng(3);+ // the near grass: cheetahDusk's seventy blades where that film has them, and more beyond its frame for when the camera is further back+ const more = rng(43), near = [...duskGrass, ...Array.from({ length: 22 }, (_, i) => ({ x: i % 2 ? -160 + more() * 160 : W + more() * 160, h: 90 + more() * 220, a: (more() - 0.5) * 0.5, w: 9 + more() * 8, col: more() < 0.15 ? TEAL : mix(NIGHT, TEAL, more() * 0.5), ph: more() * 6.28 }))];+ const far = Array.from({ length: 150 }, () => ({ x: q() * 1320, h: 26 + q() * 60, w: 5 + q() * 5, col: mix(NIGHT, TEAL, q() * 0.55), dy: q() * 10, a: (q() - 0.5) * 0.5, ph: q() * 6.28 }));+ const streaks = Array.from({ length: 34 }, () => ({ x: q() * 2200, y: 700 + q() * 150, len: 140 + q() * 420, w: 2.2 + q() * 4.5, col: mix(RUST, NIGHT, 0.35 + q() * 0.5), al: 0.3 + q() * 0.3 }));+ const flecks = Array.from({ length: 14 }, () => ({ x: q() * 2600, y: 880 + q() * 180, len: 200 + q() * 300, w: 1.4 + q() * 1.6, al: 0.1 + q() * 0.14 }));+ return { near, far, streaks, flecks };+})();+const SIDE_CAM = { z: 410 / 470, look: [540, 430 - 8 / (410 / 470)] as P }; // the sun where the look still has it: (540, 548), radius 410+const PIVOT: P = [540, FLOOR - 130];+// a camera of its own for the side: in on her for the stretch, back out to give the run its room, in a little as she flies+const push = (F: number) => 1 + 0.26 * inOut(ramp(F, CUE.side, CUE.bow)) * (1 - inOut(ramp(F, CUE.up - 6, CUE.go + 22))) + 0.07 * smooth(ramp(F, CUE.speed, CUE.speed + 45)) * (1 - smooth(ramp(F, CUE.brake - 30, CUE.brake))) + 0.12 * inOut(ramp(F, CUE.slid, CUE.stood + 6)) * (1 - inOut(ramp(F, CUE.swingBack - 18, CUE.swingBack)));++const grainLayer = (env: Env): Layer => { // the paper's tooth and the dusk settling at the edges: cheetahDusk's, grain for grain+ const key = "cheetahRun:grain"; let L = env.cache.get(key) as Layer | undefined; if (L) return L;+ L = env.canvas(Math.round(W * env.scale), Math.round(H * env.scale)); env.cache.set(key, L);+ const c = L.ctx, q = rng(77); c.setTransform(env.scale, 0, 0, env.scale, 0, 0);+ for (let i = 0; i < 200000; i++) { const x = q() * W, y = q() * H, d = Math.hypot(x - 540, y - 520) / 740; if (q() > d * d * d * 1.1) continue; c.fillStyle = DARK; c.globalAlpha = 0.42; c.fillRect(x, y, 1.1, 1.1); }+ for (let i = 0; i < 30000; i++) { const x = q() * W, y = q() * H, lt = q() < 0.5; c.fillStyle = lt ? "#fff5de" : "#000000"; c.globalAlpha = lt ? 0.06 : 0.08; c.fillRect(x, y, 1.1, 1.1); }+ return L;+};++const side = (ctx: Ctx, F: number, env: Env) => {+ const c = ctx, s = env.scale, out = F < CUE.swingBack, w = out ? ramp(F, CUE.swing, CUE.side) : 1 - ramp(F, CUE.swingBack, CUE.face), e = inOut(w), turning = w < 1;+ // cheetahDusk's world through a camera that pulls back as it comes round: its sun stays, far off+ const tau = out ? lerp(255, 262, ramp(F, CUE.swing, CUE.side)) : 262, d = camAt(tau), Z = push(F), zz = Math.exp(lerp(Math.log(d.z), Math.log(SIDE_CAM.z), e)) * Z;+ const cam = { z: zz, look: [lerp(d.look[0], SIDE_CAM.look[0], e) + ((PIVOT[0] - 540) * (Z - 1)) / zz, lerp(d.look[1], SIDE_CAM.look[1], e) + ((PIVOT[1] - 540) * (Z - 1)) / zz] as P };+ drawDusk(c, tau, env, { cam, world: F, face: false, grass: false, grain: false });+ // her, and how the turn places her: the head's centre on screen, its size, how far round we are+ const pose = poseAt(F), anchor = headAnchor(pose), yaw = (Math.PI / 2) * e, k0 = d.z, k = Math.exp(lerp(Math.log(k0), Math.log(HEAD_K), e)), kb = k / HEAD_K, sx = Math.sin(yaw);+ const P0: P = [(FACE[0] - d.look[0]) * d.z + 540, (FACE[1] - d.look[1]) * d.z + 540], hc: P = [lerp(P0[0], anchor[0], e), lerp(P0[1], anchor[1], e)];+ // the ground comes up under her as the camera pulls back: everything that stands on it rides on this line+ const drop = turning ? hc[1] + (FLOOR - anchor[1]) * kb - FLOOR : 0, shake = knock(F);+ const screen = () => c.setTransform(s * Z, 0, 0, s * Z, s * PIVOT[0] * (1 - Z), s * (PIVOT[1] * (1 - Z) + drop + shake));+ const v = pace(F), X = TRAVEL(F), lean = smooth(v);+ if (drop < 1400) {+ screen();+ drawFar(c, LAND, X, F, GROUND, lean);+ drawFlock(c, LAND, X, F, GROUND, frameAt);+ for (const g of SCENE.streaks) leaf(c, wrap(g.x - X * 0.8, 2200, -500) + g.len, g.y, Math.PI, g.len * (0.4 + 0.6 * v), g.w, g.col, g.al * v);+ c.fillStyle = mix(NIGHT, DARK, 0.35); c.fillRect(-40, GROUND, W + 80, H - GROUND + 400);+ for (const g of SCENE.far) leaf(c, wrap(g.x - X, 1320, -120), GROUND + 6 + g.dy, -Math.PI / 2 + g.a * (1 - lean) + Math.sin(F * (Math.PI / 45) + g.ph) * 0.06 * (1 - lean) - lean * 1.1, g.h * (1 + lean * 0.35), g.w, g.col);+ drawPan(c, LAND, X, F, GROUND, v);+ }+ const SWAP = 0.64; // past this point of the turn the head is small and nearly side-on, and the profile head takes over+ // the chest under the face goes first: it is a front, and a front narrows away and sinks behind her as we come round+ if (turning && yaw < 0.36) { c.save(); c.setTransform(s, 0, 0, s, 0, 0); c.beginPath(); c.rect(-10, -10, hc[0] + 240 * k * Math.cos(yaw) + 10, H + 20); c.clip(); turnedNeck(c, env, out ? 255 : 262, yaw, hc[0], hc[1] + (1 - Math.cos(yaw)) * 1500 * k, k); c.restore(); }+ if (sx > 0.03) {+ // her body: edge-on behind the face at first, its full length by the time we are beside her+ if (turning) c.setTransform(s * kb * sx, 0, 0, s * kb, s * (hc[0] - anchor[0] * kb * sx), s * (hc[1] - anchor[1] * kb)); else screen();+ const l = lowest(pose), air = clamp((FLOOR - Math.max(l.fore, l.hind)) / 60), x0 = pose.spine[0][0], x1 = pose.spine[9][0];+ for (let i = 0; i < 5; i++) { c.fillStyle = DARK; c.globalAlpha = 0.2 * (1 - air * 0.45); c.beginPath(); c.ellipse((x0 + x1) / 2 + 20, FLOOR + 8, ((x1 - x0) * 0.62 + 40) * (1 - i * 0.11) * (1 - air * 0.12), 15 - i * 2.1, 0, 0, Math.PI * 2); c.fill(); }+ c.globalAlpha = 1;+ bursts(c, F, false);+ drawCheetah(c, pose, turning && w < SWAP ? { head: false } : {});+ bursts(c, F, true);+ }+ if (turning && w < SWAP) turnedHead(c, env, out ? 255 : 262, yaw, hc[0], hc[1], k);+ // the near grass, in cheetahDusk's own camera: its blades, swept over and streaming while she runs+ c.setTransform(s * cam.z, 0, 0, s * cam.z, s * (540 - cam.look[0] * cam.z), s * (540 - cam.look[1] * cam.z));+ for (const g of SCENE.near) blade(c, [wrap(g.x - X * NEAR, WRAP, -160), H + 20], -Math.PI / 2 + g.a * (1 - lean * 0.8) + Math.sin(F * (Math.PI / 45) + g.ph) * 0.07 - lean * 1.02, g.h * (1 + lean * 0.45), g.w, g.col);+ screen();+ drawLens(c, LAND, X, H);+ for (const g of SCENE.flecks) leaf(c, wrap(g.x - X * 2.1, 2600, -600) + g.len, g.y, Math.PI, g.len, g.w, PALE, g.al * v);+ c.setTransform(1, 0, 0, 1, 0, 0); c.globalAlpha = 1; c.drawImage(grainLayer(env).canvas, 0, 0);+};++const draw = (ctx: Ctx, F: number, env: Env) => {+ if (F < CUE.swing) return drawDusk(ctx, F, env);+ if (F < CUE.face) return side(ctx, F, env);+ drawDusk(ctx, F < CUE.eye ? lerp(262, 270, ramp(F, CUE.face, CUE.eye)) : F - 450, env, { world: F });+};++// the score: nine bars at 80 bpm are the film's 27 s exactly, so the loop is the music's own+const audio = (sr: number): [Float32Array, Float32Array] => {+ const n = Math.round((N / 30) * sr), L = new Float32Array(n), R = new Float32Array(n), m = renderLoop(cheetahRunScore(), sr); // its tail folded back onto its start: no cut at the seam+ for (let i = 0; i < n && i < m.L.length; i++) { L[i] = m.L[i]; R[i] = m.R[i]; }+ return [L, R];+};++export const cheetahRun: Film = {+ meta: { title: "Cheetah, run", W, H, fps: 30, bpm: 120, durationFrames: N, raster: "cpu", kind: "loop", poster: 500, score: { tempo: 80, form: "9 bars of 12/8, looping: drone and cell, the cycle enters, one long note, held breath and go, the cell in her stride, the stop, home" }, sync: [{ frame: 405, label: "she goes" }, { frame: 575, label: "forefeet braced" }] },+ assets: { images: {} },+ shots: [{ id: "run", start: 0, end: N, draw }],+ audio: Object.assign(audio, { scores: [cheetahRunScore()] }),+};++// ---------------------------------------------------------------- the signed cut, for posting+// The loop played once, then four seconds on the night it ends in: the wordmark is written in the+// sky in the moon's own white, its dot landing on the score's last note, and the address is set+// under it. The stars and the grass keep moving; nothing else is added to the picture.+const END = N + 120, SIGN = { write: N + 10, dot: N + 38, url: N + 48, set: N + 66 };+const signed = (ctx: Ctx, F: number, env: Env) => {+ if (F < N) return draw(ctx, F, env);+ duskNight(ctx, F - 450, env);+ logoCentred(ctx, env, 540, 452, 92, ramp(F, SIGN.write, SIGN.write + (SIGN.dot - SIGN.write) / 0.9), { color: DUSK_PALE, pen: false });+ setType(ctx, env, "github.com/alexgreensh/anidoodle", 540, 628, 34, ramp(F, SIGN.url, SIGN.set), { color: DUSK_PALE, align: "center", weight: 600, track: 0.01 });+};+const signedAudio = (sr: number): [Float32Array, Float32Array] => {+ const n = Math.round((END / 30) * sr), L = new Float32Array(n), R = new Float32Array(n), m = renderPiece(cheetahRunSignedScore(), sr);+ for (let i = 0; i < n && i < m.L.length; i++) { L[i] = m.L[i]; R[i] = m.R[i]; }+ return [L, R];+};+export const cheetahRunSigned: Film = {+ meta: { title: "Cheetah, run (signed)", W, H, fps: 30, bpm: 120, durationFrames: END, raster: "cpu", kind: "story", poster: 500, score: { tempo: 80, form: "the nine bars once, then a tenth that closes them: the cell lands on the tonic and rings" }, sync: [{ frame: 405, label: "she goes" }, { frame: 575, label: "forefeet braced" }] },+ assets: { images: {} },+ shots: [{ id: "run", start: 0, end: END, draw: signed }],+ audio: Object.assign(signedAudio, { scores: [cheetahRunSignedScore()] }),+};diff --git a/skills/anidoodle/engine/src/canvas-core/cheetahRunSigned.ts b/skills/anidoodle/engine/src/canvas-core/cheetahRunSigned.tsnew file mode 100644index 0000000..188015c--- /dev/null+++ b/skills/anidoodle/engine/src/canvas-core/cheetahRunSigned.ts@@ -0,0 +1,2 @@+// The signed cut of "Cheetah, run", for posting: the film once, then the wordmark and the address on the night it ends in.+export { cheetahRunSigned } from "./cheetahRun";diff --git a/skills/anidoodle/engine/src/canvas-core/cheetahScenery.ts b/skills/anidoodle/engine/src/canvas-core/cheetahScenery.tsnew file mode 100644index 0000000..fbbd228--- /dev/null+++ b/skills/anidoodle/engine/src/canvas-core/cheetahScenery.ts@@ -0,0 +1,109 @@+// THE COUNTRY SHE RUNS THROUGH. Speed is read from the world, so the world is in layers at their own+// distances, and each goes by at its own rate: the sun does not move at all, the far range creeps,+// the acacias drift, the scrub and rocks pass, the grass at her feet streams, and now and then+// something close to the lens whips across the whole frame. It is also a journey, not a treadmill:+// what she passes changes as the ground goes by under her.+// the thorn savanna (acacias, termite mounds) · a kopje (boulders, dead trees; the birds go up+// here) · a shallow pan (water, reeds, the sun laid down in it, spray off her feet) · grass again.+// Every element has a place on the ground, G, in px of ground travelled; a layer at depth factor f+// puts it on screen at 540 + (G - X) * f, where X is how far she has run. Nothing wraps, nothing repeats.+import { rng, type Ctx, type P } from "./core";+import { mix } from "./gallery";++const NIGHT = "#0d1a22", TEAL = "#2a6a70", RUST = "#b8452a", CORAL = "#f2643c", PALE = "#fff5de", DARK = "#0a1216", OCHRE = "#e3a23a";+const clamp = (v: number, a = 0, b = 1) => Math.max(a, Math.min(b, v));+const lerp = (a: number, b: number, t: number) => a + (b - a) * t;+export const leaf = (c: Ctx, x: number, y: number, a: number, len: number, w: number, col: string, alpha = 1) => {+ if (alpha <= 0.004 || len < 1) return;+ const ca = Math.cos(a), sa = Math.sin(a), tx = x + ca * len, ty = y + sa * len, mx = x + ca * len * 0.42, my = y + sa * len * 0.42;+ c.globalAlpha = alpha; c.fillStyle = col; c.beginPath(); c.moveTo(x, y); c.quadraticCurveTo(mx - sa * w, my + ca * w, tx, ty); c.quadraticCurveTo(mx + sa * w, my - ca * w, x, y); c.fill(); c.globalAlpha = 1;+};+const UP = -Math.PI / 2;++// ---------------------------------------------------------------- the things that stand in it+// an umbrella thorn: a leaning trunk, a fork of boughs, and the flat crown laid in tiers+const acacia = (c: Ctx, x: number, base: number, h: number, seed: number, col: string, lit: string) => {+ const q = rng(seed), lean = (q() - 0.5) * 0.3, fx = x + lean * h * 0.4, fy = base - h * 0.5;+ leaf(c, x, base + 4, UP + lean * 0.8, h * 0.58, h * 0.055, col);+ for (const a of [-1.0, -0.45, 0.2, 0.8]) { const ang = UP + a + (q() - 0.5) * 0.2, len = h * (0.42 + q() * 0.16), tx = fx + Math.cos(ang) * len, ty = fy + Math.sin(ang) * len; leaf(c, fx, fy, ang, len, h * 0.026, col); const w = h * (0.34 + q() * 0.2); for (let k = 0; k < 3; k++) leaf(c, tx - w * (0.5 + (q() - 0.5) * 0.3), ty - k * h * 0.03 + q() * 4, 0, w * (1 - k * 0.18), h * (0.05 - k * 0.008), k === 2 ? lit : col); }+};+const deadTree = (c: Ctx, x: number, base: number, h: number, seed: number, col: string) => { const q = rng(seed), lean = (q() - 0.5) * 0.3; leaf(c, x, base + 4, UP + lean, h, h * 0.05, col); for (let k = 0; k < 5; k++) { const t = 0.35 + q() * 0.55, sd = k % 2 ? 1 : -1; leaf(c, x + Math.cos(UP + lean) * h * t, base + Math.sin(UP + lean) * h * t, UP + lean + sd * (0.5 + q() * 0.6), h * (0.2 + q() * 0.25), h * 0.016, col); } };+const mound = (c: Ctx, x: number, base: number, h: number, col: string) => { leaf(c, x, base + 6, UP, h, h * 0.42, col); leaf(c, x + h * 0.2, base + 6, UP + 0.12, h * 0.6, h * 0.3, col); };+const bush = (c: Ctx, x: number, base: number, h: number, seed: number, col: string, lit: string) => { const q = rng(seed); for (let k = 0; k < 9; k++) leaf(c, x + (q() - 0.5) * h * 0.5, base + 5, UP + (k / 8 - 0.5) * 1.9 + (q() - 0.5) * 0.2, h * (0.6 + q() * 0.4), h * 0.2, k % 4 === 0 ? lit : col); };+// boulders: each a lens lying on the one below it+const kopje = (c: Ctx, x: number, base: number, h: number, seed: number, col: string, lit: string) => { const q = rng(seed); for (let k = 0; k < 5; k++) { const w = h * (1.1 - k * 0.17) * (0.8 + q() * 0.4), ox = (q() - 0.5) * h * 0.5, y = base - k * h * 0.2 - h * 0.06; leaf(c, x + ox - w / 2, y, 0, w, h * (0.34 - k * 0.03), k === 4 ? lit : col); } };+const reeds = (c: Ctx, x: number, base: number, h: number, seed: number, col: string, F: number, lean: number) => { const q = rng(seed); for (let k = 0; k < 7; k++) { const a = UP + (q() - 0.5) * 0.4 + Math.sin(F * (Math.PI / 45) + k) * 0.05 - lean * 0.7, len = h * (0.6 + q() * 0.4), ox = (q() - 0.5) * h * 0.4; leaf(c, x + ox, base + 5, a, len, 3.2, col); if (k % 2) leaf(c, x + ox + Math.cos(a) * len * 0.82, base + 5 + Math.sin(a) * len * 0.82, a, len * 0.2, 5.5, mix(col, OCHRE, 0.35)); } };++// ---------------------------------------------------------------- where everything stands+export type Country = ReturnType<typeof country>;+/** Lay the country out along `total` px of ground. */+export const country = (total: number) => {+ const z = (g: number) => (g < total * 0.31 ? "thorn" : g < total * 0.6 ? "kopje" : g < total * 0.86 ? "pan" : "grass");+ const q = rng(911), span = (f: number) => [-760 / f, total + 760 / f] as const;+ type Item = { g: number; h: number; seed: number; kind: string };+ const scatter = (f: number, gap: (zone: string) => number, kind: (zone: string, r: number) => string, h: (kind: string, r: number) => number): Item[] => { const [a, b] = span(f), out: Item[] = []; for (let g = a; g < b; ) { const zone = z(g), k = kind(zone, q()); out.push({ g, h: h(k, q()), seed: Math.floor(q() * 1e6), kind: k }); g += gap(zone) * (0.55 + q() * 0.9); } return out; };+ const trees = scatter(0.22, (zn) => (zn === "thorn" ? 1150 : zn === "kopje" ? 1900 : zn === "pan" ? 4200 : 1500), (zn, r) => (zn === "kopje" && r < 0.55 ? "dead" : "acacia"), (k, r) => (k === "dead" ? 190 + r * 130 : 240 + r * 190));+ const mid = scatter(0.5, (zn) => (zn === "pan" ? 430 : zn === "kopje" ? 520 : 640), (zn, r) => (zn === "thorn" ? (r < 0.4 ? "mound" : "bush") : zn === "kopje" ? (r < 0.7 ? "kopje" : "bush") : zn === "pan" ? "reeds" : "bush"), (k, r) => (k === "mound" ? 95 + r * 85 : k === "kopje" ? 130 + r * 130 : k === "reeds" ? 105 + r * 75 : 62 + r * 52));+ const lens = [0.1, 0.27, 0.43, 0.57, 0.7, 0.83, 0.93].map((t, i) => ({ g: total * t + (q() - 0.5) * 300, seed: 40 + i, kind: i % 3 === 1 ? "trunk" : "grass" }));+ const flock = Array.from({ length: 18 }, () => ({ dx: (q() - 0.5) * 520, t0: q() * 9, vx: -1 + q() * 5, vy: 5 + q() * 6, size: 0.7 + q() * 0.6, ph: q() * 6.28 }));+ return { total, zone: z, trees, mid, lens, flock, flockG: total * 0.37, pan: [total * 0.6 + 260, total * 0.86 - 260] as const };+};++// ---------------------------------------------------------------- drawing it, back to front+const ridge = (c: Ctx, X: number, f: number, horizon: number, h0: number, h1: number, ph: number, col: string) => {+ c.fillStyle = col; c.beginPath(); c.moveTo(-20, horizon + 30);+ for (let x = -20; x <= 1100; x += 14) { const u = x + X * f + ph, n = 0.5 + 0.5 * (Math.sin(u / 211) * 0.5 + Math.sin(u / 97 + 1.7) * 0.3 + Math.sin(u / 43 + 0.6) * 0.2), peak = Math.pow(n, 1.6); c.lineTo(x, horizon - lerp(h0, h1, peak)); }+ c.lineTo(1100, horizon + 30); c.closePath(); c.fill();+};+/** Everything behind her: the two ranges, the trees, the scrub and rocks. `horizon` is the ground line on screen. */+export const drawFar = (c: Ctx, w: Country, X: number, F: number, horizon: number, lean: number) => {+ ridge(c, X, 0.03, horizon, 40, 170, 0, mix(CORAL, NIGHT, 0.6));+ ridge(c, X, 0.075, horizon, 16, 92, 900, mix(RUST, NIGHT, 0.8));+ const tree = mix(NIGHT, TEAL, 0.1), treeLit = mix(RUST, NIGHT, 0.55), near = mix(NIGHT, TEAL, 0.2), nearLit = mix(TEAL, NIGHT, 0.45);+ for (const t of w.trees) { const x = 540 + (t.g - X) * 0.22; if (x < -260 || x > 1340) continue; if (t.kind === "dead") deadTree(c, x, horizon, t.h, t.seed, tree); else acacia(c, x, horizon, t.h, t.seed, tree, treeLit); }+ for (const m of w.mid) { const x = 540 + (m.g - X) * 0.5; if (x < -200 || x > 1280) continue; if (m.kind === "mound") mound(c, x, horizon, m.h, mix(RUST, NIGHT, 0.72)); else if (m.kind === "kopje") kopje(c, x, horizon, m.h, m.seed, near, mix(RUST, NIGHT, 0.6)); else if (m.kind === "reeds") reeds(c, x, horizon, m.h, m.seed, nearLit, F, lean); else bush(c, x, horizon, m.h, m.seed, near, nearLit); }+};+/** The pan: shallow water lying on the ground she runs over, with the sun laid down in it. Returns how much of it is under her (0-1). */+export const drawPan = (c: Ctx, w: Country, X: number, F: number, horizon: number, v: number) => {+ const a = 540 + (w.pan[0] - X), b = 540 + (w.pan[1] - X); if (b < -40 || a > 1120) return;+ const x0 = Math.max(-20, a), x1 = Math.min(1100, b), y0 = horizon + 8, y1 = horizon + 86;+ c.save(); c.beginPath(); c.moveTo(x0 - (a > -20 ? 60 : 0), y1); c.lineTo(x0, y0); c.lineTo(x1, y0); c.lineTo(x1 + (b < 1100 ? 60 : 0), y1); c.closePath(); c.clip();+ c.fillStyle = mix(TEAL, NIGHT, 0.52); c.fillRect(-80, y0, 1240, y1 - y0);+ const q = rng(70);+ // the sun in it, in broken bars under the sun itself; then the ripples going by+ for (let i = 0; i < 26; i++) { const yy = y0 + 4 + q() * (y1 - y0 - 8), half = 70 + q() * 230 * (1 - (yy - y0) / (y1 - y0) * 0.5), dx = (q() - 0.5) * 60 + Math.sin(F * 0.21 + i) * 9; leaf(c, 540 + dx - half, yy, 0, half * 2, 2.2 + q() * 2.6, i % 3 ? CORAL : "#ff8a4a", 0.5 + q() * 0.35); }+ for (let i = 0; i < 30; i++) { const gx = q() * 2400, yy = y0 + 3 + q() * (y1 - y0 - 6), len = 60 + q() * 200; leaf(c, ((((gx - X) % 2400) + 2400) % 2400) - 600, yy, 0, len * (1 + v), 1.2 + q() * 1.4, PALE, 0.1 + q() * 0.16); }+ c.restore();+};+export const inPan = (w: Country, g: number) => g > w.pan[0] + 30 && g < w.pan[1] - 30;+/** The birds she puts up as she comes through the kopje: out of the grass ahead of her, up and across the sun. */+export const drawFlock = (c: Ctx, w: Country, X: number, F: number, horizon: number, frameAt: (g: number) => number) => {+ const t0 = frameAt(w.flockG - 700); if (F < t0) return;+ const col = mix(NIGHT, DARK, 0.4);+ for (const b of w.flock) {+ const t = F - t0 - b.t0; if (t < 0 || t > 110) continue;+ const x = 540 + (w.flockG - X) * 0.82 + b.dx + b.vx * t, y = horizon - 6 - b.vy * t * (1 - t / 260) - Math.sin(t * 0.25 + b.ph) * 6, flap = Math.sin(t * 0.85 + b.ph), s = 13 * b.size;+ if (x < -60 || x > 1140 || y < -40) continue;+ leaf(c, x, y, Math.PI + 0.25 + flap * 0.75, s * 1.5, s * 0.36, col); leaf(c, x, y, -0.25 - flap * 0.75, s * 1.5, s * 0.36, col); leaf(c, x - s * 0.5, y, 0, s, s * 0.22, col);+ }+};+/** What crosses close to the lens: a spray of grass, or a thorn trunk with a bough, dark and huge, gone in a few frames. */+export const drawLens = (c: Ctx, w: Country, X: number, bottom: number) => {+ for (const o of w.lens) {+ const x = 540 + (o.g - X) * 3.1; if (x < -700 || x > 1780) continue;+ const q = rng(o.seed), col = mix(NIGHT, DARK, 0.55);+ if (o.kind === "trunk") { leaf(c, x - 60, bottom + 60, UP + 0.06, 1500, 150, col); leaf(c, x - 30, bottom - 620, UP + 1.05, 700, 46, col); leaf(c, x - 60, bottom - 760, UP - 0.9, 560, 38, col); }+ else for (let k = 0; k < 16; k++) leaf(c, x + (q() - 0.5) * 420, bottom + 60, UP - 0.75 + (q() - 0.5) * 0.5, 520 + q() * 560, 26 + q() * 30, k % 5 === 0 ? mix(TEAL, NIGHT, 0.6) : col);+ }+};+export type Burst = { x: number; y: number; age: number; seed: number; wet: boolean; big: number };+/** What her feet throw up: dust off the plain, spray out of the pan. */+export const drawBurst = (c: Ctx, b: Burst) => {+ const q = rng(b.seed), n = b.wet ? 16 : 11, life = b.wet ? 13 : 18, k = b.age / life; if (k < 0 || k > 1) return;+ for (let i = 0; i < n; i++) {+ const a = b.wet ? UP + (q() - 0.5) * 1.9 : Math.PI + 0.15 + q() * 0.95, sp = 0.4 + q() * 0.6, d = q();+ if (b.wet) { const vx = Math.cos(a) * 9 * sp * b.big, vy = Math.sin(a) * 15 * sp * b.big, t = b.age; leaf(c, b.x + vx * t, b.y + vy * t + 0.75 * t * t, Math.atan2(vy + 1.5 * t, vx), 9 + sp * 13, 2.4 + d * 1.6, d < 0.4 ? PALE : mix(PALE, TEAL, 0.45), 0.9 * (1 - k)); }+ else leaf(c, b.x - d * 70 * k * b.big, b.y - 4 - d * 56 * k * b.big, a, (24 + d * 58) * b.big * Math.min(1, k * 3), (7 + q() * 8) * b.big, mix(mix(OCHRE, PALE, 0.4), NIGHT, 0.12 + q() * 0.25), (0.62 - d * 0.15) * (1 - k));+ }+};+export const at = (p: P, dx: number, dy: number): P => [p[0] + dx, p[1] + dy];diff --git a/skills/anidoodle/engine/src/canvas-core/cheetahTurn.ts b/skills/anidoodle/engine/src/canvas-core/cheetahTurn.tsnew file mode 100644index 0000000..7df4fad--- /dev/null+++ b/skills/anidoodle/engine/src/canvas-core/cheetahTurn.ts@@ -0,0 +1,63 @@+// THE HEAD THAT TURNS. cheetahDusk's face is a flat drawing; here it is wrapped onto a head (a ball,+// with the muzzle standing out of it) and that head is turned about its upright axis, so the camera+// can come round from the front to the side and it is the same face all the way: at yaw 0 it is the+// drawing, untouched; by 90 degrees one eye and one tear line are left, the nose is the profile's+// leading edge, and the back of the head has come round from behind, in the same coat.+// How: the face is drawn once into a layer and laid over a grid of small triangles. Each corner of the+// grid has a depth; turning moves it sideways by that depth; each triangle carries its piece of the+// drawing across with an affine map. Nothing is generated and nothing is sampled from a clock.+import { type Ctx, type Env, type Layer, type P } from "./core";+import { duskFace, FACE } from "./cheetahDusk";++const R = 372, SQ = 0.95, OVER = 1.25; // the head's radius in the drawing; its slight flattening; the layer's oversampling+const X0 = 60, X1 = 1020, Y0 = 150, Y1 = 1070, NX = 34, NY = 32;+type Part = "neck" | "coat" | "head";+const layer = (env: Env, f: number, part: Part): Layer => {+ const key = `cheetahTurn:${part}:${f}`; let L = env.cache.get(key) as Layer | undefined; if (L) return L;+ const k = env.scale * OVER; L = env.canvas(Math.round(1080 * k), Math.round(1080 * k)); env.cache.set(key, L);+ L.ctx.setTransform(k, 0, 0, k, 0, 0); duskFace(L.ctx, f, part); L.ctx.setTransform(1, 0, 0, 1, 0, 0);+ return L;+};+const ball = (x: number, y: number) => { const dx = x - FACE[0], dy = (y - FACE[1]) / SQ, r2 = dx * dx + dy * dy; return r2 < R * R ? Math.sqrt(R * R - r2) : 0; };+// the front of the head: the ball, and the muzzle pushed forward out of it below the eyes+const front = (x: number, y: number) => ball(x, y) + 150 * Math.exp(-((x - 540) ** 2 + (y - 735) ** 2) / (2 * 105 * 105));+const GRID = (() => { const g: { x: number; y: number; zf: number; zb: number; in: boolean }[] = []; for (let j = 0; j <= NY; j++) for (let i = 0; i <= NX; i++) { const x = X0 + ((X1 - X0) * i) / NX, y = Y0 + ((Y1 - Y0) * j) / NY, b = ball(x, y); g.push({ x, y, zf: front(x, y), zb: -b, in: b > 0 }); } return g; })();++/** The head, turned by `yaw` radians (0 faces us, positive brings the nose round to the right), its centre at (cx, cy) on screen, drawn at scale k. */+export const turnedHead = (c: Ctx, env: Env, f: number, yaw: number, cx: number, cy: number, k: number) => {+ const s = env.scale, cs = Math.cos(yaw), sn = Math.sin(yaw), head = layer(env, f, "head"), coat = layer(env, f, "coat");+ const put = (x: number, y: number, z: number): [number, number, number] => [cx + k * ((x - FACE[0]) * cs + z * sn), cy + k * (y - FACE[1]), -(x - FACE[0]) * sn + z * cs];+ const F = GRID.map((g) => put(g.x, g.y, g.zf)), B = yaw > 0.02 ? GRID.map((g) => put(g.x, g.y, g.zb)) : [];+ const tris: { img: Layer; a: number; b: number; c: number; v: [number, number, number][]; d: number }[] = [];+ for (let j = 0; j < NY; j++) for (let i = 0; i < NX; i++) {+ const p = j * (NX + 1) + i, q = p + 1, r = p + NX + 1, t = r + 1;+ for (const [a, b, cc] of [[p, q, r], [q, t, r]] as const) {+ const area = (v: [number, number, number][]) => (v[b][0] - v[a][0]) * (v[cc][1] - v[a][1]) - (v[cc][0] - v[a][0]) * (v[b][1] - v[a][1]);+ if (area(F) > 0.02 * k * k) tris.push({ img: head, a, b, c: cc, v: F, d: F[a][2] + F[b][2] + F[cc][2] }); // facing us+ if (B.length && GRID[a].in && GRID[b].in && GRID[cc].in && area(B) < -0.02 * k * k) tris.push({ img: coat, a, b, c: cc, v: B, d: B[a][2] + B[b][2] + B[cc][2] }); // the back, come round+ }+ }+ tris.sort((m, n) => m.d - n.d);+ const o = OVER;+ for (const t of tris) {+ const s0 = GRID[t.a], s1 = GRID[t.b], s2 = GRID[t.c], d0 = t.v[t.a], d1 = t.v[t.b], d2 = t.v[t.c];+ const ax = s1.x - s0.x, ay = s1.y - s0.y, bx = s2.x - s0.x, by = s2.y - s0.y, det = ax * by - bx * ay; if (!det) continue;+ const ux = d1[0] - d0[0], uy = d1[1] - d0[1], vx = d2[0] - d0[0], vy = d2[1] - d0[1];+ const m11 = (ux * by - vx * ay) / det, m12 = (vx * ax - ux * bx) / det, m21 = (uy * by - vy * ay) / det, m22 = (vy * ax - uy * bx) / det;+ const tx = d0[0] - m11 * s0.x - m12 * s0.y, ty = d0[1] - m21 * s0.x - m22 * s0.y;+ // clip to the triangle, grown a hair so neighbours leave no seam, then lay the drawing through the map+ const gx = (d0[0] + d1[0] + d2[0]) / 3, gy = (d0[1] + d1[1] + d2[1]) / 3, grow = (p: [number, number, number]): P => { const dx = p[0] - gx, dy = p[1] - gy, l = Math.hypot(dx, dy) || 1; return [p[0] + (dx / l) * 0.8, p[1] + (dy / l) * 0.8]; };+ const e0 = grow(d0), e1 = grow(d1), e2 = grow(d2);+ c.save(); c.setTransform(s, 0, 0, s, 0, 0); c.beginPath(); c.moveTo(e0[0], e0[1]); c.lineTo(e1[0], e1[1]); c.lineTo(e2[0], e2[1]); c.closePath(); c.clip();+ c.setTransform(m11 / o, m21 / o, m12 / o, m22 / o, s * tx, s * ty); c.drawImage(t.img.canvas, 0, 0); c.restore();+ }+};++/** The chest under the face, as cheetahDusk has it: a flat front that narrows away as the camera comes round. */+export const turnedNeck = (c: Ctx, env: Env, f: number, yaw: number, cx: number, cy: number, k: number) => {+ const w = Math.cos(yaw) ** 2; if (w < 0.03) return;+ const s = env.scale, L = layer(env, f, "neck");+ c.save(); c.setTransform(s * k * w, 0, 0, s * k, s * (cx - FACE[0] * k * w), s * (cy - FACE[1] * k)); c.drawImage(L.canvas, 0, 0, 1080, 1080); c.restore();+};+/** The size the turned head must be drawn at to stand in for the profile head cheetahBody draws. */+export const HEAD_K = 0.118;diff --git a/skills/anidoodle/engine/src/canvas-core/music/pieces/cheetahRun.ts b/skills/anidoodle/engine/src/canvas-core/music/pieces/cheetahRun.tsnew file mode 100644index 0000000..00f3b8e--- /dev/null+++ b/skills/anidoodle/engine/src/canvas-core/music/pieces/cheetahRun.ts@@ -0,0 +1,70 @@+// CHEETAH, RUN: the score. Nine bars of 12/8 at 80 bpm, exactly the film's 27 s, written to loop.+// One idea held all the way through: a G drone, the G major pentatonic, one marimba cell that grows,+// and a hand-percussion cycle that comes in under it, fills for the run and thins away again. It+// draws on East African mallet and lyre music for its shape (a drone, a cycling pattern, a melody+// that returns to the drone note); every note here is written for this film.+// The cell: up from the fifth through the sixth to the tonic, D E G. The 12/8 lilt never changes, so+// the run is the same music at full strength, not a different piece: her stride is 15 frames, a+// quarter note here, six to the bar across the bar's four beats.+// BARS [film]: 1 the drone and the cell, alone [night, the sun] · 2-3 the cell answers itself, the+// drums begin [the face makes itself, alive] · 4 one long note over a harp sweep [the turn, the bow] ·+// 5 the drone steps to E, two beats held, then a run of eighths up [she gathers; she goes at 405] ·+// 6 the cell in her stride, the highest note, full drums [at speed] · 7 three strides and everything+// stops [forefeet braced at 575] · 8-9 the cell comes home, the drone turns back [she stands, the+// turn back, into the eye], and round again.+import { type Piece } from "../plan";+import { composePiece, type Material } from "../compose";++const SHAKE = "C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected]";+export const cheetahRunMaterial = (): Material => ({+ style: "world", title: "Cheetah, run", seed: 5521, mood: "joy", bpm: 80, key: "G", mode: "majorPentatonic", meter: "12/8", loop: true, tail: 1.2,+ voices: { lead: "marimba" }, levels: { lead: 4, counter: -5, kick: -4.5, ghost: -6, perc: 8, chords: -3 }, moodControls: { energy: 0.5, warmth: 0.5, brightness: 0.68, tension: 0.4, space: 0.5 },+ chords: {+ G: { voicing: "[G2 D3 G3]", bass: "G1:4" },+ E: { voicing: "[E2 B2 E3]", bass: "E1:4" },+ D: { voicing: "[D3 A3 D4]", bass: "D2:4" },+ },+ motifs: {+ call: "r:2 D5:1/3 E5:1/3 G5:4/3",+ answer: "E5:1/3 D5:1/3 B4:1/3 D5:1 r:1 D5:1/3 E5:1/3 G5:1/3",+ alive: "A5:2/3 G5:1/3 E5:1 D5:1/3 E5:1/3 G5:1/3 D5:1",+ stretch: "E5:1/3 G5:1/3 A5:7/3 G5:1",+ gather: "r:2 B4:1/3 D5:1/3 E5:1/3 G5:1/3 A5:1/3 B5:1/3",+ stride: "D6:2/[email protected] B5:2/3 A5:2/3 G5:2/3 A5:2/3 B5:2/3",+ brace: "D6:2/[email protected] B5:2/3 G5:2/3 r:2",+ stand: "r:1 D5:1/3 E5:1/3 G5:1/3 E5:1 D5:1",+ home: "r:1 B4:1/3 D5:1/3 E5:1/3 D5:2",+ rest: "r:4",+ echo: "r:3 G4:1/3 D5:1/3 G5:1/3",+ sweep: "G3:1/3 D4:1/3 G4:1/3 B4:1/3 D5:1/3 G5:1/3 r:2",+ roll: "G4:1/3 D5:1/3 G5:1/3 D5:1/3 B4:1/3 D5:1/3 G4:1/3 D5:1/3 G5:1/3 D5:1/3 B4:1/3 D5:1/3",+ rollStop: "G4:1/3 D5:1/3 G5:1/3 D5:1/3 r:8/3",+ settle: "r:2 [G4 D5]:2",+ },+ grooves: {+ // the cycle: a low stroke on one and a lighter one on three, a rim answer, a shaker on the lilt+ cycle: { kick: ["G2:2 G2:[email protected] r:1"], ghost: ["r:1 C4:1/[email protected] r:2/3 r:1 C4:1/[email protected] r:2/3"], perc: ["C4:1/[email protected] r:1/3 C4:1/[email protected] C4:1/[email protected] r:1/3 C4:1/[email protected] C4:1/[email protected] r:1/3 C4:1/[email protected] C4:1/[email protected] r:1/3 C4:1/[email protected]"] },+ thin: { kick: ["G2:2 r:2"], perc: ["C4:1/[email protected] r:2/3 r:1 C4:1/[email protected] r:2/3 r:1"] },+ launch: { kick: ["r:2 G2:1 G2:[email protected]"], ghost: ["r:2 C4:2/3 C4:2/[email protected] C4:2/3"], perc: ["r:2 C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected]"] },+ gallop: { kick: ["G2:1 G2:[email protected] G2:1 G2:[email protected]", "G2:1 G2:1/[email protected] r:8/3"], ghost: ["C4:2/3 C4:2/[email protected] C4:2/3 C4:2/[email protected] C4:2/3 C4:2/[email protected]", "C4:2/3 C4:2/[email protected] r:8/3"], perc: [SHAKE, "C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] C4:1/[email protected] r:8/3"] },+ },+ sections: [+ { kind: "intro", bars: 1, harmony: ["G"], lead: ["call"], energy: 0.35 },+ { kind: "verse", bars: 3, harmony: ["G", "~", "~"], lead: ["answer", "alive", "stretch"], counter: ["rest", "echo", "sweep"], groove: "cycle", energy: 0.45 },+ { kind: "build", bars: 1, harmony: ["E"], lead: ["gather"], groove: "launch", energy: 0.6 },+ { kind: "drop", bars: 2, harmony: ["G", "~"], lead: ["stride", "brace"], counter: ["roll", "rollStop"], groove: "gallop", energy: 0.9 },+ { kind: "breath", bars: 2, harmony: ["G", "D"], lead: ["stand", "home"], counter: ["settle", "rest"], groove: "thin", energy: 0.35 },+ ],+});+export const cheetahRunScore = (): Piece => composePiece(cheetahRunMaterial());+// The signed cut: the same nine bars played once, then a tenth that closes them. The cell one last+// time, landing on the tonic and held, over a rolled harp chord and one soft low stroke; no cycle.+// The ending takes away: it is the quietest bar of the piece, and it is left to ring.+export const cheetahRunSignedMaterial = (): Material => {+ const m = cheetahRunMaterial();+ return { ...m, title: "Cheetah, run (signed)", loop: false, tail: 2.2,+ motifs: { ...m.motifs, sign: "r:1 D5:1/[email protected] E5:1/[email protected] G5:7/[email protected]", signHarp: "G3:1/[email protected] D4:1/[email protected] G4:1/[email protected] r:1 [G4 D5]:[email protected]" },+ grooves: { ...m.grooves, last: { kick: ["G2:[email protected]"] } },+ sections: [...m.sections, { kind: "outro", bars: 1, harmony: ["G"], lead: ["sign"], counter: ["signHarp"], groove: "last", energy: 0.25 }] };+};+export const cheetahRunSignedScore = (): Piece => composePiece(cheetahRunSignedMaterial());diff --git a/skills/anidoodle/engine/src/canvas-core/music/render.ts b/skills/anidoodle/engine/src/canvas-core/music/render.tsindex e79fbb1..e9430ac 100644--- a/skills/anidoodle/engine/src/canvas-core/music/render.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/render.ts@@ -6,7 +6,7 @@ import { type Piece, type Part, resequence, beatsPerBar } from "./plan";import { perform, type Performance, type Played } from "./perform";import { renderPiano, PIANO_REAL, type PianoOpts } from "./piano";import { keysVoice, renderPianoV2 } from "./keys";-import { bankFor, samplerVoice, sampleZones, registerBank, type SampleBank, type SampleTrim } from "./sampler";+import { bankFor, pinnedPitch, samplerVoice, sampleZones, registerBank, type SampleBank, type SampleTrim } from "./sampler";import * as I from "./instruments";import * as O from "./orchestra";import { chokeTimes, type KitCtx } from "./drums";@@ -65,7 +65,9 @@ const v2Job = (piece: Piece, pt: Part, pi: number, keys: Played[], perf: Performconst transient = style.master === "gentle" && tg && isStruck(pt.inst, pt.opts) ? tg : undefined;const post: StemPost | undefined = lofi || amp !== undefined || transient ? { id: pt.id, seed: piece.seed, ...(lofi ? { lofi, period } : {}), ...(amp !== undefined ? { amp } : {}), ...(transient ? { transient } : {}) } : undefined;const bank = pt.opts?.sampled === false ? undefined : bankFor(pt.inst, typeof pt.opts?.variant === "string" ? pt.opts.variant : undefined);- const base = bank ? { kind: "sampled" as const, bank: bank.id, bankHash: bank.hash, inst: pt.inst, keys, pedal: perf.pedal, sr, n, seed: pt.inst === "piano" ? piece.seed + pi : piece.seed * 101 + pi, opts: pt.opts }+ // a recorded drum pinned to a note plays it, as the modelled one does: a drum lane's written pitch is a placeholder+ const pin = bank && typeof pt.opts?.pitch === "number" ? pinnedPitch(pt.opts.pitch as number, bank.entry.range) : undefined, played = pin === undefined ? keys : keys.map((k) => ({ ...k, p: pin }));+ const base = bank ? { kind: "sampled" as const, bank: bank.id, bankHash: bank.hash, inst: pt.inst, keys: played, pedal: perf.pedal, sr, n, seed: pt.inst === "piano" ? piece.seed + pi : piece.seed * 101 + pi, opts: pt.opts }: pt.inst === "piano" ? (o.piano ? { kind: "piano" as const, keys, pedal: perf.pedal, sr, n, seed: piece.seed + pi, v1: o.piano } : { kind: "piano" as const, keys, pedal: perf.pedal, sr, n, seed: piece.seed + pi, opts: pt.opts ?? {} }): { kind: "voice" as const, inst: pt.inst, opts: pt.opts, keys, sr, n, seed: piece.seed * 101 + pi, ctx };const roomHashes: [string, string, number, number][] = [effectiveSpace(piece.plan.space)?.room ?? prof.space?.room, prof.drumRoom?.room].flatMap((id) => { const r = roomFor(id); return r && id ? [[id, r.sha256, r.rt60, r.directFrames ?? 0] as [string, string, number, number]] : []; });diff --git a/skills/anidoodle/engine/src/canvas-core/music/sampler.ts b/skills/anidoodle/engine/src/canvas-core/music/sampler.tsindex 7743653..49129a2 100644--- a/skills/anidoodle/engine/src/canvas-core/music/sampler.ts+++ b/skills/anidoodle/engine/src/canvas-core/music/sampler.ts@@ -61,6 +61,8 @@ export const sampleCutoff = (e: SampleEntry, z: SampleZone, k: Played, sr: numbereturn Math.min(sr * 0.45, 350 + 17650 * t * t);};export const sampleGain = (e: SampleEntry, z: SampleZone, v: number) => e.normalized ? pianoVelocity(v) * db(-8 * Math.max(0, 0.6 - v)) : v <= 0 ? 0 : db(clamp(16 * (v - layerV(e, z.layer)), -6, 6));+/** A drum tuned to one note (a timpani in a drum lane) plays that note whatever the lane wrote, moved by octaves into what was recorded. */+export const pinnedPitch = (pitch: number, range: readonly [number, number] | readonly number[]) => { let p = Math.round(pitch); while (p < range[0]) p += 12; while (p > range[1]) p -= 12; return p < range[0] ? range[0] : p; };/** Nearest pitch, then nearest layer, then this note's seeded recording. State stays local to a render. */export const sampleZones = (bank: SampleBank, keys: Played[], seed: number, pedal: PedalSpan[] = [], art?: "rel") => {const previous = new Map<string, SampleBank["zones"][number]>(), e = bank.entry, pool = bank.zones.filter(({ zone }) => zone.art === art);diff --git a/skills/anidoodle/engine/src/hosts/page-cheetahRun.ts b/skills/anidoodle/engine/src/hosts/page-cheetahRun.tsnew file mode 100644index 0000000..0a471e6--- /dev/null+++ b/skills/anidoodle/engine/src/hosts/page-cheetahRun.ts@@ -0,0 +1,4 @@+import { cheetahRun } from "../canvas-core/cheetahRun";+import { mountFilm } from "./page";++mountFilm(cheetahRun);diff --git a/skills/anidoodle/engine/src/hosts/page-cheetahRunSigned.ts b/skills/anidoodle/engine/src/hosts/page-cheetahRunSigned.tsnew file mode 100644index 0000000..b45360e--- /dev/null+++ b/skills/anidoodle/engine/src/hosts/page-cheetahRunSigned.ts@@ -0,0 +1,4 @@+import { cheetahRunSigned } from "../canvas-core/cheetahRunSigned";+import { mountFilm } from "./page";++mountFilm(cheetahRunSigned);diff --git a/skills/anidoodle/engine/test/cheetahRig.test.ts b/skills/anidoodle/engine/test/cheetahRig.test.tsnew file mode 100644index 0000000..1448dd8--- /dev/null+++ b/skills/anidoodle/engine/test/cheetahRig.test.ts@@ -0,0 +1,48 @@+// CHEETAH RIG TESTS. The skeleton (src/canvas-core/cheetahRig.ts) is pure arithmetic, so the things+// that make her move like an animal and not like rubber are held to numbers here: node tools/test.mjs+import { BOW, CROUCH, fk, FORE_BONES, gallopRig, gallopY, HIND_BONES, lowest, mixRig, settle, SKID, STAND, TAIL_BONES } from "../src/canvas-core/cheetahRig";+import type { P } from "../src/canvas-core/core";++export const name = "cheetahRig";+export const run = (ok: (cond: boolean, label: string) => void) => {+ const dist = (a: P, b: P) => Math.hypot(a[0] - b[0], a[1] - b[1]);+ const bones = (pts: P[], B: number[]) => Math.max(...B.map((b, i) => Math.abs(dist(pts[i], pts[i + 1]) - b)));++ // 1. bones never change length, in the stride or in a blend between two held poses+ let worst = 0, finite = true;+ for (let i = 0; i < 400; i++) {+ const q = fk(i % 2 ? gallopRig(i / 400) : mixRig(STAND, BOW, i / 400), 0, 0, 0);+ worst = Math.max(worst, bones(q.foreN, FORE_BONES), bones(q.foreF, FORE_BONES), bones(q.hindN, HIND_BONES), bones(q.hindF, HIND_BONES), bones(q.tail, TAIL_BONES));+ for (const pts of [q.spine, q.tail, q.foreN, q.foreF, q.hindN, q.hindF]) for (const p of pts) if (!Number.isFinite(p[0]) || !Number.isFinite(p[1])) finite = false;+ }+ ok(worst < 1e-6, `every leg and tail bone keeps its length (worst error ${worst.toExponential(1)} px)`);+ ok(finite, "every point is finite through the stride and the blends");++ // 2. the stride is a loop: phase 1 is phase 0, and no joint jumps between neighbouring frames+ const a = fk(gallopRig(0), 0, gallopY(0), 0), b = fk(gallopRig(1), 0, gallopY(1), 0);+ ok(dist(a.foreN[4], b.foreN[4]) < 1e-6 && dist(a.hindN[4], b.hindN[4]) < 1e-6 && dist(a.tail[5], b.tail[5]) < 1e-6, "the stride closes: phase 1 lands exactly on phase 0");+ let jump = 0; const at = (ph: number) => fk(gallopRig(ph), 0, gallopY(ph), 0);+ for (let i = 0; i < 300; i++) { const p = at(i / 300), q = at((i + 1) / 300); for (const k of ["foreN", "foreF", "hindN", "hindF"] as const) jump = Math.max(jump, dist(p[k][4], q[k][4])); }+ ok(jump < 12, `no foot jumps within the stride (largest move in 1/300 of a stride: ${jump.toFixed(1)} px)`);++ // 3. she never goes through the ground, and she really leaves it twice a stride+ const lowAt = (ph: number) => { const l = lowest(fk(gallopRig(ph), 0, gallopY(ph), 0)); return Math.max(l.fore, l.hind); };+ let sink = -Infinity, flights = 0, wasAir = lowAt(299 / 300) < -12; // the stride is a loop: start from where the last one ended+ for (let i = 0; i < 300; i++) { const low = lowAt(i / 300); sink = Math.max(sink, low); const air = low < -12; if (air && !wasAir) flights++; wasAir = air; }+ ok(sink < 0.01, `no foot below the ground in the stride (lowest ${sink.toFixed(2)} px)`);+ ok(flights === 2, `two flight phases a stride, the extended and the gathered (found ${flights})`);++ // 4. the spine folds and opens: the hips come well forward under her in the gathered phase+ const span = (ph: number) => { const q = at(ph); return q.spine[6][0] - q.spine[1][0]; };+ ok(span(0) - span(0.52) > 20, `shoulder-to-hip distance closes in the gathered phase (${span(0).toFixed(0)} px extended, ${span(0.52).toFixed(0)} px gathered)`);+ const g = at(0.56); ok(Math.max(g.hindN[4][0], g.hindF[4][0]) > Math.min(g.foreN[4][0], g.foreF[4][0]), "gathered, a hind foot reaches past a forefoot");++ // 5. the head rides level while the body works under it+ let hi = Infinity, lo = -Infinity, hipHi = Infinity, hipLo = -Infinity;+ for (let i = 0; i < 100; i++) { const q = at(i / 100); hi = Math.min(hi, q.spine[9][1]); lo = Math.max(lo, q.spine[9][1]); hipHi = Math.min(hipHi, q.spine[1][1]); hipLo = Math.max(hipLo, q.spine[1][1]); }+ ok(lo - hi < 4 && hipLo - hipHi > 30, `the head moves ${(lo - hi).toFixed(1)} px through a stride while the hips move ${(hipLo - hipHi).toFixed(0)} px`);++ // 6. a held pose stands on all four feet+ for (const [n, r] of Object.entries({ STAND, BOW, CROUCH, SKID })) { const s = settle(r), l = lowest(fk(s.rig, 0, s.y, 0)); ok(Math.abs(l.fore) < 2 && Math.abs(l.hind) < 2, `${n} settles with fore and hind feet on the ground (${l.fore.toFixed(1)}, ${l.hind.toFixed(1)} px)`); }+ const s = settle(BOW), q = fk(s.rig, 0, s.y, 0); ok(q.spine[1][1] < q.spine[6][1] - 120, "in the bow the hips stay high above the shoulders");+};diff --git a/skills/anidoodle/engine/test/sampler.test.ts b/skills/anidoodle/engine/test/sampler.test.tsindex e77b6c8..7fe0390 100644--- a/skills/anidoodle/engine/test/sampler.test.ts+++ b/skills/anidoodle/engine/test/sampler.test.ts@@ -1,5 +1,6 @@// Tiny synthetic recordings: pitch, dynamics, dampers, bank routing and cache identity, no files.import { registerBank, bankFor, clearBanks, samplerVoice, sampleZones, sampleCutoff, sampleGain, bankIdentity, type SampleBank, type SampleZone } from "../src/canvas-core/music/sampler";+import { pinnedPitch } from "../src/canvas-core/music/sampler";import { renderPiece, voiceJobs, runVoiceJob, voiceJobKey, calibrateBank } from "../src/canvas-core/music/render";import { nocturne } from "../src/canvas-core/music/pieces/nocturne";import { marimbaCurious } from "../src/canvas-core/music/pieces/families";@@ -191,4 +192,8 @@ export const run = (ok: (cond: boolean, label: string) => void) => {const sustained = bank(); sustained.entry.kind = "sustained";ok(throws(() => registerBank("strings", sustained), /sustained zone without a loop/), "sustained zones without loops are rejected");} finally { clearBanks(); }++ // a drum pinned to a note lands inside what was recorded, on the same note name+ ok(pinnedPitch(31, [37, 59]) === 43 && pinnedPitch(60, [37, 59]) === 48 && pinnedPitch(43, [37, 59]) === 43, "a pinned pitch moves by octaves into the recorded range (G1 to G2, C4 to C3) and stays put when it is inside");+ ok([24, 31, 38, 60, 72, 90].every((p) => { const q = pinnedPitch(p, [37, 59]); return q >= 37 && q <= 59 && (q - p) % 12 === 0; }), "every pinned pitch stays in range and keeps its note name");};