Skip to content

Commit 112ba53

Browse files
jrusso1020claude
andauthored
fix(studio): motion blur falls back for a WebGL canvas Studio reaches first (#7060)
* fix(core): motion blur notices a WebGL canvas the Studio page touched first When a same-origin host page (Studio) reaches a preview element before the scene does, for example through a MutationObserver record, Chrome gives that element the host's prototype. The scene's getContext("webgl") then ran the host's unpatched getContext, so the canvas never reached the accelerated canvas set, capture motion blur stayed on, and the WebGL canvas froze during preview playback. The tracker now records a canvas in the set of the canvas's own window, and installs itself in a same-origin parent too, compiled with the parent's own Function. Cross-origin parents are skipped. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * fix(studio): track accelerated canvases in Studio's window, not the embedder's The preview runtime no longer installs the canvas tracker in its parent window. That ran on every preview boot, so any same-origin page embedding the player got its getContext wrapped, and a host CSP without unsafe-eval reported a violation on each load. Studio now installs the tracker in its own window when its Player module loads, and core exposes it at @hyperframes/core/page-motion-blur. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * chore: keep the comment share of the touched files from rising Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 05ddddb commit 112ba53

7 files changed

Lines changed: 143 additions & 2 deletions

File tree

‎packages/core/package-subpaths.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@
134134
"types": "./dist/studioPreviewMark.d.ts",
135135
"environments": ["browser", "bun", "node"]
136136
},
137+
"./page-motion-blur": {
138+
"source": "./src/pageMotionBlur.ts",
139+
"runtime": "./dist/pageMotionBlur.js",
140+
"types": "./dist/pageMotionBlur.d.ts",
141+
"environments": ["browser", "bun", "node"]
142+
},
137143
"./keyframe-defaults": {
138144
"source": "./src/keyframeDefaults.ts",
139145
"runtime": "./dist/keyframeDefaults.js",

‎packages/core/package.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,12 @@
146146
"import": "./src/studioPreviewMark.ts",
147147
"types": "./src/studioPreviewMark.ts"
148148
},
149+
"./page-motion-blur": {
150+
"bun": "./src/pageMotionBlur.ts",
151+
"node": "./dist/pageMotionBlur.js",
152+
"import": "./src/pageMotionBlur.ts",
153+
"types": "./src/pageMotionBlur.ts"
154+
},
149155
"./keyframe-defaults": {
150156
"bun": "./src/keyframeDefaults.ts",
151157
"node": "./dist/keyframeDefaults.js",
@@ -712,6 +718,10 @@
712718
"import": "./dist/studioPreviewMark.js",
713719
"types": "./dist/studioPreviewMark.d.ts"
714720
},
721+
"./page-motion-blur": {
722+
"import": "./dist/pageMotionBlur.js",
723+
"types": "./dist/pageMotionBlur.d.ts"
724+
},
715725
"./keyframe-defaults": {
716726
"import": "./dist/keyframeDefaults.js",
717727
"types": "./dist/keyframeDefaults.d.ts"

‎packages/core/src/pageMotionBlur.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,44 @@ describe("accelerated canvas tracker", () => {
5656
canvas.getContext("webgl");
5757
expect(isAcceleratedCanvas(canvas)).toBe(true);
5858
});
59+
60+
it("tracks a scene canvas that has the host page's prototype", () => {
61+
const frame = document.createElement("iframe");
62+
document.body.append(frame);
63+
try {
64+
const scene = frame.contentWindow as (Window & typeof globalThis) | null;
65+
if (!scene) throw new Error("no iframe window");
66+
const sceneProto = scene.HTMLCanvasElement.prototype as unknown as Proto;
67+
expect(sceneProto === proto).toBe(false);
68+
sceneProto.getContext = vi.fn(() => ({}));
69+
// The host installs in its own window, as Studio does; the scene runs its own copy.
70+
installAcceleratedCanvasTracker();
71+
scene.eval(acceleratedCanvasTrackerSource());
72+
const canvas = scene.document.createElement("canvas");
73+
// Chrome gives a canvas the host's prototype when the host page reaches it first.
74+
Object.setPrototypeOf(canvas, HTMLCanvasElement.prototype);
75+
canvas.getContext("webgl2");
76+
const tracked: unknown = Reflect.get(scene, "__hfAcceleratedCanvases");
77+
expect((tracked as WeakSet<object>).has(canvas)).toBe(true);
78+
} finally {
79+
frame.remove();
80+
}
81+
});
82+
83+
it("leaves the page that embeds the scene alone", () => {
84+
const frame = document.createElement("iframe");
85+
document.body.append(frame);
86+
try {
87+
const scene = frame.contentWindow as (Window & typeof globalThis) | null;
88+
if (!scene) throw new Error("no iframe window");
89+
const hostGetContext = proto.getContext;
90+
const hostSet = Object.getOwnPropertyDescriptor(window, "__hfAcceleratedCanvases");
91+
scene.eval(acceleratedCanvasTrackerSource());
92+
expect(Reflect.get(scene, "__hfAcceleratedCanvases")).toBeDefined();
93+
expect(proto.getContext).toBe(hostGetContext);
94+
expect(Object.getOwnPropertyDescriptor(window, "__hfAcceleratedCanvases")).toEqual(hostSet);
95+
} finally {
96+
frame.remove();
97+
}
98+
});
5999
});

