Skip to content

Play a finished motion-graphics ad in sync with the rough drafts it grew from, and export the breakdown as MP4.

Move section beat timing into sections.mjs and add tests

hippi3450ccd7ca7fc4b
diff --git a/public/demo.mjs b/public/demo.mjs
index 30158f8..afc481f 100644
--- a/public/demo.mjs
+++ b/public/demo.mjs
@@ -1,11 +1,11 @@
import { buildScrap, scrapNames } from './scraps.mjs';
import { buildDraft } from './drafts.mjs';
+import { HAND_BUILT, planSections } from './sections.mjs';
// The full reference plays on top; below it, the super-simple HyperFrames drafts it grew
// from — one per section. The draft for the section on screen follows the film exactly;
// the rest keep looping their own part. Each draft is as wide as its section is long, and
// the playhead runs straight across them.
-const HAND_BUILT = { id: '5924a2f28506', cuts: [0, 3, 5.75, 9.5, 13, 16.5] };
const $ = id => document.getElementById(id);
const video = $('final-video');
const reducedMotion = matchMedia('(prefers-reduced-motion: reduce)').matches;
@@ -18,37 +18,9 @@ const sectionAt = t => Math.max(0, sections.findLastIndex(section => t >= sectio
const shortName = reference => reference.name.split(' · ')[0].split(' ')[0];
// ---------- Sections ----------
-function planSections(reference) {
- if (reference.id === HAND_BUILT.id) {
- return HAND_BUILT.cuts.map((start, index) => ({ start, end: HAND_BUILT.cuts[index + 1] ?? duration, name: scrapNames[index], scrap: index }));
- }
- const key = drafts.aliases[reference.id] || reference.id, spec = drafts.drafts[key];
- let spans = bounds[reference.id] || bounds[key];
- if (!spans?.length) {
- const count = Math.max(3, Math.min(8, Math.round(duration / 4.5)));
- spans = Array.from({ length: count }, (_, i) => ({ start: duration * i / count, end: duration * (i + 1) / count }));
- }
- const sceneCuts = cuts[reference.id] || cuts[key] || [];
- return spans.map(({ start, end }, index) => {
- const section = spec?.sections[index], beats = section?.beats || [['text', `Part ${index + 1}`]];
- end = index === spans.length - 1 ? duration : end;
- return { start, end, name: section?.name || `Part ${index + 1}`, beats, starts: beatStarts(beats.length, start, end, sceneCuts),
- palette: spec?.palette || { bg: '#111113', fg: '#f1f1f1', accent: '#ff5a1f' } };
- });
-}
-// A two-beat draft switches where the film itself cuts: the scene change between 30% and
-// 80% of the section (where its two reference frames were sampled) nearest the middle.
-function beatStarts(count, start, end, sceneCuts) {
- const length = end - start;
- if (count !== 2) return Array.from({ length: count }, (_, i) => length * i / count);
- const inside = sceneCuts.map(cut => cut - start).filter(t => t > length * .3 && t < length * .8);
- inside.sort((a, b) => Math.abs(a - length * .55) - Math.abs(b - length * .55));
- return [0, inside[0] ?? length * .55];
-}
-
function buildSections() {
sections.forEach(section => section.timeline?.kill());
- sections = planSections(current);
+ sections = planSections(current, { duration, drafts, bounds, cuts, names: scrapNames });
const row = $('scraps'); row.replaceChildren(); row.dataset.count = sections.length;
sections.forEach(section => {
section.length = section.end - section.start;
diff --git a/public/sections.mjs b/public/sections.mjs
new file mode 100644
index 0000000..92e1b2f
--- /dev/null
+++ b/public/sections.mjs
@@ -0,0 +1,34 @@
+// Section and beat timing for the drafts row. Pure functions, no DOM, so they can be
+// tested in Node.
+export const HAND_BUILT = { id: '5924a2f28506', cuts: [0, 3, 5.75, 9.5, 13, 16.5] };
+
+// `duration` is the reference length; `drafts`, `bounds` and `cuts` are the loaded
+// draft specs, section bounds and scene cuts; `names` names the hand-built scraps.
+export function planSections(reference, { duration, drafts = { aliases: {}, drafts: {} }, bounds = {}, cuts = {}, names = [] }) {
+ if (reference.id === HAND_BUILT.id) {
+ return HAND_BUILT.cuts.map((start, index) => ({ start, end: HAND_BUILT.cuts[index + 1] ?? duration, name: names[index], scrap: index }));
+ }
+ const key = drafts.aliases[reference.id] || reference.id, spec = drafts.drafts[key];
+ let spans = bounds[reference.id] || bounds[key];
+ if (!spans?.length) {
+ const count = Math.max(3, Math.min(8, Math.round(duration / 4.5)));
+ spans = Array.from({ length: count }, (_, i) => ({ start: duration * i / count, end: duration * (i + 1) / count }));
+ }
+ const sceneCuts = cuts[reference.id] || cuts[key] || [];
+ return spans.map(({ start, end }, index) => {
+ const section = spec?.sections[index], beats = section?.beats || [['text', `Part ${index + 1}`]];
+ end = index === spans.length - 1 ? duration : end;
+ return { start, end, name: section?.name || `Part ${index + 1}`, beats, starts: beatStarts(beats.length, start, end, sceneCuts),
+ palette: spec?.palette || { bg: '#111113', fg: '#f1f1f1', accent: '#ff5a1f' } };
+ });
+}
+
+// A two-beat draft switches where the film itself cuts: the scene change between 30% and
+// 80% of the section (where its two reference frames were sampled) nearest the middle.
+export function beatStarts(count, start, end, sceneCuts) {
+ const length = end - start;
+ if (count !== 2) return Array.from({ length: count }, (_, i) => length * i / count);
+ const inside = sceneCuts.map(cut => cut - start).filter(t => t > length * .3 && t < length * .8);
+ inside.sort((a, b) => Math.abs(a - length * .55) - Math.abs(b - length * .55));
+ return [0, inside[0] ?? length * .55];
+}
diff --git a/test/sections.test.js b/test/sections.test.js
new file mode 100644
index 0000000..e8158c9
--- /dev/null
+++ b/test/sections.test.js
@@ -0,0 +1,47 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { HAND_BUILT, planSections, beatStarts } from '../public/sections.mjs';
+
+test('hand-timed beats follow the hand-built cuts', () => {
+ const names = ['a', 'b', 'c', 'd', 'e', 'f'];
+ const sections = planSections({ id: HAND_BUILT.id }, { duration: 20, names });
+ assert.deepEqual(sections.map(s => s.start), HAND_BUILT.cuts);
+ assert.deepEqual(sections.map(s => s.end), [3, 5.75, 9.5, 13, 16.5, 20]);
+ assert.deepEqual(sections.map(s => s.name), names);
+ assert.deepEqual(sections.map(s => s.scrap), [0, 1, 2, 3, 4, 5]);
+});
+
+test('a two-beat draft snaps to the scene cut nearest the middle', () => {
+ // Section 10..20: cuts at 30%..80% are 13..18; 15.4 is nearest 55% (15.5).
+ assert.deepEqual(beatStarts(2, 10, 20, [11, 13.5, 15.4, 17.9, 19]), [0, 5.4]);
+ const [section] = planSections({ id: 'r' }, {
+ duration: 10,
+ drafts: { aliases: {}, drafts: { r: { sections: [{ name: 'Intro', beats: [['text', 'x'], ['text', 'y']] }] } } },
+ bounds: { r: [{ start: 0, end: 10 }] },
+ cuts: { r: [6] },
+ });
+ assert.equal(section.name, 'Intro');
+ assert.deepEqual(section.starts, [0, 6]);
+});
+
+test('cuts outside 30%-80% are ignored', () => {
+ assert.deepEqual(beatStarts(2, 0, 10, [3, 8]), [0, 5.5]);
+});
+
+test('with no cut a two-beat draft falls back to 55%', () => {
+ assert.deepEqual(beatStarts(2, 4, 14, []), [0, 5.5]);
+});
+
+test('three or more beats split the section evenly', () => {
+ assert.deepEqual(beatStarts(3, 0, 9, [4.5]), [0, 3, 6]);
+ assert.deepEqual(beatStarts(4, 2, 10, []), [0, 2, 4, 6]);
+ assert.deepEqual(beatStarts(1, 0, 5, []), [0]);
+});
+
+test('without bounds the reference is split evenly into 3-8 sections', () => {
+ const sections = planSections({ id: 'none' }, { duration: 9 });
+ assert.equal(sections.length, 3);
+ assert.deepEqual(sections.map(s => s.start), [0, 3, 6]);
+ assert.equal(sections.at(-1).end, 9);
+ assert.equal(sections[0].name, 'Part 1');
+});