Skip to content

Write HTML. Render video. Built for agents.

feat(core): block capture motion blur on html-in-canvas defects (#5971)

Vance Ingalls536346c5d1e6
diff --git a/packages/core/package-subpaths.json b/packages/core/package-subpaths.json
index cc40df3544..1d5cfbef16 100644
--- a/packages/core/package-subpaths.json
+++ b/packages/core/package-subpaths.json
@@ -104,6 +104,12 @@
"types": "./dist/keyframeDefaults.d.ts",
"environments": ["browser", "bun", "node"]
},
+ "./html-in-canvas": {
+ "source": "./src/htmlInCanvas.ts",
+ "runtime": "./dist/htmlInCanvas.js",
+ "types": "./dist/htmlInCanvas.d.ts",
+ "environments": ["browser", "bun", "node"]
+ },
"./gsap-cdn": {
"source": "./src/gsapCdn.ts",
"runtime": "./dist/gsapCdn.js",
diff --git a/packages/core/package.json b/packages/core/package.json
index a3c5aa906d..c085f72543 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -116,6 +116,12 @@
"import": "./src/keyframeDefaults.ts",
"types": "./src/keyframeDefaults.ts"
},
+ "./html-in-canvas": {
+ "bun": "./src/htmlInCanvas.ts",
+ "node": "./dist/htmlInCanvas.js",
+ "import": "./src/htmlInCanvas.ts",
+ "types": "./src/htmlInCanvas.ts"
+ },
"./gsap-cdn": {
"bun": "./src/gsapCdn.ts",
"node": "./dist/gsapCdn.js",
@@ -614,6 +620,10 @@
"import": "./dist/keyframeDefaults.js",
"types": "./dist/keyframeDefaults.d.ts"
},
+ "./html-in-canvas": {
+ "import": "./dist/htmlInCanvas.js",
+ "types": "./dist/htmlInCanvas.d.ts"
+ },
"./gsap-cdn": {
"import": "./dist/gsapCdn.js",
"types": "./dist/gsapCdn.d.ts"
diff --git a/packages/core/src/htmlInCanvas.test.ts b/packages/core/src/htmlInCanvas.test.ts
new file mode 100644
index 0000000000..084840ff93
--- /dev/null
+++ b/packages/core/src/htmlInCanvas.test.ts
@@ -0,0 +1,107 @@
+// @vitest-environment happy-dom
+import { afterEach, describe, expect, it, vi } from "vitest";
+import {
+ htmlInCanvasForm,
+ markCaptureCanvas,
+ markCaptured,
+ unmarkCaptured,
+ CAPTURE_CANVAS_SELECTOR,
+} from "./htmlInCanvas";
+import { supportsCaptureMotionBlur } from "./pageMotionBlur";
+
+const canvasProto = HTMLCanvasElement.prototype as unknown as Record<string, unknown>;
+const CANVAS_MEMBERS = ["requestPaint", "layoutSubtree", "updateElementGeometry"];
+
+function install(canvasMembers: string[], drawElementImage = true): void {
+ vi.stubGlobal(
+ "CanvasRenderingContext2D",
+ drawElementImage
+ ? class {
+ drawElementImage(): void {}
+ }
+ : class {},
+ );
+ for (const name of canvasMembers) canvasProto[name] = () => {};
+}
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+ for (const name of CANVAS_MEMBERS) delete canvasProto[name];
+});
+
+describe("htmlInCanvasForm", () => {
+ it("is layoutsubtree where the canvas has layoutSubtree, even if it also has updateElementGeometry", () => {
+ install(["requestPaint", "layoutSubtree", "updateElementGeometry"]);
+ expect(htmlInCanvasForm()).toBe("layoutsubtree");
+ });
+
+ it("is drawable where layoutSubtree is gone and updateElementGeometry is there", () => {
+ install(["requestPaint", "updateElementGeometry"]);
+ expect(htmlInCanvasForm()).toBe("drawable");
+ });
+
+ it("is null when neither form is there", () => {
+ install(["requestPaint"]);
+ expect(htmlInCanvasForm()).toBeNull();
+ });
+
+ it("is null without drawElementImage or without requestPaint", () => {
+ install(["requestPaint", "layoutSubtree"], false);
+ expect(htmlInCanvasForm()).toBeNull();
+ vi.unstubAllGlobals();
+ delete canvasProto.requestPaint;
+ install(["layoutSubtree"]);
+ expect(htmlInCanvasForm()).toBeNull();
+ });
+});
+
+describe("supportsCaptureMotionBlur", () => {
+ it.each([
+ ["old form", ["requestPaint", "layoutSubtree"], true],
+ ["new form", ["requestPaint", "updateElementGeometry"], true],
+ ["API present but no known form", ["requestPaint"], false],
+ ])("%s", (_name, members, expected) => {
+ install(members);
+ expect(supportsCaptureMotionBlur()).toBe(expected);
+ });
+});
+
+describe("marking", () => {
+ it("marks the old form with layoutsubtree and leaves children alone", () => {
+ const canvas = document.createElement("canvas");
+ const child = document.createElement("div");
+ markCaptureCanvas(canvas, "layoutsubtree");
+ markCaptured(child, "layoutsubtree");
+ expect(canvas.hasAttribute("layoutsubtree")).toBe(true);
+ expect(canvas.hasAttribute("content")).toBe(false);
+ expect(child.hasAttribute("drawable")).toBe(false);
+ });
+
+ it("marks the new form with content=drawable and drawable children, and unmarks them", () => {
+ const canvas = document.createElement("canvas");
+ const child = document.createElement("div");
+ markCaptureCanvas(canvas, "drawable");
+ markCaptured(child, "drawable");
+ expect(canvas.getAttribute("content")).toBe("drawable");
+ expect(canvas.hasAttribute("layoutsubtree")).toBe(false);
+ expect(child.hasAttribute("drawable")).toBe(true);
+ unmarkCaptured(child, "drawable");
+ expect(child.hasAttribute("drawable")).toBe(false);
+ });
+
+ it("finds a nested capture canvas of either form", () => {
+ for (const mark of [
+ (c: HTMLElement) => c.setAttribute("layoutsubtree", ""),
+ (c: HTMLElement) => c.setAttribute("content", "drawable"),
+ ]) {
+ const root = document.createElement("div");
+ const canvas = document.createElement("canvas");
+ mark(canvas);
+ root.append(canvas);
+ expect(root.querySelector(CAPTURE_CANVAS_SELECTOR)).toBe(canvas);
+ }
+ const plain = document.createElement("div");
+ plain.append(document.createElement("canvas"));
+ expect(plain.querySelector(CAPTURE_CANVAS_SELECTOR)).toBeNull();
+ });
+});
diff --git a/packages/core/src/htmlInCanvas.ts b/packages/core/src/htmlInCanvas.ts
new file mode 100644
index 0000000000..a6f1dc0530
--- /dev/null
+++ b/packages/core/src/htmlInCanvas.ts
@@ -0,0 +1,29 @@
+/** Chrome <=155 marks a capture canvas with `layoutsubtree`; 157+ with `content="drawable"` and `drawable` on each captured child. */
+export type HtmlInCanvasForm = "layoutsubtree" | "drawable";
+
+export const CAPTURE_CANVAS_SELECTOR = 'canvas[layoutsubtree], canvas[content="drawable"]';
+
+/** The form this browser's html-in-canvas takes, or null when it has no usable one. */
+export function htmlInCanvasForm(win: Window = window): HtmlInCanvasForm | null {
+ const ctx = (win as Window & { CanvasRenderingContext2D?: { prototype: object } })
+ .CanvasRenderingContext2D?.prototype;
+ if (!ctx || !("drawElementImage" in ctx)) return null;
+ const canvas = win.document.createElement("canvas");
+ if (!("requestPaint" in canvas)) return null;
+ // 155 already has updateElementGeometry but still wants layoutsubtree, so this order matters.
+ if ("layoutSubtree" in canvas) return "layoutsubtree";
+ return "updateElementGeometry" in canvas ? "drawable" : null;
+}
+
+export function markCaptureCanvas(canvas: HTMLCanvasElement, form: HtmlInCanvasForm): void {
+ if (form === "layoutsubtree") canvas.setAttribute("layoutsubtree", "");
+ else canvas.setAttribute("content", "drawable");
+}
+
+export function markCaptured(el: Element, form: HtmlInCanvasForm): void {
+ if (form === "drawable") el.setAttribute("drawable", "");
+}
+
+export function unmarkCaptured(el: Element, form: HtmlInCanvasForm): void {
+ if (form === "drawable") el.removeAttribute("drawable");
+}
diff --git a/packages/core/src/htmlInCanvasDefects.test.ts b/packages/core/src/htmlInCanvasDefects.test.ts
new file mode 100644
index 0000000000..b7965abfdf
--- /dev/null
+++ b/packages/core/src/htmlInCanvasDefects.test.ts
@@ -0,0 +1,323 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import {
+ chromeMajor,
+ findCaptureDefect,
+ scanBackfaceDefect,
+ type DefectEnv,
+} from "./htmlInCanvasDefects";
+
+const ua = (major: number | null, headless = false) => ({
+ navigator: {
+ userAgent:
+ major === null
+ ? "Mozilla/5.0 HappyDOM"
+ : `Mozilla/5.0 (Macintosh) AppleWebKit/537.36 (KHTML, like Gecko) ${headless ? "HeadlessChrome" : "Chrome"}/${major}.0.0.0 Safari/537.36`,
+ },
+});
+
+function mount(html: string, rootStyle = ""): HTMLElement {
+ document.body.innerHTML = `<div id="root" style="${rootStyle}">${html}</div>`;
+ const root = document.getElementById("root");
+ if (!root) throw new Error("no root");
+ return root;
+}
+
+const env = (over: Partial<DefectEnv> = {}): DefectEnv => ({
+ isAccelerated: () => false,
+ backface: false,
+ ...over,
+});
+
+const code = (root: HTMLElement, e: DefectEnv) => findCaptureDefect(root, e)?.code ?? null;
+
+afterEach(() => {
+ document.body.innerHTML = document.head.innerHTML = "";
+ vi.unstubAllGlobals();
+ Reflect.deleteProperty(document, "getAnimations");
+});
+
+const animating = (target: Element, frames: object[]) => {
+ class Effect {
+ target = target;
+ getKeyframes = () => frames;
+ }
+ vi.stubGlobal("KeyframeEffect", Effect);
+ Object.assign(document, { getAnimations: () => [{ effect: new Effect() }] });
+};
+
+describe("chromeMajor", () => {
+ it("reads Chrome and HeadlessChrome, and is null for anything else", () => {
+ expect(chromeMajor(ua(152, true))).toBe(152);
+ expect(chromeMajor(ua(157))).toBe(157);
+ expect(chromeMajor(ua(null))).toBeNull();
+ });
+});
+
+describe("nested-capture-canvas", () => {
+ it("is found on every version, in either API form", () => {
+ for (const attr of ["layoutsubtree", 'content="drawable"']) {
+ const root = mount(`<canvas ${attr}></canvas>`);
+ for (const major of [152, 158, null]) {
+ expect(code(root, env({ win: ua(major) }))).toBe("nested-capture-canvas");
+ }
+ }
+ });
+});
+
+describe("accelerated-canvas", () => {
+ const gl = (root: HTMLElement) => env({ isAccelerated: (c) => root.contains(c) });
+
+ it("applies below 157 and when the version is unknown, not from 157", () => {
+ const root = mount("<canvas></canvas>");
+ for (const major of [152, 155, null]) {
+ expect(code(root, { ...gl(root), win: ua(major) })).toBe("accelerated-canvas");
+ }
+ for (const major of [157, 158]) expect(code(root, { ...gl(root), win: ua(major) })).toBeNull();
+ });
+
+ it("ignores a canvas the page did not give a WebGL context", () => {
+ expect(code(mount("<canvas></canvas>"), env({ win: ua(152) }))).toBeNull();
+ });
+});
+
+describe("iframe-in-root", () => {
+ it("applies on every version: a reload cannot be undone", () => {
+ const root = mount('<div><iframe src="about:blank"></iframe></div>');
+ for (const major of [152, 157, 158])
+ expect(code(root, env({ win: ua(major) }))).toBe("iframe-in-root");
+ });
+
+ it("is not reported for a composition without an iframe", () => {
+ expect(code(mount("<div><img></div>"), env())).toBeNull();
+ });
+});
+
+describe("root-transform", () => {
+ it("applies below 157 and when the version is unknown, not from 157", () => {
+ const root = mount("", "transform: scale(0.5)");
+ expect(code(root, env({ win: ua(152) }))).toBe("root-transform");
+ expect(code(root, env({ win: ua(156) }))).toBe("root-transform");
+ expect(code(root, env({ win: ua(null) }))).toBe("root-transform");
+ expect(code(root, env({ win: ua(157) }))).toBeNull();
+ expect(code(root, env({ win: ua(158) }))).toBeNull();
+ });
+
+ it("ignores a root without a transform", () => {
+ expect(code(mount(""), env({ win: ua(152) }))).toBeNull();
+ });
+});
+
+describe("root-opacity", () => {
+ it("applies from 155 and when the version is unknown, not on 152", () => {
+ const root = mount("", "opacity: 0.5");
+ expect(code(root, env({ win: ua(152) }))).toBeNull();
+ expect(code(root, env({ win: ua(155) }))).toBe("root-opacity");
+ expect(code(root, env({ win: ua(158) }))).toBe("root-opacity");
+ expect(code(root, env({ win: ua(null) }))).toBe("root-opacity");
+ });
+
+ it("ignores a fully opaque root", () => {
+ expect(code(mount("", "opacity: 1"), env({ win: ua(158) }))).toBeNull();
+ });
+});
+
+describe("backface-visibility", () => {
+ const face = "backface-visibility: hidden";
+
+ it("is found on every version once the scan saw one", () => {
+ const root = mount("");
+ for (const major of [152, 155, 157, 158, null]) {
+ expect(code(root, env({ backface: true, win: ua(major) }))).toBe("backface-visibility");
+ }
+ });
+
+ it("scan: a face inside a preserve-3d card", () => {
+ const root = mount(
+ `<div style="transform-style: preserve-3d"><div style="${face}"></div></div>`,
+ );
+ expect(scanBackfaceDefect(root)).toBe(true);
+ });
+
+ it("scan: a face under a perspective ancestor", () => {
+ const root = mount(`<div style="perspective: 800px"><div style="${face}"></div></div>`);
+ expect(scanBackfaceDefect(root)).toBe(true);
+ });
+
+ it("scan: a face with its own 3D rotation", () => {
+ const root = mount(`<div style="${face}; transform: rotateY(180deg)"></div>`);
+ expect(scanBackfaceDefect(root)).toBe(true);
+ });
+
+ it("scan: the -webkit- spelling counts", () => {
+ const root = mount(
+ '<div style="transform-style: preserve-3d"><div style="-webkit-backface-visibility: hidden"></div></div>',
+ );
+ expect(scanBackfaceDefect(root)).toBe(true);
+ });
+
+ it("scan: a face found through a stylesheet rule, including inside @media", () => {
+ document.head.innerHTML =
+ "<style>.card{transform-style:preserve-3d}@media screen{.face{backface-visibility:hidden}}</style>";
+ const root = mount('<div class="card"><div class="face"></div></div>');
+ expect(scanBackfaceDefect(root)).toBe(true);
+ });
+
+ it("scan: the flat performance hack is not a defect", () => {
+ document.head.innerHTML = "<style>.perf{backface-visibility:hidden}</style>";
+ const root = mount(
+ `<div class="perf"></div><div style="${face}; transform: translate3d(0,0,0)"></div>`,
+ );
+ expect(scanBackfaceDefect(root)).toBe(false);
+ });
+
+ it("scan: a 3D card without a hidden face is fine", () => {
+ const root = mount('<div style="transform-style: preserve-3d"><div></div></div>');
+ expect(scanBackfaceDefect(root)).toBe(false);
+ });
+
+ it("scan: a selector that cannot be queried is skipped", () => {
+ document.head.innerHTML = "<style>.x::before{backface-visibility:hidden}</style>";
+ expect(scanBackfaceDefect(mount("<div class='x'></div>"))).toBe(false);
+ });
+
+ it("scan: a 3D context outside the root does not count", () => {
+ document.body.innerHTML = `<div style="transform-style: preserve-3d"><div id="root"><div style="${face}"></div></div></div>`;
+ const root = document.getElementById("root");
+ if (!root) throw new Error("no root");
+ expect(scanBackfaceDefect(root)).toBe(false);
+ });
+});
+
+describe("backface-visibility rules the page reaches indirectly", () => {
+ const withProps = (proto: object, props: Record<string, unknown>) =>
+ Object.create(
+ proto,
+ Object.fromEntries(Object.entries(props).map(([k, value]) => [k, { value }])),
+ );
+ const rule = (selectorText: string, hidden: boolean, cssRules: unknown[] = []) =>
+ withProps(CSSStyleRule.prototype, {
+ selectorText,
+ cssRules,
+ style: {
+ getPropertyValue: (n: string) => (hidden && n === "backface-visibility" ? "hidden" : ""),
+ },
+ });
+ const imported = (styleSheet: unknown) => withProps(CSSImportRule.prototype, { styleSheet });
+ const sheetOf = (...cssRules: unknown[]) => ({ cssRules });
+ const realComputed = window.getComputedStyle.bind(window);
+ // jsdom computes no style from the stubbed sheets, so `.face` always computes hidden: what is
+ // left to prove is that the scan picks the element as a candidate through the sheet.
+ const useSheets = (...sheets: unknown[]) => {
+ vi.spyOn(document, "styleSheets", "get").mockReturnValue(sheets as never);
+ vi.spyOn(window, "getComputedStyle").mockImplementation((el) => {
+ const cs = realComputed(el);
+ if (!el.classList.contains("face")) return cs;
+ return new Proxy(cs, {
+ get: (target, key) =>
+ key === "getPropertyValue"
+ ? (n: string) => (n === "backface-visibility" ? "hidden" : target.getPropertyValue(n))
+ : Reflect.get(target, key),
+ });
+ });
+ };
+ const card = (inner = '<div class="face"></div>') =>
+ mount(`<div class="card" style="transform-style: preserve-3d">${inner}</div>`);
+
+ afterEach(() => vi.restoreAllMocks());
+
+ it("finds a face hidden by a nested rule, with or without &", () => {
+ for (const nested of ["& .face", ".face", "&>.face"]) {
+ useSheets(sheetOf(rule(".card", false, [rule(nested, true)])));
+ expect(scanBackfaceDefect(card())).toBe(true);
+ }
+ });
+
+ it("resolves a nested rule against its parent: a face elsewhere is not found", () => {
+ for (const nested of ["& .face", ".face"]) {
+ useSheets(sheetOf(rule(".other", false, [rule(nested, true)])));
+ expect(scanBackfaceDefect(card())).toBe(false);
+ }
+ });
+
+ it("walks nested rules several levels down, and each selector of a nested list", () => {
+ useSheets(
+ sheetOf(rule(".card", false, [rule(".a, .b", false, [rule("& .face, & .x", true)])])),
+ );
+ expect(scanBackfaceDefect(card('<div class="a"><div class="face"></div></div>'))).toBe(true);
+ });
+
+ it("finds a face hidden by an @imported sheet", () => {
+ useSheets(sheetOf(imported(sheetOf(rule(".face", true)))));
+ expect(scanBackfaceDefect(card())).toBe(true);
+ });
+
+ it("skips an @import that is not loaded or that the page may not read", () => {
+ const blocked = {
+ get cssRules(): never {
+ throw new DOMException("cross-origin", "SecurityError");
+ },
+ };
+ useSheets(sheetOf(imported(null), imported(blocked), rule(".face", true)));
+ expect(scanBackfaceDefect(card())).toBe(true);
+ });
+
+ it("reads a sheet that imports itself once", () => {
+ let reads = 0;
+ const loop = {
+ get cssRules(): unknown[] {
+ reads++;
+ return [imported(loop), rule(".face", true)];
+ },
+ };
+ useSheets(loop);
+ expect(scanBackfaceDefect(card())).toBe(true);
+ expect(reads).toBe(1);
+ });
+
+ it("stops following @import chains past a fixed depth", () => {
+ let chain = sheetOf(rule(".face", true));
+ for (let i = 0; i < 40; i++) chain = sheetOf(imported(chain));
+ useSheets(chain);
+ expect(scanBackfaceDefect(card())).toBe(false);
+ });
+
+ it("finds a face whose backface-visibility comes from @keyframes, named or not yet running", () => {
+ document.head.innerHTML = "<style>@keyframes flip{to{backface-visibility:hidden}}</style>";
+ useSheets(...Array.from(document.styleSheets));
+ const face = (name: string) => card(`<div class="face" style="animation-name: ${name}"></div>`);
+ expect(scanBackfaceDefect(face("flip"))).toBe(true);
+ expect(scanBackfaceDefect(face("other, flip"))).toBe(true);
+ expect(scanBackfaceDefect(face("other"))).toBe(false);
+ });
+
+ it("finds a face a running animation keys to hidden, inside the root only", () => {
+ const root = card();
+ const face = root.querySelector(".face") ?? root;
+ const outside = document.body.appendChild(document.createElement("div"));
+ outside.className = "face";
+ outside.style.transformStyle = "preserve-3d";
+ useSheets();
+ for (const [frames, el, expected] of [
+ [{ backfaceVisibility: "hidden" }, face, true],
+ [{ backfaceVisibility: "visible" }, face, false],
+ [{ backfaceVisibility: "hidden" }, outside, false],
+ ] as const) {
+ animating(el, [frames]);
+ expect(scanBackfaceDefect(root)).toBe(expected);
+ }
+ });
+
+ it("still leaves the flat backface hack alone when written as a nested rule", () => {
+ useSheets(sheetOf(rule(".card", false, [rule("& .face", true)])));
+ expect(scanBackfaceDefect(mount('<div class="card"><div class="face"></div></div>'))).toBe(
+ false,
+ );
+ });
+});
+
+describe("order", () => {
+ it("reports the first applicable defect", () => {
+ const root = mount("<canvas layoutsubtree></canvas>", "opacity: 0.5");
+ expect(code(root, env({ win: ua(158), backface: true }))).toBe("nested-capture-canvas");
+ });
+});
diff --git a/packages/core/src/htmlInCanvasDefects.ts b/packages/core/src/htmlInCanvasDefects.ts
new file mode 100644
index 0000000000..fa30b9d269
--- /dev/null
+++ b/packages/core/src/htmlInCanvasDefects.ts
@@ -0,0 +1,289 @@
+import { CAPTURE_CANVAS_SELECTOR } from "./htmlInCanvas";
+
+type UaWindow = { navigator: { userAgent: string } };
+
+/** Major Chrome version from the user agent (HeadlessChrome included), or null when unknown. */
+export function chromeMajor(win: UaWindow = window): number | null {
+ const m = /Chrome\/(\d+)/.exec(win.navigator.userAgent);
+ return m?.[1] ? Number(m[1]) : null;
+}
+
+export interface CaptureDefect {
+ code: string;
+ reason: string;
+}
+
+export interface DefectEnv {
+ isAccelerated: (canvas: HTMLCanvasElement) => boolean;
+ /** Result of `scanBackfaceDefect`, refreshed whenever the scene is rescanned. */
+ backface: boolean;
+ win?: UaWindow;
+}
+
+interface Defect {
+ code: string;
+ reason: string;
+ /** Chrome majors the defect applies to; null bounds are open. */
+ from: number | null;
+ until: number | null;
+ detect: (root: HTMLElement, env: DefectEnv) => boolean;
+}
+
+const hasHidden3dFace = (_root: HTMLElement, env: DefectEnv): boolean => env.backface;
+
+const hasAcceleratedCanvas = (root: HTMLElement, env: DefectEnv): boolean =>
+ Array.from(root.querySelectorAll("canvas")).some(env.isAccelerated);
+
+const hasRootTransform = (root: HTMLElement): boolean => {
+ const t = getComputedStyle(root).transform;
+ return t !== "none" && t !== "";
+};
+
+const hasRootOpacity = (root: HTMLElement): boolean =>
+ Number.parseFloat(getComputedStyle(root).opacity) < 1;
+
+const hasIframe = (root: HTMLElement): boolean => root.querySelector("iframe") !== null;
+
+// Chrome <=156 captures a WebGL canvas frozen at its pre-wrap frame; 157 fixed it.
+const DEFECTS: readonly Defect[] = [
+ {
+ code: "nested-capture-canvas",
+ reason: "the composition already uses html-in-canvas",
+ from: null,
+ until: null,
+ detect: (root) => root.querySelector(CAPTURE_CANVAS_SELECTOR) !== null,
+ },
+ {
+ code: "accelerated-canvas",
+ reason: "the composition draws a WebGL/WebGPU canvas, which html-in-canvas captures frozen",
+ from: null,
+ until: 157,
+ detect: hasAcceleratedCanvas,
+ },
+ {
+ code: "backface-visibility",
+ reason: "backface-visibility: hidden inside a 3D transform is drawn mirrored by html-in-canvas",
+ from: null,
+ until: null,
+ detect: hasHidden3dFace,
+ },
+ {
+ code: "iframe-in-root",
+ reason: "the composition contains an <iframe>, which reloads whenever motion blur wraps it",
+ from: null,
+ until: null,
+ detect: hasIframe,
+ },
+ {
+ code: "root-transform",
+ reason: "html-in-canvas ignores a transform on the composition root in this Chrome version",
+ from: null,
+ until: 157,
+ detect: hasRootTransform,
+ },
+ {
+ code: "root-opacity",
+ reason: "html-in-canvas ignores the composition root's own opacity in this Chrome version",
+ from: 155,
+ until: null,
+ detect: hasRootOpacity,
+ },
+];
+
+function applies(defect: Defect, major: number | null): boolean {
+ if (major === null) return true;
+ return (
+ (defect.from === null || major >= defect.from) &&
+ (defect.until === null || major < defect.until)
+ );
+}
+
+/** The first defect that makes html-in-canvas capture this root wrongly in this browser, or null. */
+export function findCaptureDefect(root: HTMLElement, env: DefectEnv): CaptureDefect | null {
+ const major = chromeMajor(env.win);
+ for (const defect of DEFECTS) {
+ if (applies(defect, major) && defect.detect(root, env)) {
+ return { code: defect.code, reason: defect.reason };
+ }
+ }
+ return null;
+}
+
+const FACE_3D_TRANSFORM = /perspective\(|rotate3d\(|rotate[XY]\(|matrix3d\(/;
+
+function isHidden(cs: CSSStyleDeclaration): boolean {
+ return (
+ cs.getPropertyValue("backface-visibility") === "hidden" ||
+ cs.getPropertyValue("-webkit-backface-visibility") === "hidden"
+ );
+}
+
+function transformsIn3d(el: HTMLElement, cs: CSSStyleDeclaration): boolean {
+ return FACE_3D_TRANSFORM.test(cs.transform) || FACE_3D_TRANSFORM.test(el.style.transform);
+}
+
+function in3dContext(el: HTMLElement, root: HTMLElement): boolean {
+ for (let node: HTMLElement | null = el; node; node = node.parentElement) {
+ const cs = getComputedStyle(node);
+ if (cs.transformStyle === "preserve-3d" || transformsIn3d(node, cs)) return true;
+ if (node !== el && cs.perspective !== "none" && cs.perspective !== "") return true;
+ if (node === root) break;
+ }
+ return false;
+}
+
+interface StyleBlock {
+ selector: string;
+ style: CSSStyleDeclaration;
+}
+
+const MAX_RULE_DEPTH = 16;
+
+const KF = "@keyframes ";
+
+function splitSelectorList(list: string): string[] {
+ const parts: string[] = [];
+ let depth = 0;
+ let from = 0;
+ for (let i = 0; i < list.length; i++) {
+ const ch = list.charAt(i);
+ if (ch === "(" || ch === "[") depth++;
+ else if (ch === ")" || ch === "]") depth--;
+ else if (ch === "," && depth === 0) {
+ parts.push(list.slice(from, i).trim());
+ from = i + 1;
+ }
+ }
+ parts.push(list.slice(from).trim());
+ return parts;
+}
+
+/** A nested rule's selector made absolute: `&` is the parent, and no `&` means a descendant. */
+function resolveNested(selector: string, parent: string | null): string {
+ if (parent === null) return selector;
+ const outer = `:is(${parent})`;
+ return splitSelectorList(selector)
+ .map((part) => (part.includes("&") ? part.replaceAll("&", outer) : `${outer} ${part}`))
+ .join(", ");
+}
+
+function* styleBlocks(
+ rules: CSSRuleList,
+ parent: string | null,
+ seen: Set<CSSStyleSheet>,
+ depth: number,
+): Generator<StyleBlock> {
+ if (depth > MAX_RULE_DEPTH) return;
+ for (const rule of Array.from(rules)) yield* ruleBlocks(rule, parent, seen, depth);
+}
+
+function nestedDeclarations(rule: CSSRule, parent: string | null): StyleBlock | null {
+ const style: unknown = "style" in rule ? rule.style : null;
+ return parent !== null && style instanceof CSSStyleDeclaration
+ ? { selector: parent, style }
+ : null;
+}
+
+function* ruleBlocks(
+ rule: CSSRule,
+ parent: string | null,
+ seen: Set<CSSStyleSheet>,
+ depth: number,
+): Generator<StyleBlock> {
+ if (rule instanceof CSSStyleRule) {
+ const selector = resolveNested(rule.selectorText, parent);
+ yield { selector, style: rule.style };
+ yield* styleBlocks(rule.cssRules, selector, seen, depth + 1);
+ return;
+ }
+ if (rule instanceof CSSImportRule) {
+ if (rule.styleSheet) yield* sheetBlocks(rule.styleSheet, seen, depth + 1);
+ return;
+ }
+ const declarations = nestedDeclarations(rule, parent);
+ if (declarations) yield declarations;
+ else if ("cssRules" in rule && rule.cssRules instanceof CSSRuleList) {
+ const scope = rule instanceof CSSKeyframesRule ? KF + rule.name : parent;
+ yield* styleBlocks(rule.cssRules, scope, seen, depth + 1);
+ }
+}
+
+function sheetRules(sheet: CSSStyleSheet): CSSRuleList | null {
+ try {
+ return sheet.cssRules;
+ } catch {
+ return null;
+ }
+}
+
+function* sheetBlocks(
+ sheet: CSSStyleSheet,
+ seen: Set<CSSStyleSheet>,
+ depth: number,
+): Generator<StyleBlock> {
+ const rules = seen.has(sheet) ? null : sheetRules(sheet);
+ seen.add(sheet);
+ if (rules) yield* styleBlocks(rules, null, seen, depth);
+}
+
+function matching(root: HTMLElement, selector: string): HTMLElement[] {
+ try {
+ const found = Array.from(root.querySelectorAll<HTMLElement>(selector));
+ return root.matches(selector) ? [root, ...found] : found;
+ } catch {
+ return [];
+ }
+}
+
+function runningFaces(root: HTMLElement): HTMLElement[] {
+ return (document.getAnimations?.() ?? []).flatMap(({ effect }) => {
+ if (!(effect instanceof KeyframeEffect)) return [];
+ const el = effect.target;
+ const keyed = effect.getKeyframes().some((k) => k.backfaceVisibility === "hidden");
+ return keyed && el instanceof HTMLElement && root.contains(el) ? [el] : [];
+ });
+}
+
+function namedFaces(root: HTMLElement, keyframed: string[]): HTMLElement[] {
+ if (keyframed.length === 0) return [];
+ return [root, ...root.querySelectorAll<HTMLElement>("*")].filter((el) =>
+ getComputedStyle(el)
+ .animationName.split(", ")
+ .some((n) => keyframed.includes(KF + n)),
+ );
+}
+
+function hiddenSelectors(): string[] {
+ const seen = new Set<CSSStyleSheet>();
+ const sheets = [...Array.from(document.styleSheets), ...(document.adoptedStyleSheets ?? [])];
+ const out: string[] = [];
+ for (const sheet of sheets) {
+ for (const block of sheetBlocks(sheet, seen, 0))
+ if (isHidden(block.style)) out.push(block.selector);
+ }
+ return out;
+}
+
+function backfaceCandidates(root: HTMLElement, selectors: string[]): Set<HTMLElement> {
+ const keyframed = selectors.filter((s) => s.startsWith(KF));
+ const found = new Set<HTMLElement>([
+ ...root.querySelectorAll<HTMLElement>('[style*="backface"]'),
+ ...runningFaces(root),
+ ...namedFaces(root, keyframed),
+ ]);
+ for (const selector of selectors) for (const el of matching(root, selector)) found.add(el);
+ return found;
+}
+
+/**
+ * A `backface-visibility: hidden` element in a 3D context (preserve-3d, perspective or a 3D
+ * transform), which html-in-canvas draws anyway. Flat uses are a harmless performance hack.
+ * Residual: a rotation that only appears later, via a tween on an element with no 3D context at
+ * scan time, is not seen.
+ */
+export function scanBackfaceDefect(root: HTMLElement): boolean {
+ for (const el of backfaceCandidates(root, hiddenSelectors())) {
+ if (isHidden(getComputedStyle(el)) && in3dContext(el, root)) return true;
+ }
+ return false;
+}
diff --git a/packages/core/src/pageMotionBlur.ts b/packages/core/src/pageMotionBlur.ts
index 9169613f1e..6ba2617415 100644
--- a/packages/core/src/pageMotionBlur.ts
+++ b/packages/core/src/pageMotionBlur.ts
@@ -1,16 +1,11 @@
-// Set by the engine: its own sub-frame blur runs, or the Studio preview could not blur.
-export const PAGE_MOTION_BLUR_OFF_GLOBAL = "__HF_PAGE_MOTION_BLUR_OFF__";
+import { htmlInCanvasForm } from "./htmlInCanvas.js";
+// Engine-set: the off switch (its sub-frame blur runs, or the preview could not blur); the render scale (dpr falls back to 1).
+export const PAGE_MOTION_BLUR_OFF_GLOBAL = "__HF_PAGE_MOTION_BLUR_OFF__";
export const PAGE_DEVICE_SCALE_GLOBAL = "__HF_DEVICE_SCALE_FACTOR__";
export function supportsCaptureMotionBlur(win: Window = window): boolean {
- const w = win as Window & {
- CanvasRenderingContext2D?: { prototype: object };
- HTMLCanvasElement?: { prototype: object };
- };
- const ctx = w.CanvasRenderingContext2D?.prototype;
- const canvas = w.HTMLCanvasElement?.prototype;
- return !!ctx && !!canvas && "drawElementImage" in ctx && "requestPaint" in canvas;
+ return htmlInCanvasForm(win) !== null;
}
const ACCELERATED_CANVASES_GLOBAL = "__hfAcceleratedCanvases";
diff --git a/packages/core/src/runtime/captureMotionBlur/index.test.ts b/packages/core/src/runtime/captureMotionBlur/index.test.ts
index 3cc52b744b..7638881a1f 100644
--- a/packages/core/src/runtime/captureMotionBlur/index.test.ts
+++ b/packages/core/src/runtime/captureMotionBlur/index.test.ts
@@ -40,7 +40,7 @@ function fakeContext() {
let contexts: Array<ReturnType<typeof fakeContext>> = [];
const captureContext = () => contexts[0];
-function installHtmlInCanvas(): void {
+function installHtmlInCanvas(form: "old" | "new" = "old"): void {
vi.spyOn(HTMLCanvasElement.prototype, "getContext").mockImplementation(() => {
const ctx = fakeContext();
contexts.push(ctx);
@@ -52,7 +52,10 @@ function installHtmlInCanvas(): void {
drawElementImage(): void {}
},
);
- (HTMLCanvasElement.prototype as unknown as Record<string, unknown>).requestPaint = () => {};
+ const canvasProto = HTMLCanvasElement.prototype as unknown as Record<string, unknown>;
+ canvasProto.requestPaint = () => {};
+ if (form === "old") canvasProto.layoutSubtree = false;
+ else canvasProto.updateElementGeometry = () => {};
vi.stubGlobal(
"OffscreenCanvas",
class {
@@ -99,7 +102,9 @@ afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
vi.useRealTimers();
- delete (HTMLCanvasElement.prototype as unknown as Record<string, unknown>).requestPaint;
+ for (const name of ["requestPaint", "layoutSubtree", "updateElementGeometry"]) {
+ delete (HTMLCanvasElement.prototype as unknown as Record<string, unknown>)[name];
+ }
delete win.__HF_PAGE_MOTION_BLUR_OFF__;
delete win.__hfMotionBlur;
delete win.__hf_page_composite_pending;
@@ -234,6 +239,72 @@ describe("opt-in and off switches", () => {
expect(win.__hfMotionBlur?.report().reason).toMatch(/WebGL/);
});
+ it("stays off for an iframe in the root, which reloads when the root is wrapped", () => {
+ installHtmlInCanvas();
+ const root = makeRoot();
+ root.append(document.createElement("iframe"));
+ initCaptureMotionBlur(host(root));
+ expect(root.parentElement).toBe(document.body);
+ expect(win.__hfMotionBlur?.report()).toMatchObject({ state: "off" });
+ expect(win.__hfMotionBlur?.report().reason).toMatch(/iframe/);
+ });
+
+ it("turns off instead of wrapping when an iframe appears after init", () => {
+ installHtmlInCanvas();
+ const { root, h } = movingHost();
+ initCaptureMotionBlur(h);
+ root.append(document.createElement("iframe"));
+ void paintCaptureMotionBlur(0.5, PLAY);
+ expect(root.parentElement).toBe(document.body);
+ expect(win.__hfMotionBlur?.report().reason).toMatch(/iframe/);
+ });
+
+ it("stays off for a hidden-backface card in a 3D context, but not for a flat one", () => {
+ installHtmlInCanvas();
+ const flat = makeRoot();
+ flat.innerHTML = '<div style="backface-visibility: hidden"></div>';
+ initCaptureMotionBlur(host(flat));
+ expect(win.__hfMotionBlur?.report().state).toBe("on");
+
+ resetCaptureMotionBlurForTests();
+ flat.innerHTML =
+ '<div style="transform-style: preserve-3d"><div style="backface-visibility: hidden"></div></div>';
+ initCaptureMotionBlur(host(flat));
+ expect(win.__hfMotionBlur?.report().state).toBe("off");
+ expect(win.__hfMotionBlur?.report().reason).toMatch(/backface-visibility/);
+ });
+
+ it("turns off instead of wrapping when the root gets a transform or opacity after init", () => {
+ installHtmlInCanvas();
+ for (const style of ["transform: scale(0.5)", "opacity: 0.5"]) {
+ const { root, h } = movingHost();
+ initCaptureMotionBlur(h);
+ root.setAttribute("style", style);
+ void paintCaptureMotionBlur(0.5, { engineMode: true });
+ expect(root.parentElement).toBe(document.body);
+ expect(win.__hfMotionBlur?.report().state).toBe("off");
+ expect(win.__hfMotionBlur?.report().reason).toMatch(/root/);
+ resetCaptureMotionBlurForTests();
+ document.body.innerHTML = "";
+ }
+ });
+
+ it("rescans for a hidden-backface card when the timeline changes", () => {
+ installHtmlInCanvas();
+ const { root, h } = movingHost();
+ let tl = "a";
+ initCaptureMotionBlur({ ...h, timeline: () => tl });
+ void paintCaptureMotionBlur(0.5, { engineMode: true });
+ expect(win.__hfMotionBlur?.report().state).toBe("on");
+ root.insertAdjacentHTML(
+ "beforeend",
+ '<div style="transform-style: preserve-3d"><div style="backface-visibility: hidden"></div></div>',
+ );
+ tl = "b";
+ void paintCaptureMotionBlur(0.6, { engineMode: true });
+ expect(win.__hfMotionBlur?.report().reason).toMatch(/backface-visibility/);
+ });
+
it("clamps the shutter angle", () => {
installHtmlInCanvas();
const root = makeRoot({ "data-motion-blur": "on", "data-motion-blur-shutter": "9999" });
@@ -252,6 +323,8 @@ describe("wrapping", () => {
const src = captureCanvas(root);
expect(src.tagName).toBe("CANVAS");
expect(src.hasAttribute("layoutsubtree")).toBe(true);
+ expect(src.hasAttribute("content")).toBe(false);
+ expect(root.hasAttribute("drawable")).toBe(false);
expect(src.hasAttribute("data-hf-ignore")).toBe(true);
expect([src.width, src.height]).toEqual([640, 360]);
const out = src.nextElementSibling as HTMLElement;
@@ -260,6 +333,33 @@ describe("wrapping", () => {
expect(out.style.pointerEvents).toBe("none");
});
+ it("marks the canvas and the root for the content=drawable form, and unmarks on unwrap", () => {
+ installHtmlInCanvas("new");
+ const { root, h } = movingHost();
+ initCaptureMotionBlur(h);
+ void paintCaptureMotionBlur(0.5, PLAY);
+ const src = captureCanvas(root);
+ expect(src.getAttribute("content")).toBe("drawable");
+ expect(src.hasAttribute("layoutsubtree")).toBe(false);
+ expect(root.hasAttribute("drawable")).toBe(true);
+ root.removeAttribute("data-motion-blur");
+ initCaptureMotionBlur(h);
+ expect(root.parentElement).toBe(document.body);
+ expect(root.hasAttribute("drawable")).toBe(false);
+ });
+
+ it("turns off when a content=drawable capture canvas appears after init", () => {
+ installHtmlInCanvas("new");
+ const { root, h } = movingHost();
+ initCaptureMotionBlur(h);
+ const late = document.createElement("canvas");
+ late.setAttribute("content", "drawable");
+ root.append(late);
+ void paintCaptureMotionBlur(0.5, PLAY);
+ expect(root.parentElement).toBe(document.body);
+ expect(win.__hfMotionBlur?.report().state).toBe("off");
+ });
+
it("unwraps when the root opts back out", () => {
installHtmlInCanvas();
const { root, h } = movingHost();
diff --git a/packages/core/src/runtime/captureMotionBlur/index.ts b/packages/core/src/runtime/captureMotionBlur/index.ts
index fbda3c223f..aac89d9ffc 100644
--- a/packages/core/src/runtime/captureMotionBlur/index.ts
+++ b/packages/core/src/runtime/captureMotionBlur/index.ts
@@ -11,6 +11,14 @@
* Needs html-in-canvas (`drawElementImage`); without it nothing changes.
*/
+import {
+ htmlInCanvasForm,
+ markCaptureCanvas,
+ markCaptured,
+ unmarkCaptured,
+ type HtmlInCanvasForm,
+} from "../../htmlInCanvas";
+import { type DefectEnv, findCaptureDefect, scanBackfaceDefect } from "../../htmlInCanvasDefects";
import {
PAGE_DEVICE_SCALE_GLOBAL,
PAGE_MOTION_BLUR_OFF_GLOBAL,
@@ -110,16 +118,8 @@ function readConfig(root: HTMLElement): Config | null {
/** Must run before composition scripts: see `installAcceleratedCanvasTracker`. */
export const trackAcceleratedCanvases = installAcceleratedCanvasTracker;
-/** What in the composition the capture cannot take, checked again per frame (scripts add canvases). */
-function captureBlocker(root: HTMLElement): string | null {
- // A capture canvas inside another one blocks the renderer's main thread.
- if (root.querySelector("canvas[layoutsubtree]"))
- return "the composition already uses html-in-canvas";
- for (const canvas of root.querySelectorAll("canvas")) {
- if (isAcceleratedCanvas(canvas))
- return "the composition draws a WebGL/WebGPU canvas, which html-in-canvas captures frozen";
- }
- return null;
+function defectEnv(backface: boolean): DefectEnv {
+ return { isAccelerated: isAcceleratedCanvas, backface };
}
function unsupportedReason(root: HTMLElement): string | null {
@@ -128,7 +128,7 @@ function unsupportedReason(root: HTMLElement): string | null {
if (!supportsCaptureMotionBlur()) {
return "html-in-canvas (drawElementImage) is unavailable in this browser";
}
- return captureBlocker(root);
+ return findCaptureDefect(root, defectEnv(scanBackfaceDefect(root)))?.reason ?? null;
}
function describe(el: Element): string {
@@ -163,13 +163,13 @@ function makeCanvas(width: number, height: number, role: string): HTMLCanvasElem
return canvas;
}
-function createInstance(host: CaptureMotionBlurHost, config: Config) {
+function createInstance(host: CaptureMotionBlurHost, config: Config, form: HtmlInCanvasForm) {
const { root } = host;
const { width, height } = rootSize(root);
const src = makeCanvas(width, height, "capture") as PaintCanvas;
const out = makeCanvas(width, height, "output");
out.style.pointerEvents = "none";
- src.setAttribute("layoutsubtree", "");
+ markCaptureCanvas(src, form);
const captureContext = src.getContext("2d") as DrawElementContext | null;
const outputContext = out.getContext("2d");
@@ -196,6 +196,7 @@ function createInstance(host: CaptureMotionBlurHost, config: Config) {
let sampleMs = 0;
let scanMs = 0;
let scannedTimeline: unknown = undefined;
+ let backface = false;
let wrapped = false;
// The output still shows the last frame of the previous wrap until this one draws.
let staleOutput = false;
@@ -212,6 +213,7 @@ function createInstance(host: CaptureMotionBlurHost, config: Config) {
moveRootThenResync(() => {
root.before(src);
src.append(root);
+ markCaptured(root, form);
src.after(out);
document.head.append(hideRule);
});
@@ -232,6 +234,7 @@ function createInstance(host: CaptureMotionBlurHost, config: Config) {
wrapped = false;
moveRootThenResync(() => {
hideRule.remove();
+ unmarkCaptured(root, form);
src.before(root);
src.remove();
out.remove();
@@ -244,6 +247,7 @@ function createInstance(host: CaptureMotionBlurHost, config: Config) {
function discover(restoreTo: number): void {
scannedTimeline = host.timeline();
+ backface = scanBackfaceDefect(root);
const s0 = performance.now();
const found = discoverMovers({
scene: root,
@@ -449,9 +453,9 @@ function createInstance(host: CaptureMotionBlurHost, config: Config) {
}
// Checked again here, not only at init: a block's script can add its own capture canvas
// (wrapping one inside ours hangs the renderer) or a WebGL canvas later.
- const blocked = captureBlocker(root);
+ const blocked = findCaptureDefect(root, defectEnv(backface));
if (blocked) {
- disable(blocked);
+ disable(blocked.reason);
return Promise.resolve();
}
wrap();
@@ -559,8 +563,9 @@ export function initCaptureMotionBlur(host: CaptureMotionBlurHost): void {
instance.rediscover();
return;
}
+ const form = htmlInCanvasForm();
const reason = unsupportedReason(host.root);
- if (reason) {
+ if (!form || reason) {
console.info(`[motion-blur] off: ${reason}`);
w.__hfMotionBlur = {
report: () => ({
@@ -578,7 +583,7 @@ export function initCaptureMotionBlur(host: CaptureMotionBlurHost): void {
};
return;
}
- instance = createInstance(host, config);
+ instance = createInstance(host, config, form);
const current = instance;
w.__hfMotionBlur = { report: () => current.report() };
}
diff --git a/packages/core/src/runtime/vfx.test.ts b/packages/core/src/runtime/vfx.test.ts
index a540b6bb81..feda9aa8e2 100644
--- a/packages/core/src/runtime/vfx.test.ts
+++ b/packages/core/src/runtime/vfx.test.ts
@@ -500,6 +500,35 @@ describe("vfx runtime — self capture", () => {
expectRefused(/chrome:\/\/flags\/#canvas-draw-element/);
});
+ describe.each([
+ ["dropped layoutsubtree", "updateElementGeometry", "drawable"],
+ ["still has layoutsubtree", "layoutSubtree", null],
+ ])("in a browser that %s", (_name, member, content) => {
+ const canvasProto = HTMLCanvasElement.prototype as unknown as Record<string, unknown>;
+ beforeEach(() => {
+ vi.stubGlobal(
+ "CanvasRenderingContext2D",
+ class {
+ drawElementImage(): void {}
+ },
+ );
+ canvasProto.requestPaint = () => {};
+ canvasProto[member] = () => {};
+ });
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ delete canvasProto.requestPaint;
+ delete canvasProto[member];
+ });
+
+ it("marks the capture canvas and its child only where the exporter's markup needs it", () => {
+ const host = makeCaptureHost(createMockCtx2d());
+ initVfx(document.body, 30);
+ expect(host.querySelector("canvas.hf-vfx-src")?.getAttribute("content")).toBe(content);
+ expect(host.querySelector(".hf-vfx-in")?.hasAttribute("drawable")).toBe(content !== null);
+ });
+ });
+
it("arms the page-composite protocol instead of painting inline in engine mode", () => {
makeCaptureHost(createMockCtx2d());
initVfx(document.body, 30);
diff --git a/packages/core/src/runtime/vfx.ts b/packages/core/src/runtime/vfx.ts
index a17be68766..efbf2d4c20 100644
--- a/packages/core/src/runtime/vfx.ts
+++ b/packages/core/src/runtime/vfx.ts
@@ -31,6 +31,7 @@ import {
type HfVfxParamValues,
type HfVfxRefParam,
} from "../vfx";
+import { htmlInCanvasForm, markCaptureCanvas, markCaptured } from "../htmlInCanvas";
import { registerSeekCompletion } from "./adapters/seek-dispatch";
import { isCanvasElement, isHtmlElement } from "./domRealm";
@@ -467,6 +468,13 @@ function findBackdropWrapper(owner: HTMLElement): HTMLCanvasElement | null {
return null;
}
+/** The exporter writes `layoutsubtree`; a browser that dropped it needs `content="drawable"` and `drawable` instead. */
+function adoptCaptureForm(canvas: HTMLCanvasElement, inner: HTMLElement): void {
+ if (htmlInCanvasForm() !== "drawable") return;
+ markCaptureCanvas(canvas, "drawable");
+ markCaptured(inner, "drawable");
+}
+
/**
* The `<canvas layoutsubtree class="hf-vfx-src">` that supplies `owner`'s
* pixels, plus its 2-D context. Never injected at runtime: the compiler derives
@@ -509,6 +517,7 @@ function resolveCaptureSource(
);
return undefined;
}
+ adoptCaptureForm(canvas, inner);
return {
canvas,
inner,
diff --git a/packages/studio/src/components/renders/useRenderQueue.ts b/packages/studio/src/components/renders/useRenderQueue.ts
index 0e24c194f8..73a7f10726 100644
--- a/packages/studio/src/components/renders/useRenderQueue.ts
+++ b/packages/studio/src/components/renders/useRenderQueue.ts
@@ -1,3 +1,4 @@
+import { htmlInCanvasForm } from "@hyperframes/core/html-in-canvas";
import { buildProjectApiPath } from "../../utils/projectRouting";
import { useState, useEffect, useCallback, useRef, useMemo } from "react";
import type { CanvasResolution } from "@hyperframes/parsers";
@@ -12,9 +13,7 @@ import { studioApiFetch } from "../../utils/studioApiFetch";
// WYSIWYG: the preview iframe runs in this browser, so a render blurs only if this one can.
function canPreviewMotionBlur(): boolean {
- const ctx = globalThis.CanvasRenderingContext2D?.prototype;
- if (!ctx || typeof document === "undefined") return false;
- return "drawElementImage" in ctx && "requestPaint" in document.createElement("canvas");
+ return typeof document !== "undefined" && htmlInCanvasForm() !== null;
}
export interface RenderJob {
diff --git a/packages/studio/src/components/renders/useRenderQueueMotionBlur.test.tsx b/packages/studio/src/components/renders/useRenderQueueMotionBlur.test.tsx
index 485a8bfbc8..743445625a 100644
--- a/packages/studio/src/components/renders/useRenderQueueMotionBlur.test.tsx
+++ b/packages/studio/src/components/renders/useRenderQueueMotionBlur.test.tsx
@@ -20,11 +20,24 @@ async function startRenderBody(): Promise<Record<string, unknown>> {
return started.body;
}
+function stubHtmlInCanvas(members: string[]): void {
+ vi.stubGlobal(
+ "CanvasRenderingContext2D",
+ class {
+ drawElementImage(): void {}
+ },
+ );
+ const proto = HTMLCanvasElement.prototype as unknown as Record<string, unknown>;
+ for (const name of ["requestPaint", ...members]) proto[name] = () => {};
+}
+
afterEach(() => {
queue?.unmount();
queue = null;
document.body.innerHTML = "";
- delete (HTMLCanvasElement.prototype as unknown as Record<string, unknown>).requestPaint;
+ for (const name of ["requestPaint", "layoutSubtree", "updateElementGeometry"]) {
+ delete (HTMLCanvasElement.prototype as unknown as Record<string, unknown>)[name];
+ }
vi.unstubAllGlobals();
});
@@ -33,14 +46,16 @@ describe("render request previewMotionBlur", () => {
expect((await startRenderBody())["previewMotionBlur"]).toBe(false);
});
- it("is true in a browser with html-in-canvas", async () => {
- vi.stubGlobal(
- "CanvasRenderingContext2D",
- class {
- drawElementImage(): void {}
- },
- );
- (HTMLCanvasElement.prototype as unknown as Record<string, unknown>).requestPaint = () => {};
+ it.each([
+ ["the layoutsubtree form", "layoutSubtree"],
+ ["the content=drawable form", "updateElementGeometry"],
+ ])("is true in a browser with html-in-canvas in %s", async (_name, member) => {
+ stubHtmlInCanvas([member]);
expect((await startRenderBody())["previewMotionBlur"]).toBe(true);
});
+
+ it("is false when html-in-canvas has neither known form", async () => {
+ stubHtmlInCanvas([]);
+ expect((await startRenderBody())["previewMotionBlur"]).toBe(false);
+ });
});