‎packages/core/src/pageMotionBlur.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@ export function installAcceleratedCanvasTracker(): void {
3535
Object.defineProperty(tracked, "__hfAccelTracked", { value: true });
3636
Object.defineProperty(proto, name, { value: tracked, writable: true, configurable: true });
3737
};
38+
const ownWindowSet = (canvas: object): WeakSet<object> => {
39+
const view = (canvas as Node).ownerDocument?.defaultView;
40+
const own: unknown = view ? Reflect.get(view, key) : undefined;
41+
return own && typeof (own as WeakSet<object>).add === "function"
42+
? (own as WeakSet<object>)
43+
: seen;
44+
};
3845
wrap("getContext", (canvas, args, ctx) => {
39-
if (ctx && accelerated.includes(String(args[0]))) seen.add(canvas);
46+
if (ctx && accelerated.includes(String(args[0]))) ownWindowSet(canvas).add(canvas);
4047
});
4148
wrap("transferControlToOffscreen", (canvas) => {
42-
seen.add(canvas);
49+
ownWindowSet(canvas).add(canvas);
4350
});
4451
}
4552

‎packages/engine/src/services/captureMotionBlur.pixels.browser.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Win = Window & {
3333
__hf_page_composite_resolve?: () => boolean;
3434
__hfMotionBlur?: {
3535
report: () => { state: string; tier: string | null; reasonCode: string | null; movers: number };
36+
probe: () => { state: string; tier: string | null; reasonCode: string | null; movers: number };
3637
};
3738
};
3839

@@ -301,6 +302,73 @@ describe.skipIf(!hasBrowser)("capture motion blur pixels in Chrome", () => {
301302
expect(ink(trail) / ink(row(sharp.frame, 40))).toBeLessThan(1.1);
302303
});
303304

