Skip to content

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

Crop exported film to the card like the page does

jay236064346c325012c
diff --git a/public/demo.mjs b/public/demo.mjs
index 7868822..a9da538 100644
--- a/public/demo.mjs
+++ b/public/demo.mjs
@@ -206,7 +206,11 @@ async function drawExportFrame(context, geometry, film, time) {
const drafts = await Promise.all(sections.map(rasterize));
context.fillStyle = '#0b0b0c'; context.fillRect(0, 0, geometry.width, geometry.height);
const f = geometry.film;
- context.save(); rounded(f.x, f.y, f.w, f.h, 14 * scale); context.clip(); context.drawImage(film, f.x, f.y, f.w, f.h); context.restore();
+ // Centered crop, like the page's object-fit: cover, so a vertical reference is not stretched.
+ const scaleToCover = Math.max(f.w / film.displayWidth, f.h / film.displayHeight);
+ const sourceW = f.w / scaleToCover, sourceH = f.h / scaleToCover;
+ const sourceX = (film.displayWidth - sourceW) / 2, sourceY = (film.displayHeight - sourceH) / 2;
+ context.save(); rounded(f.x, f.y, f.w, f.h, 14 * scale); context.clip(); context.drawImage(film, sourceX, sourceY, sourceW, sourceH, f.x, f.y, f.w, f.h); context.restore();
context.textBaseline = 'middle'; context.textAlign = 'center'; context.font = `500 ${12.5 * scale}px Inter, system-ui, sans-serif`;
sections.forEach((section, index) => {
const { x, y, w, h } = geometry.cards[index], isActive = index === active;
diff --git a/test/export-crop.test.js b/test/export-crop.test.js
new file mode 100644
index 0000000..ac26eca
--- /dev/null
+++ b/test/export-crop.test.js
@@ -0,0 +1,56 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { readFile } from 'node:fs/promises';
+
+// The exporter must crop the film like the page's object-fit: cover, not stretch it into the card.
+const sourceText = await readFile(new URL('../public/demo.mjs', import.meta.url), 'utf8');
+
+function drawExportFrameBody() {
+ const from = sourceText.indexOf('async function drawExportFrame');
+ assert.notEqual(from, -1, 'demo.mjs should define drawExportFrame');
+ return sourceText.slice(from, sourceText.indexOf('\n}\n', from));
+}
+
+// The crop is computed from the frame and the card, so evaluate those lines as written.
+function cropFor(film, f) {
+ const body = drawExportFrameBody();
+ const start = body.indexOf('const scaleToCover');
+ assert.notEqual(start, -1, 'drawExportFrame should compute the cover crop');
+ const end = body.indexOf('context.save()', start);
+ const crop = new Function('film', 'f', `${body.slice(start, end)}\nreturn { sourceX, sourceY, sourceW, sourceH };`);
+ return crop(film, f);
+}
+
+test('export draws the film with a centered source crop', () => {
+ const body = drawExportFrameBody();
+ const start = body.indexOf('context.drawImage(film,');
+ assert.notEqual(start, -1, 'drawExportFrame should draw the film');
+ let depth = 0, end = start + 'context.drawImage'.length;
+ for (; end < body.length; end++) {
+ if (body[end] === '(') depth++;
+ if (body[end] === ')' && --depth === 0) break;
+ }
+ const args = body.slice(start + 'context.drawImage('.length, end).split(',').map(part => part.trim());
+ assert.deepEqual(args, ['film', 'sourceX', 'sourceY', 'sourceW', 'sourceH', 'f.x', 'f.y', 'f.w', 'f.h'],
+ `expected drawImage(film, sourceX, sourceY, sourceW, sourceH, f.x, f.y, f.w, f.h), got ${args.join(', ')}`);
+});
+
+test('vertical reference is cropped to the card width and centered vertically', () => {
+ const card = { x: 0, y: 0, w: 1600, h: 900 };
+ const film = { displayWidth: 1080, displayHeight: 1920 };
+ const { sourceX, sourceY, sourceW, sourceH } = cropFor(film, card);
+ assert.equal(sourceX, 0, 'no horizontal crop for a film narrower than the card ratio');
+ assert.equal(sourceW, 1080, 'source keeps the full width');
+ assert.ok(Math.abs(sourceH / sourceW - card.h / card.w) < 1e-9, 'source crop has the card aspect ratio');
+ assert.ok(Math.abs(sourceY - (1920 - sourceH) / 2) < 1e-9, 'source crop is vertically centered');
+});
+
+test('wide reference is cropped to the card height and centered horizontally', () => {
+ const card = { x: 0, y: 0, w: 1600, h: 900 };
+ const film = { displayWidth: 2560, displayHeight: 1080 };
+ const { sourceX, sourceY, sourceW, sourceH } = cropFor(film, card);
+ assert.equal(sourceY, 0, 'no vertical crop for a film with a wider ratio than the card');
+ assert.equal(sourceH, 1080, 'source keeps the full height');
+ assert.ok(Math.abs(sourceW / sourceH - card.w / card.h) < 1e-9, 'source crop has the card aspect ratio');
+ assert.ok(Math.abs(sourceX - (2560 - sourceW) / 2) < 1e-9, 'source crop is horizontally centered');
+});