Skip to content

Commit a03da4f

Browse files
vanceingallsclaude
andcommitted
feat(core): block capture motion blur on html-in-canvas defects
html-in-canvas captures a few things wrongly, and a wrong blurred frame is worse than a sharp one. Add a defect list the runtime checks before it wraps a composition: a WebGL or WebGPU canvas (captured frozen), backface-hidden faces in a 3D context (drawn mirrored), an iframe (reloaded by the wrap and not recoverable), a transform or opacity below 1 on the root (ignored by the capture) and a canvas that is already a capture canvas (nesting hangs the renderer). Any hit turns blur off for the whole composition and the runtime says which one. The backface scan reads rules nested inside a style rule (CSS nesting), sheets pulled in with @import and adopted style sheets. Shadow DOM stays a documented limit. Support Chrome 157's html-in-canvas API alongside the older one: a canvas marked content="drawable" with a drawable attribute on captured children replaces layoutsubtree. Blur, the VFX runtime and Studio's browser check detect both through the new @hyperframes/core/html-in-canvas subpath. Folded review fixes: R14, R9 (the iframe defect entry). Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent d8d7200 commit a03da4f

13 files changed

Lines changed: 894 additions & 41 deletions

File tree

‎packages/core/package-subpaths.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@
104104
"types": "./dist/keyframeDefaults.d.ts",
105105
"environments": ["browser", "bun", "node"]
106106
},
107+
"./html-in-canvas": {
108+
"source": "./src/htmlInCanvas.ts",
109+
"runtime": "./dist/htmlInCanvas.js",
110+
"types": "./dist/htmlInCanvas.d.ts",
111+
"environments": ["browser", "bun", "node"]
112+
},
107113
"./gsap-cdn": {
108114
"source": "./src/gsapCdn.ts",
109115
"runtime": "./dist/gsapCdn.js",

‎packages/core/package.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@
116116
"import": "./src/keyframeDefaults.ts",
117117
"types": "./src/keyframeDefaults.ts"
118118
},
119+
"./html-in-canvas": {
120+
"bun": "./src/htmlInCanvas.ts",
121+
"node": "./dist/htmlInCanvas.js",
122+
"import": "./src/htmlInCanvas.ts",
123+
"types": "./src/htmlInCanvas.ts"
124+
},
119125
"./gsap-cdn": {
120126
"bun": "./src/gsapCdn.ts",
121127
"node": "./dist/gsapCdn.js",
@@ -614,6 +620,10 @@
614620
"import": "./dist/keyframeDefaults.js",
615621
"types": "./dist/keyframeDefaults.d.ts"
616622
},
623+
"./html-in-canvas": {
624+
"import": "./dist/htmlInCanvas.js",
625+
"types": "./dist/htmlInCanvas.d.ts"
626+
},
617627
"./gsap-cdn": {
618628
"import": "./dist/gsapCdn.js",
619629
"types": "./dist/gsapCdn.d.ts"
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// @vitest-environment happy-dom
2+
import { afterEach, describe, expect, it, vi } from "vitest";
3+
import {
4+
htmlInCanvasForm,
5+
markCaptureCanvas,
6+
markCaptured,
7+
unmarkCaptured,
8+
CAPTURE_CANVAS_SELECTOR,
9+
} from "./htmlInCanvas";
10+
import { supportsCaptureMotionBlur } from "./pageMotionBlur";
11+
12+
const canvasProto = HTMLCanvasElement.prototype as unknown as Record<string, unknown>;
13+
const CANVAS_MEMBERS = ["requestPaint", "layoutSubtree", "updateElementGeometry"];
14+
15+
function install(canvasMembers: string[], drawElementImage = true): void {
16+
vi.stubGlobal(
17+
"CanvasRenderingContext2D",
18+
drawElementImage
19+
? class {
20+
drawElementImage(): void {}
21+
}
22+
: class {},
23+
);
24+
for (const name of canvasMembers) canvasProto[name] = () => {};
25+
}
26+
27+
afterEach(() => {
28+
vi.unstubAllGlobals();
29+
for (const name of CANVAS_MEMBERS) delete canvasProto[name];
30+
});
31+
32+
describe("htmlInCanvasForm", () => {
33+
it("is layoutsubtree where the canvas has layoutSubtree, even if it also has updateElementGeometry", () => {
34+
install(["requestPaint", "layoutSubtree", "updateElementGeometry"]);
35+
expect(htmlInCanvasForm()).toBe("layoutsubtree");
36+
});
37+
38+
it("is drawable where layoutSubtree is gone and updateElementGeometry is there", () => {
39+
install(["requestPaint", "updateElementGeometry"]);
40+
expect(htmlInCanvasForm()).toBe("drawable");
41+
});
42+
43+
it("is null when neither form is there", () => {
44+
install(["requestPaint"]);
45+
expect(htmlInCanvasForm()).toBeNull();
46+
});
47+
48+
it("is null without drawElementImage or without requestPaint", () => {
49+
install(["requestPaint", "layoutSubtree"], false);
50+
expect(htmlInCanvasForm()).toBeNull();
51+
vi.unstubAllGlobals();
52+
delete canvasProto.requestPaint;
53+
install(["layoutSubtree"]);
54+
expect(htmlInCanvasForm()).toBeNull();
55+
});
56+
});
57+
58+
describe("supportsCaptureMotionBlur", () => {
59+
it.each([
60+
["old form", ["requestPaint", "layoutSubtree"], true],
61+
["new form", ["requestPaint", "updateElementGeometry"], true],
62+
["API present but no known form", ["requestPaint"], false],
63+
])("%s", (_name, members, expected) => {
64+
install(members);
65+
expect(supportsCaptureMotionBlur()).toBe(expected);
66+
});
67+
});
68+
69+
describe("marking", () => {
70+
it("marks the old form with layoutsubtree and leaves children alone", () => {
71+
const canvas = document.createElement("canvas");
72+
const child = document.createElement("div");
73+
markCaptureCanvas(canvas, "layoutsubtree");
74+
markCaptured(child, "layoutsubtree");
75+
expect(canvas.hasAttribute("layoutsubtree")).toBe(true);
76+
expect(canvas.hasAttribute("content")).toBe(false);
77+
expect(child.hasAttribute("drawable")).toBe(false);
78+
});
79+
80+
it("marks the new form with content=drawable and drawable children, and unmarks them", () => {
81+
const canvas = document.createElement("canvas");
82+
const child = document.createElement("div");
83+
markCaptureCanvas(canvas, "drawable");
84+
markCaptured(child, "drawable");
85+
expect(canvas.getAttribute("content")).toBe("drawable");
86+
expect(canvas.hasAttribute("layoutsubtree")).toBe(false);
87+
expect(child.hasAttribute("drawable")).toBe(true);
88+
unmarkCaptured(child, "drawable");
89+
expect(child.hasAttribute("drawable")).toBe(false);
90+
});
91+
92+
it("finds a nested capture canvas of either form", () => {
93+
for (const mark of [
94+
(c: HTMLElement) => c.setAttribute("layoutsubtree", ""),
95+
(c: HTMLElement) => c.setAttribute("content", "drawable"),
96+
]) {
97+
const root = document.createElement("div");
98+
const canvas = document.createElement("canvas");
99+
mark(canvas);
100+
root.append(canvas);
101+
expect(root.querySelector(CAPTURE_CANVAS_SELECTOR)).toBe(canvas);
102+
}
103+
const plain = document.createElement("div");
104+
plain.append(document.createElement("canvas"));
105+
expect(plain.querySelector(CAPTURE_CANVAS_SELECTOR)).toBeNull();
106+
});
107+
});

‎packages/core/src/htmlInCanvas.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/** Chrome <=155 marks a capture canvas with `layoutsubtree`; 157+ with `content="drawable"` and `drawable` on each captured child. */
2+
export type HtmlInCanvasForm = "layoutsubtree" | "drawable";
3+
4+
export const CAPTURE_CANVAS_SELECTOR = 'canvas[layoutsubtree], canvas[content="drawable"]';
5+
6+
/** The form this browser's html-in-canvas takes, or null when it has no usable one. */
7+
export function htmlInCanvasForm(win: Window = window): HtmlInCanvasForm | null {
8+
const ctx = (win as Window & { CanvasRenderingContext2D?: { prototype: object } })
9+
.CanvasRenderingContext2D?.prototype;
10+
if (!ctx || !("drawElementImage" in ctx)) return null;
11+
const canvas = win.document.createElement("canvas");
12+
if (!("requestPaint" in canvas)) return null;
13+
// 155 already has updateElementGeometry but still wants layoutsubtree, so this order matters.
14+
if ("layoutSubtree" in canvas) return "layoutsubtree";
15+
return "updateElementGeometry" in canvas ? "drawable" : null;
16+
}
17+
18+
export function markCaptureCanvas(canvas: HTMLCanvasElement, form: HtmlInCanvasForm): void {
19+
if (form === "layoutsubtree") canvas.setAttribute("layoutsubtree", "");
20+
else canvas.setAttribute("content", "drawable");
21+
}
22+
23+
export function markCaptured(el: Element, form: HtmlInCanvasForm): void {
24+
if (form === "drawable") el.setAttribute("drawable", "");
25+
}
26+
27+
export function unmarkCaptured(el: Element, form: HtmlInCanvasForm): void {
28+
if (form === "drawable") el.removeAttribute("drawable");
29+
}

0 commit comments

Comments
 (0)