Skip to content

Commit 6555fd7

Browse files
authored
perf: inline html5 images at the size a scaled bake draws them (#268)
1 parent e0dd02a commit 6555fd7

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

‎src/components/canvas/players/html5-player.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,10 @@ export class Html5Player extends Player {
291291

292292
// Captured frames are SVG images, which can't load remote files, so ingest files are inlined as data URIs.
293293
// Callers await this only when the asset has ingest URLs, so other assets compose without yielding.
294-
private async composeWithIngestFiles(asset: Html5Asset): Promise<string> {
294+
// Every frame decodes its inlined images again, so a scaled bake inlines images sized for its scale.
295+
private async composeWithIngestFiles(asset: Html5Asset, scale: number = 1): Promise<string> {
295296
const longestEdge = html5ClipLongestEdge(this.clipConfiguration, this.edit.getResolvedEdit()?.output);
296-
return composeHtml5IframeSrcdoc(await inlineHtml5IngestAssets(asset, longestEdge));
297+
return composeHtml5IframeSrcdoc(await inlineHtml5IngestAssets(asset, longestEdge * scale));
297298
}
298299

299300
private async mountIframe(asset: Html5Asset): Promise<void> {
@@ -442,7 +443,7 @@ export class Html5Player extends Player {
442443
// An earlier capture may have seeked this iframe, and a seeked animation can leave state
443444
// a fresh page lacks (GSAP writes start values inline once a tween has
444445
// rendered). The backend captures from a fresh page, so capture does too.
445-
if (!(await this.loadFreshPage(this.iframe, stale))) return null;
446+
if (!(await this.loadFreshPage(this.iframe, stale, scale))) return null;
446447

447448
const { frameCount } = computeHtml5FrameCount({
448449
clipLengthSeconds: this.getLength(),
@@ -535,8 +536,9 @@ export class Html5Player extends Player {
535536
return true;
536537
}
537538

538-
private async loadFreshPage(iframe: HTMLIFrameElement, stale: () => boolean): Promise<boolean> {
539-
const srcdoc = findHtml5IngestUrls(this.asset).length > 0 ? await this.composeWithIngestFiles(this.asset) : composeHtml5IframeSrcdoc(this.asset);
539+
private async loadFreshPage(iframe: HTMLIFrameElement, stale: () => boolean, scale: number = 1): Promise<boolean> {
540+
const srcdoc =
541+
findHtml5IngestUrls(this.asset).length > 0 ? await this.composeWithIngestFiles(this.asset, scale) : composeHtml5IframeSrcdoc(this.asset);
540542
if (stale()) return false;
541543
iframe.srcdoc = srcdoc; // eslint-disable-line no-param-reassign -- reloading the iframe it is given is this helper's job
542544
await waitForIframeLoad(iframe, undefined, true);

‎tests/html5-player.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,20 @@ it("keys each bake by its scale", async () => {
938938
expect(jest.requireMock("@canvas/players/html5-cache").html5CachePut).toHaveBeenCalledWith("original@0.5", expect.anything());
939939
});
940940

941+
it("inlines ingest files sized for the bake's scale, and for full size in a still", async () => {
942+
const { player, harness } = createStillPlayer();
943+
shownAt(harness, 0.46);
944+
canvasLib.findHtml5IngestUrls.mockReturnValue([INGEST_URL]);
945+
canvasLib.inlineHtml5IngestAssets.mockImplementation(async asset => asset);
946+
947+
await harness.runCapture();
948+
expect(canvasLib.inlineHtml5IngestAssets).toHaveBeenLastCalledWith(expect.objectContaining({ type: "html5" }), 640);
949+
950+
await player.prepareStaticRender();
951+
player.endStaticRender();
952+
expect(canvasLib.inlineHtml5IngestAssets).toHaveBeenLastCalledWith(expect.objectContaining({ type: "html5" }), 1280);
953+
});
954+
941955
it("keeps stills and captures at full resolution while a scaled bake exists", async () => {
942956
const { player, harness } = createStillPlayer();
943957
Object.assign(harness, { capturedFrames: [new Blob(["baked 0"])], capturedHash: "original@0.5", capturedScale: 0.5 });

0 commit comments

Comments
 (0)