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
fix(render): ranged render writes out/<film>.<from>-<to>.<ext> and never the finished film; alpha-weighted motion blur; stepped art is not blurred; --blur validated; determinism probe hashes the blurred frame
Alex Greenshpunfdaac7142c28
diff --git a/skills/anidoodle/engine/src/hosts/page.ts b/skills/anidoodle/engine/src/hosts/page.tsindex 9eaf499..5c216ea 100644--- a/skills/anidoodle/engine/src/hosts/page.ts+++ b/skills/anidoodle/engine/src/hosts/page.ts@@ -25,22 +25,31 @@ export const mountFilm = (film: Film) => {// MOTION BLUR. One output frame as the average of subframes spread over a one-frame shutter// centred on it, confined to its own shot so nothing bleeds across a cut. The average is taken// in LINEAR light: blur is light integration, and averaging in sRGB darkens every moving edge.+ // It is also ALPHA-WEIGHTED (premultiplied): a transparent subframe carries no colour, so a+ // sticker's moving edge fades out instead of picking up a dark fringe from the zero RGB that+ // canvas keeps under alpha 0. Opaque pixels take exactly the unweighted path.+ // STEPPED ART (meta.step > 1, or onTwos) holds each drawing for `step` frames, so the shutter+ // would straddle two drawings and smear a pose into its neighbour: such films are not blurred.// hosts may keep time; the core never does.const toLin = new Float32Array(256);for (let i = 0; i < 256; i++) { const s = i / 255; toLin[i] = s <= 0.04045 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4); }const fromLin = (l: number) => { const v = l <= 0.0031308 ? l * 12.92 : 1.055 * Math.pow(l, 1 / 2.4) - 0.055; return Math.max(0, Math.min(255, Math.round(v * 255))); };const blur = (frame: number, samples: number) => {const n = Math.max(0, Math.min(film.meta.durationFrames - 1, Math.round(frame))), shot = film.shots.find((s) => n >= s.start && n < s.end);- if (!shot || samples < 2) return { shot: shot?.id ?? null, ms: 0 };+ if (!shot || samples < 2 || (film.meta.step ?? (film.meta.onTwos ? 2 : 1)) > 1) { const r = seek(n); return { shot: r.shot, ms: r.ms }; }const t0 = performance.now(), w = canvas.width, h = canvas.height, acc = new Float64Array(w * h * 4);for (let i = 0; i < samples; i++) {const t = Math.min(shot.end - 1, Math.max(shot.start, n + (i + 0.5) / samples - 0.5));renderFrame(film, ctx, t, env);const d = ctx.getImageData(0, 0, w, h).data;- for (let p = 0; p < d.length; p += 4) { acc[p] += toLin[d[p]]; acc[p + 1] += toLin[d[p + 1]]; acc[p + 2] += toLin[d[p + 2]]; acc[p + 3] += d[p + 3]; }+ for (let p = 0; p < d.length; p += 4) { const a = d[p + 3], k = a / 255; acc[p] += toLin[d[p]] * k; acc[p + 1] += toLin[d[p + 1]] * k; acc[p + 2] += toLin[d[p + 2]] * k; acc[p + 3] += a; }+ }+ const img = ctx.createImageData(w, h), o = img.data, inv = 1 / samples, full = 255 * samples;+ for (let p = 0; p < o.length; p += 4) {+ const A = acc[p + 3]; if (A === 0) continue; // nothing was ever drawn here: stays transparent black+ const c = A === full ? inv : 255 / A; // premultiplied average, un-premultiplied by the mean alpha+ o[p] = fromLin(acc[p] * c); o[p + 1] = fromLin(acc[p + 1] * c); o[p + 2] = fromLin(acc[p + 2] * c); o[p + 3] = Math.round(A * inv);}- const img = ctx.createImageData(w, h), o = img.data, inv = 1 / samples;- for (let p = 0; p < o.length; p += 4) { o[p] = fromLin(acc[p] * inv); o[p + 1] = fromLin(acc[p + 1] * inv); o[p + 2] = fromLin(acc[p + 2] * inv); o[p + 3] = Math.round(acc[p + 3] * inv); }ctx.putImageData(img, 0, 0); current = n;return { shot: shot.id, ms: performance.now() - t0 };};diff --git a/skills/anidoodle/engine/tools/adapters/playwright.mjs b/skills/anidoodle/engine/tools/adapters/playwright.mjsindex 60e6d35..110fbb7 100644--- a/skills/anidoodle/engine/tools/adapters/playwright.mjs+++ b/skills/anidoodle/engine/tools/adapters/playwright.mjs@@ -18,6 +18,8 @@ export const open = async ({ pw, browser: found }, pagePath, { scale = 1, workerhash: (n, w = 0) => pick(w).evaluate((f) => { window.FILM.seek(f); return window.FILM.hash(); }, n),// motion-blurred frame: `samples` subframes averaged in the page (see hosts/page.ts blur)blur: async (n, samples, w = n) => { const t0 = Date.now(); const r = await pick(w).evaluate(([f, s]) => { const r = window.FILM.blur(f, s); const t = performance.now(); const png = window.FILM.png(); return { ...r, png, enc: performance.now() - t }; }, [n, samples]); return { png: Buffer.from(r.png, "base64"), shot: r.shot, drawMs: r.ms, encodeMs: r.enc, roundTripMs: Date.now() - t0 }; },+ // hash of the motion-blurred frame: what a --blur render actually writes, so the probe checks THAT+ blurHash: (n, samples, w = 0) => pick(w).evaluate(([f, s]) => { window.FILM.blur(f, s); return window.FILM.hash(); }, [n, samples]),audio: (sr) => pages[0].evaluate((s) => window.FILM.audio(s), sr),close: () => browser.close(),};diff --git a/skills/anidoodle/engine/tools/render.mjs b/skills/anidoodle/engine/tools/render.mjsindex ca27bd3..ab7cee6 100644--- a/skills/anidoodle/engine/tools/render.mjs+++ b/skills/anidoodle/engine/tools/render.mjs@@ -1,11 +1,13 @@// node tools/render.mjs [film] [--scale 1] [--workers 4] [--out out/x.mp4|.gif|.webm|.apng] [--gif-fps 15] [--width 640]// [--blur N] [--from F] [--to F]-// --blur N motion blur: every output frame is the average of N subframes over a one-frame-// shutter, integrated in linear light inside the page. Fast pans and dolly moves stop-// juddering; stills and stepped (on-twos) art are unchanged because their subframes are-// identical. Costs N times the draw time. The default path (--blur 1) is untouched.+// --blur N motion blur (N an integer >= 1): every output frame is the average of N subframes over+// a one-frame shutter, integrated in linear light and weighted by alpha inside the page.+// Fast pans and dolly moves stop juddering; held frames come out unchanged because their+// subframes are identical. Stepped art (meta.step > 1 or onTwos) is NOT blurred: the+// shutter would straddle two drawings. Costs N times the draw time. --blur 1 is untouched.// --from/--to render only frames [from, to): for checking a passage, not for shipping. A range-// render is silent (the score would not line up) and says so.+// render is silent (the score would not line up), says so, and by default writes+// out/<film>.<from>-<to>.<ext> so it can never overwrite the finished film.// Auto-detects a backend, renders every frame through it, encodes with ffmpeg, then VERIFIES.// .mp4 the film, with its score// .gif loops and README heroes: silent, loops forever, ONE palette built from the whole piece@@ -26,8 +28,10 @@ import { float32Wav } from "./audio.mjs";const arg = (k, d) => { const i = process.argv.indexOf(`--${k}`); return i > 0 ? process.argv[i + 1] : d; };const film = process.argv[2] && !process.argv[2].startsWith("--") ? process.argv[2] : "fixtures";-const BLUR = Math.max(1, Number(arg("blur", 1)));-const scale = Number(arg("scale", 1)), fmt = (arg("out", "").match(/\.(gif|webm|apng)$/i)?.[1] ?? "mp4").toLowerCase(), workers = Number(arg("workers", Math.max(1, Math.min(4, Math.floor(cpus().length / 2))))), out = resolve(arg("out", defaultOutput(film)));+const BLUR = Number(arg("blur", 1));+if (!Number.isInteger(BLUR) || BLUR < 1) { console.error(`--blur wants a whole number of subframes >= 1, got '${arg("blur")}'`); process.exit(2); }+const scale = Number(arg("scale", 1)), fmt = (arg("out", "").match(/\.(gif|webm|apng)$/i)?.[1] ?? "mp4").toLowerCase(), workers = Number(arg("workers", Math.max(1, Math.min(4, Math.floor(cpus().length / 2)))));+const RANGE_ASKED = process.argv.includes("--from") || process.argv.includes("--to");const env = detect();console.log("backends found:"); for (const [k, v] of Object.entries(env.report)) console.log(` ${k.padEnd(11)} ${v}`);@@ -40,6 +44,11 @@ const t0 = Date.now(), session = await playwright.open(env, page.out, { scale, wconst FROM = Math.max(0, Number(arg("from", 0))), TO = Math.min(N, Number(arg("to", N)));if (!Number.isInteger(FROM) || !Number.isInteger(TO) || TO <= FROM) { console.error(`bad range: [${FROM}, ${TO}) of ${N}`); process.exit(2); }const ranged = FROM !== 0 || TO !== N;+// a passage never lands on the finished film's path: default to out/<film>.<from>-<to>.<ext>, and refuse an explicit --out that IS the film+const full = resolve(defaultOutput(film).replace(/\.mp4$/, `.${fmt}`));+const out = resolve(arg("out", ranged ? `out/${film}.${FROM}-${TO}.${fmt}` : full));+if (ranged && out === resolve(defaultOutput(film))) { console.error(`refusing to write a range render over the finished film ${out}; pick another --out or drop --from/--to`); process.exit(2); }+if (RANGE_ASKED && !ranged) console.log("range covers the whole film: rendering it complete, with its score");console.log(`film: "${meta.title}" ${meta.W}x${meta.H} @ ${meta.fps} fps, ${N} frames, ${meta.bpm} bpm, scale ${scale}, ${session.workers} page(s)`);if (BLUR > 1) console.log(`motion blur: ${BLUR} subframes per frame, one-frame shutter, linear light`);if (ranged) console.log(`range render: frames [${FROM}, ${TO}) of ${N}, silent by design`);@@ -80,9 +89,10 @@ console.log(` wall clock: ${wall.toFixed(1)} s for ${TO - FROM} frames = ${((TO// ---- verify: same frame, different order, different page. Standard = visually identical; hash equality is the cheap first test.const probe = [...new Set([0, ...meta.shots.flatMap((s) => [s.start, s.end - 1]), ...((k) => Array.from({ length: k }, (_, i) => Math.round(i * (N - 1) / (k - 1 || 1))))(Math.min(6, N)), N - 1])].filter((n) => n >= FROM && n < TO).sort((a, b) => a - b), fwd = [], rev = [];if (!probe.length) probe.push(FROM);-for (const n of probe) fwd.push(await session.hash(n, 0)); for (const n of [...probe].reverse()) rev.unshift(await session.hash(n, session.workers - 1));+const hashOf = (n, w) => (BLUR > 1 ? session.blurHash(n, BLUR, w) : session.hash(n, w)); // probe what was WRITTEN: the blurred frame when --blur+for (const n of probe) fwd.push(await hashOf(n, 0)); for (const n of [...probe].reverse()) rev.unshift(await hashOf(n, session.workers - 1));const same = probe.filter((_, i) => fwd[i] === rev[i]).length;-console.log(`\ndeterminism: ${same}/${probe.length} probe frames hash-identical (forward on page 0 vs reversed on page ${session.workers - 1})${same === probe.length ? "" : " -> fall back to PSNR > 45 dB in the Phase 2 gate"}`);+console.log(`\ndeterminism: ${same}/${probe.length} probe frames${BLUR > 1 ? ` (blurred, ${BLUR} subframes)` : ""} hash-identical (forward on page 0 vs reversed on page ${session.workers - 1})${same === probe.length ? "" : " -> fall back to PSNR > 45 dB in the Phase 2 gate"}`);await session.close();const pr = execFileSync("ffprobe", ["-v", "error", "-show_entries", "stream=codec_type,codec_name,width,height,nb_frames,duration", "-of", "csv=p=0", out]).toString().trim().split("\n");console.log(`\noutput: ${out}`); pr.forEach((l) => console.log(` ${l}`));