The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.
Exports: a page's pictures and fonts are in every frame (#257)
riverisbuilding02dc438b1b18
diff --git a/.changeset/export-page-pictures.md b/.changeset/export-page-pictures.mdnew file mode 100644index 0000000..e6f5c1f--- /dev/null+++ b/.changeset/export-page-pictures.md@@ -0,0 +1,5 @@+---+"openfilm": patch+---++Exports: a page's pictures and fonts are in every frame. A page opened during an export was drawn once its `window.film` was ready, and a page whose `ready` did not wait for its images lost them for some frames. An exact frame now waits for the page to finish loading (at most 30 s, then it is drawn as it is and the page is named among the export's problems).diff --git a/packages/openfilm/src/timeline.mjs b/packages/openfilm/src/timeline.mjsindex 102748a..a7a3b6f 100644--- a/packages/openfilm/src/timeline.mjs+++ b/packages/openfilm/src/timeline.mjs@@ -701,6 +701,33 @@ function restPage(c) {else el.addEventListener('load', close, { once: true });}+const PAGE_LOAD_MS = 30_000; // how long an exact frame waits for a page's pictures and fonts before it is drawn without them+/** each page document's wait (see pageLoaded): a page opened again is a new document, waited for again */+const pageLoads = new WeakMap();+/**+ * For an exact frame, wait until a page's document has loaded: its pictures (its images, its CSS backgrounds) and its+ * fonts. Its film is ready before them unless its `ready` waits for them (SPEC §2), and a page without that showed its+ * pictures in a preview but lost them in an export. One loaded already is drawn at once; one still loading after+ * PAGE_LOAD_MS is drawn as it is, said once.+ */+function pageLoaded(c) {+ const doc = c.win.document;+ if (doc.readyState === 'complete' && doc.fonts.status === 'loaded') return null;+ if (!pageLoads.has(doc)) {+ let timer;+ const late = new Promise((done) => { timer = setTimeout(() => done(false), PAGE_LOAD_MS); });+ const loaded = Promise.all([+ doc.readyState === 'complete' ? null : new Promise((done) => c.win.addEventListener('load', done, { once: true })),+ doc.fonts.ready,+ ]).then(() => true);+ pageLoads.set(doc, Promise.race([loaded, late]).then((ok) => {+ clearTimeout(timer);+ if (!ok) console.error(`clip "${c.id}" (${c.src}): its page did not finish loading within ${PAGE_LOAD_MS / 1000} s (a picture or font): drawn without it`);+ }));+ }+ return pageLoads.get(doc);+}+/*** Wake the videos and pages near t (those on at t always), and let those far from it rest. `only`: wake only those on* at t, and let none rest unless far too many are awake (a scrub: the rest is done once it stops, see draw). `exact`:@@ -990,6 +1017,8 @@ async function drawClip(c, t, live, play, turn, coming, going) {try { await wake(c); } catch (e) { if (live) return; throw new Error(`clip "${c.id}" (${c.src}): ${e?.message ?? e}`); }if (!c.child || !c.shown) return;}+ /* an exact frame shows the page's pictures: it waits for them, as a video clip's frame waits for its file */+ if (!live) await pageLoaded(c);const local = sourceAt(c.span, t - c.at, c.row, c.native);c.win.filmHost?.time?.(local);/* a page's error says which clip and which of its own seconds, not only the film's t */diff --git a/packages/openfilm/src/timeline.test.mjs b/packages/openfilm/src/timeline.test.mjsindex 390e81b..801ba00 100644--- a/packages/openfilm/src/timeline.test.mjs+++ b/packages/openfilm/src/timeline.test.mjs@@ -115,6 +115,27 @@ test('a page without a size, opened on its own, is 1920 × 1080', { timeout: 60_}});+test('an exact frame of a page waits for its pictures, though its film is ready without them', { timeout: 60_000 }, async () => {+ const dir = mkdtempSync(join(tmpdir(), 'of-picture-'));+ /* a picture that takes 2 s to come: the page's window.film is there long before */+ const slow = createServer((req, res) => setTimeout(() => res.writeHead(200, { 'content-type': 'image/svg+xml' })+ .end('<svg xmlns="http://www.w3.org/2000/svg" width="320" height="180"><rect width="320" height="180" fill="red"/></svg>'), 2000));+ await new Promise((done) => slow.listen(0, '127.0.0.1', done));+ writeFileSync(join(dir, 'p.html'), page('p', 2).replace('<p>p</p>', `<img src="http://127.0.0.1:${slow.address().port}/red.svg" style="display:block">`));+ writeFileSync(join(dir, 'film.html'), filmHtml({ stage: { w: 320, h: 180 }, tracks: [{ clips: [{ src: 'p.html', id: 'p' }] }] }));+ const s = await open(dir);+ try {+ await s.film.seek(1);+ const [r, g, b] = await middleColour(s.film);+ assert.ok(r > 200 && g < 80 && b < 80, `the page's picture, not its background (${r}, ${g}, ${b})`);+ } finally {+ await s.close();+ slow.closeAllConnections();+ await new Promise((done) => slow.close(done));+ rmSync(dir, { recursive: true, force: true });+ }+});+/* a page without a duration: it records the t it was last asked for */const endless = `<!doctype html><body style="margin:0;background:#246"><script>window.film={frame(t){window.lastT=t}}</script></body>`;