305+
it("falls back for a WebGL canvas the host page reached first, as Studio does", async (ctx) => {
306+
if (!supported) ctx.skip();
307+
// The preview is a same-origin iframe. A scene mounts its markup, the host's MutationObserver
308+
// walks it (Studio's timeline does), and only then does the scene script call getContext.
309+
const scene = composition(
310+
`${BOX}<script>
311+
window.mountScene = () => document.getElementById("root").insertAdjacentHTML("beforeend", '<div id="scene"><canvas id="gl" width="64" height="36"></canvas></div>');
312+
window.startScene = () => document.getElementById("gl").getContext("webgl");
313+
</script>`,
314+
true,
315+
"capture",
316+
).replace("<head>", `<head><script>${acceleratedCanvasTrackerSource()}</script>`);
317+
const page = await browser.newPage();
318+
try {
319+
// Studio installs the tracker in its own window when it loads, before any preview exists.
320+
await page.setContent(
321+
`<script>${acceleratedCanvasTrackerSource()}</script><iframe id="preview" width="${W}" height="${H}"></iframe>`,
322+
);
323+
await page.$eval(
324+
"#preview",
325+
(el, html) =>
326+
new Promise((done) => {
327+
const frame = el as HTMLIFrameElement;
328+
frame.onload = done;
329+
frame.srcdoc = html;
330+
}),
331+
scene,
332+
);
333+
// The host observes the preview before the runtime boots, as Studio's player does: its
334+
// callback runs first and so makes the wrappers of what the scene mounts.
335+
await page.evaluate(() => {
336+
const frame = document.getElementById("preview") as HTMLIFrameElement;
337+
const reached: Element[] = [];
338+
Object.assign(window, { reached });
339+
new MutationObserver((records) => {
340+
for (const record of records)
341+
for (const node of Array.from(record.addedNodes))
342+
if (node instanceof Element) reached.push(...Array.from(node.children));
343+
}).observe(frame.contentDocument?.getElementById("root") as Element, { childList: true });
344+
});
345+
const preview = page.frames().find((f) => f !== page.mainFrame());
346+
if (!preview) throw new Error("no preview frame");
347+
await preview.addScriptTag({ content: readFileSync(RUNTIME, "utf8") });
348+
await preview.waitForFunction(
349+
() => (window as Win & { __playerReady?: boolean }).__playerReady === true,
350+
);
351+
const result = await page.evaluate(async () => {
352+
type Scene = Win & { mountScene: () => void; startScene: () => unknown };
353+
const frame = document.getElementById("preview") as HTMLIFrameElement;
354+
const view = frame.contentWindow as Scene;
355+
view.mountScene();
356+
await new Promise((done) => setTimeout(done, 0));
357+
const reached = (window as unknown as { reached: Element[] }).reached;
358+
const hostRealm = reached.some(
359+
(el) => el.id === "gl" && Object.getPrototypeOf(el) === HTMLCanvasElement.prototype,
360+
);
361+
view.startScene();
362+
return { hostRealm, report: view.__hfMotionBlur?.probe() ?? null };
363+
});
364+
// The canvas really carries the host's prototype, so its getContext runs in the host's realm.
365+
expect(result.hostRealm).toBe(true);
366+
expect(result.report).toMatchObject({ tier: "filter", reasonCode: "accelerated-canvas" });
367+
} finally {
368+
await page.close();
369+
}
370+
});
371+
304372
it.each(TIERS)(
305373
"blurs a box centred with translate around where it really is, in the %s tier",
306374
async (tier, ctx) => {

‎packages/studio/src/player/components/Player.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,10 @@ describe("composition loading overlay", () => {
526526
iframe.remove();
527527
});
528528
});
529+
530+
describe("accelerated canvases", () => {
531+
it("are tracked in Studio's window, whose prototype a preview canvas gets when Studio reaches it first", () => {
532+
expect("__hfAccelTracked" in HTMLCanvasElement.prototype.getContext).toBe(true);
533+
expect(Reflect.get(window, "__hfAcceleratedCanvases")).toBeInstanceOf(WeakSet);
534+
});
535+
});

‎packages/studio/src/player/components/Player.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { buildProjectApiPath } from "../../utils/projectRouting";
22
import { forwardRef, useEffect, useRef, useState } from "react";
33
import { isLottieAnimationLoaded } from "@hyperframes/core/runtime/lottie-readiness";
4+
import { installAcceleratedCanvasTracker } from "@hyperframes/core/page-motion-blur";
45
import { useMountEffect } from "../../hooks/useMountEffect";
56
import { applyPreviewVariablesToUrl } from "../../hooks/previewVariablesStore";
67
import { HyperframesLoader } from "../../components/ui";
@@ -25,6 +26,8 @@ export function loadPlayerModule(): Promise<unknown> {
2526

2627
if (typeof window !== "undefined") void loadPlayerModule().catch(() => {});
2728

29+
if (typeof window !== "undefined") installAcceleratedCanvasTracker();
30+
2831
interface PlayerProps {
2932
projectId?: string;
3033
directUrl?: string;

0 commit comments

Comments
 (0)