From a06e0fceb5e4dce7c173b5483cd21676ecb5f5d9 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 3 Nov 2026 09:25:06 +0000 Subject: [PATCH 1/5] fix(core): keep overflowing content and SVG clip paths in motion-blurred frames A blurred mover was copied only within its own layout box, so text wider than the box (a one-line wordmark in a fixed-width container) was cut in blurred frames. The movers-alone capture also hid every non-mover, including the SVG clipPath a mover references, which empties the clip and makes the mover vanish. Both showed as flicker during playback and in full renders. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../runtime/captureMotionBlur/composite.ts | 2 + .../runtime/captureMotionBlur/filterPlan.ts | 6 +- .../runtime/captureMotionBlur/filterTier.ts | 1 + .../runtime/captureMotionBlur/frames.test.ts | 76 +++++++++++++++++++ .../src/runtime/captureMotionBlur/frames.ts | 73 +++++++++++++++++- .../runtime/captureMotionBlur/index.test.ts | 37 +++++++++ .../src/runtime/captureMotionBlur/index.ts | 9 ++- 7 files changed, 197 insertions(+), 7 deletions(-) create mode 100644 packages/core/src/runtime/captureMotionBlur/frames.test.ts diff --git a/packages/core/src/runtime/captureMotionBlur/composite.ts b/packages/core/src/runtime/captureMotionBlur/composite.ts index bd2b53a053c..31e21af871a 100644 --- a/packages/core/src/runtime/captureMotionBlur/composite.ts +++ b/packages/core/src/runtime/captureMotionBlur/composite.ts @@ -14,6 +14,8 @@ import { type Affine, invert, sweep, tapWeights, thinTaps } from "./pose"; export interface MoverFrame { w: number; h: number; + /** Where the drawn box starts in the element's own px (its glow and overflow); default 0, 0. */ + origin?: [number, number]; /** The pose the capture shows (frame time). */ now: Affine; /** Poses across the shutter. */ diff --git a/packages/core/src/runtime/captureMotionBlur/filterPlan.ts b/packages/core/src/runtime/captureMotionBlur/filterPlan.ts index 80a3ba695bc..00277e00e78 100644 --- a/packages/core/src/runtime/captureMotionBlur/filterPlan.ts +++ b/packages/core/src/runtime/captureMotionBlur/filterPlan.ts @@ -82,14 +82,16 @@ export function planFilter(frame: MoverFrame, pad: number, frameW: number): Filt const xs = offsets.map((o) => o[0]); const ys = offsets.map((o) => o[1]); const margin = pad + 3 * Math.max(...sigma) + 2; + // The frame box can start before the element's own origin (overflowing content). + const [ox, oy] = frame.origin ?? [0, 0]; const x = Math.min(0, ...xs) - margin; const y = Math.min(0, ...ys) - margin; return { offsets, sigma, region: { - x, - y, + x: ox + x, + y: oy + y, w: w + Math.max(0, ...xs) + margin - x, h: h + Math.max(0, ...ys) + margin - y, }, diff --git a/packages/core/src/runtime/captureMotionBlur/filterTier.ts b/packages/core/src/runtime/captureMotionBlur/filterTier.ts index a56aad1e2fc..61e68e7ce2b 100644 --- a/packages/core/src/runtime/captureMotionBlur/filterTier.ts +++ b/packages/core/src/runtime/captureMotionBlur/filterTier.ts @@ -189,6 +189,7 @@ export function createFilterTier( shutterSeconds: config.shutterDeg / 360 / host.fps, taps: COPIES, frameW: width, + scene: root, frameH: height, }); const plans: Array<[HTMLElement, FilterPlan]> = []; diff --git a/packages/core/src/runtime/captureMotionBlur/frames.test.ts b/packages/core/src/runtime/captureMotionBlur/frames.test.ts new file mode 100644 index 00000000000..a855746ff72 --- /dev/null +++ b/packages/core/src/runtime/captureMotionBlur/frames.test.ts @@ -0,0 +1,76 @@ +import { describe, expect, it } from "vitest"; +import { readMoverFrames } from "./frames"; +import { IDENTITY, translate } from "./pose"; + +/** Fixed layout for jsdom: an element's offset box and its client rect. */ +function laidOut(el: HTMLElement, w: number, h: number, rect: [number, number, number, number]) { + Object.defineProperty(el, "offsetWidth", { value: w, configurable: true }); + Object.defineProperty(el, "offsetHeight", { value: h, configurable: true }); + const [left, top, right, bottom] = rect; + el.getBoundingClientRect = () => + ({ left, top, right, bottom, width: right - left, height: bottom - top }) as DOMRect; +} + +describe("readMoverFrames", () => { + // A wordmark in a 540 px box whose one-line text runs to 1528 px: the box is the layout, the + // text is the picture. Cropping to the box drew "HyperFra" for "HyperFrames Studio". + it("sizes the redraw to content that overflows the mover's box", () => { + const scene = document.createElement("div"); + const brand = document.createElement("div"); + const word = document.createElement("div"); + brand.append(word); + scene.append(brand); + document.body.append(scene); + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); + laidOut(brand, 540, 240, [100, 50, 640, 290]); + laidOut(word, 1528, 240, [100, 50, 1628, 290]); + const pose = translate(IDENTITY, 100, 50); + const reader = { pose: () => pose, reset: () => {} }; + let x = 0; + const { frames } = readMoverFrames({ + movers: [brand], + reader: reader as never, + seek: (t) => { + x = t; + }, + t: 0.5, + reads: 2, + shutterSeconds: 1 / 45, + taps: 4, + frameW: 1920, + frameH: 1080, + scene, + }); + expect(x).toBe(0.5); + expect(frames[0]?.w).toBeGreaterThanOrEqual(1528); + expect(frames[0]?.h).toBeGreaterThanOrEqual(240); + document.body.innerHTML = ""; + }); + + it("keeps the box as it is when its content stays inside", () => { + const scene = document.createElement("div"); + const box = document.createElement("div"); + const child = document.createElement("span"); + box.append(child); + scene.append(box); + document.body.append(scene); + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); + laidOut(box, 200, 100, [10, 10, 210, 110]); + laidOut(child, 50, 20, [20, 20, 70, 40]); + const pose = translate(IDENTITY, 10, 10); + const { frames } = readMoverFrames({ + movers: [box], + reader: { pose: () => pose, reset: () => {} } as never, + seek: () => {}, + t: 0.5, + reads: 2, + shutterSeconds: 1 / 45, + taps: 4, + frameW: 1920, + frameH: 1080, + scene, + }); + expect(frames[0]).toMatchObject({ w: 200, h: 100, now: pose }); + document.body.innerHTML = ""; + }); +}); diff --git a/packages/core/src/runtime/captureMotionBlur/frames.ts b/packages/core/src/runtime/captureMotionBlur/frames.ts index 651d25a3949..e5bed71ca06 100644 --- a/packages/core/src/runtime/captureMotionBlur/frames.ts +++ b/packages/core/src/runtime/captureMotionBlur/frames.ts @@ -6,10 +6,66 @@ import { type createPoseReader, IDENTITY, autoTaps, + invert, tapsFromReads, translate, } from "./pose"; +/** Descendants read per mover per frame for its painted extent. */ +const CONTENT_NODE_LIMIT = 256; + +type Box = [number, number, number, number]; + +/** Maps client px back into the mover's own px: pose maps mover px to scene px, and the scene sits at its client rect, scaled to the frame. */ +function clientToMover(pose: Affine, scene: HTMLElement, frameW: number): Affine | null { + const sr = scene.getBoundingClientRect(); + const s = frameW > 0 && sr.width > 0 ? sr.width / frameW : 1; + return invert({ + a: pose.a * s, + b: pose.b * s, + c: pose.c * s, + d: pose.d * s, + e: sr.left + pose.e * s, + f: sr.top + pose.f * s, + }); +} + +/** Grows `box` to hold client rect `r`, mapped into the mover's px by `back`. */ +function growToRect(box: Box, back: Affine, r: DOMRect): void { + for (const [x, y] of [ + [r.left, r.top], + [r.right, r.top], + [r.left, r.bottom], + [r.right, r.bottom], + ] as const) { + const lx = back.a * x + back.c * y + back.e; + const ly = back.b * x + back.d * y + back.f; + box[0] = Math.min(box[0], lx); + box[1] = Math.min(box[1], ly); + box[2] = Math.max(box[2], lx); + box[3] = Math.max(box[3], ly); + } +} + +/** + * The mover's painted extent in its own px, `[x0, y0, x1, y1]`: its border box, grown to every + * descendant that overflows it (text wider than the box, positioned children). The crop is cut to + * this box, so content outside it would be dropped from the redraw. + */ +function contentBox(el: HTMLElement, pose: Affine, scene: HTMLElement, frameW: number): Box { + const box: Box = [0, 0, el.offsetWidth, el.offsetHeight]; + const cs = getComputedStyle(el); + if (cs.overflowX !== "visible" && cs.overflowY !== "visible") return box; + const back = clientToMover(pose, scene, frameW); + if (!back) return box; + const walker = el.ownerDocument.createTreeWalker(el, NodeFilter.SHOW_ELEMENT); + for (let n = 0; n < CONTENT_NODE_LIMIT && walker.nextNode(); n++) { + const r = (walker.currentNode as Element).getBoundingClientRect(); + if (r.width && r.height) growToRect(box, back, r); + } + return [Math.floor(box[0]), Math.floor(box[1]), Math.ceil(box[2]), Math.ceil(box[3])]; +} + export interface FrameRequest { movers: HTMLElement[]; reader: ReturnType; @@ -23,6 +79,8 @@ export interface FrameRequest { taps: number | null; /** Px each mover's box grows by on every side (its glow); none for a mover not listed. */ pads?: ReadonlyMap; + /** The composition root the poses are relative to; with it, overflowing content is kept. */ + scene?: HTMLElement; frameW: number; frameH: number; } @@ -45,14 +103,21 @@ export function readMoverFrames(req: FrameRequest): { els: HTMLElement[]; frames r.reset(); const frames = visible.map((el, i) => { const pad = req.pads?.get(el) ?? 0; - const w = el.offsetWidth + 2 * pad; - const h = el.offsetHeight + 2 * pad; - const grow = (m: Affine): Affine => translate(m, -pad, -pad); + const pose = r.pose(el); + const [x0, y0, x1, y1] = req.scene + ? contentBox(el, pose, req.scene, req.frameW) + : [0, 0, el.offsetWidth, el.offsetHeight]; + const w = x1 - x0 + 2 * pad; + const h = y1 - y0 + 2 * pad; + const ox = x0 - pad; + const oy = y0 - pad; + const grow = (m: Affine): Affine => translate(m, ox, oy); const path = (poses[i] ?? [IDENTITY]).map(grow); return { w, h, - now: grow(r.pose(el)), + origin: [ox, oy] as [number, number], + now: grow(pose), taps: req.taps === null ? autoTaps(path, w, h, req.frameW, req.frameH) diff --git a/packages/core/src/runtime/captureMotionBlur/index.test.ts b/packages/core/src/runtime/captureMotionBlur/index.test.ts index cedd037a0c0..20f99512358 100644 --- a/packages/core/src/runtime/captureMotionBlur/index.test.ts +++ b/packages/core/src/runtime/captureMotionBlur/index.test.ts @@ -1981,3 +1981,40 @@ describe("filter fallback", () => { expect(document.querySelectorAll("filter")).toHaveLength(0); }); }); + +describe("the movers-alone capture", () => { + // A mover clipped by an SVG elsewhere in the composition: hiding the clip path's + // shape empties the clip, so the mover captured as nothing and vanished from blurred frames. + it("leaves SVG clip paths and masks visible, hiding the rest of the scene", () => { + installHtmlInCanvas(); + const { root, h } = movingHost(); + const NS = "http://www.w3.org/2000/svg"; + const svg = document.createElementNS(NS, "svg"); + const defs = document.createElementNS(NS, "defs"); + const clip = document.createElementNS(NS, "clipPath"); + const shape = document.createElementNS(NS, "use"); + clip.append(shape); + defs.append(clip); + svg.append(defs); + const still = document.createElement("div"); + root.append(svg, still); + initCaptureMotionBlur(h); + void paintCaptureMotionBlur(0.5, PLAY); + expect(root.hasAttribute(ONLY)).toBe(true); + // jsdom has no cascade for this: check which elements each isolation rule selects. + const rules = [...document.head.querySelectorAll("style")] + .flatMap((s) => (s.textContent ?? "").split("\n")) + .filter((line) => line.startsWith(`[${ONLY}]`)); + const selecting = (el: Element, value: string) => + rules.some( + (line) => + line.includes(`visibility: ${value}`) && el.matches(line.slice(0, line.indexOf("{"))), + ); + expect(selecting(still, "hidden")).toBe(true); + expect(selecting(still, "visible")).toBe(false); + // Neither hidden itself nor left to inherit its 's hidden: its is made visible. + expect(selecting(shape, "hidden")).toBe(false); + expect(selecting(clip, "hidden")).toBe(false); + expect(selecting(defs, "visible")).toBe(true); + }); +}); diff --git a/packages/core/src/runtime/captureMotionBlur/index.ts b/packages/core/src/runtime/captureMotionBlur/index.ts index f21954bd458..39c994f294e 100644 --- a/packages/core/src/runtime/captureMotionBlur/index.ts +++ b/packages/core/src/runtime/captureMotionBlur/index.ts @@ -65,6 +65,9 @@ const HIDE_ATTR = "data-hf-motion-blur-hide"; /** On the root while the movers alone are captured; the movers carry MOVER_ATTR. */ const ONLY_ATTR = "data-hf-motion-blur-only"; const MOVER_ATTR = "data-hf-motion-blur-mover"; +/** SVG resources: drawn only through a reference (a mover's clip-path or mask), never on their own. */ +const SVG_RESOURCES = ["defs", "clipPath", "mask", "pattern", "marker", "symbol"]; +const IN_SVG_RESOURCE = SVG_RESOURCES.flatMap((tag) => [tag, `${tag} *`]).join(", "); const NESTED_CANVAS_KEEPS_ITS_PLACE = `:where([data-hf-motion-blur="capture"] canvas) { translate: 0 0 0; }`; interface Job { @@ -169,8 +172,11 @@ function createInstance(host: CaptureMotionBlurHost, config: Config, form: HtmlI `[${HIDE_ATTR}] { opacity: 0 !important; }`, // The root stays visible: a rotated or scaled mover under a hidden root is captured at the origin, untransformed. `[${ONLY_ATTR}] { background: none !important; border-color: transparent !important; box-shadow: none !important; outline-color: transparent !important; }`, - `[${ONLY_ATTR}]::before, [${ONLY_ATTR}]::after, [${ONLY_ATTR}] *:not([${MOVER_ATTR}], [${MOVER_ATTR}] *) { visibility: hidden !important; }`, + // A hidden SVG resource (even by inheritance from its ) empties the clip or mask of the + // mover that references it, so the mover would capture as nothing. + `[${ONLY_ATTR}]::before, [${ONLY_ATTR}]::after, [${ONLY_ATTR}] *:not([${MOVER_ATTR}], [${MOVER_ATTR}] *, ${IN_SVG_RESOURCE}) { visibility: hidden !important; }`, `[${ONLY_ATTR}] [${MOVER_ATTR}] { visibility: visible !important; }`, + `[${ONLY_ATTR}] :is(${SVG_RESOURCES.join(", ")}) { visibility: visible !important; }`, NESTED_CANVAS_KEEPS_ITS_PLACE, ].join("\n"); @@ -280,6 +286,7 @@ function createInstance(host: CaptureMotionBlurHost, config: Config, form: HtmlI taps: config.samples, pads, frameW: width, + scene: root, frameH: height, }); } From 76d41819475f94a3ecf51661e9faf505a129c327 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 3 Nov 2026 09:27:27 +0000 Subject: [PATCH 2/5] fix(core): keep the motion-blur fix within each file's comment share Co-Authored-By: Claude Opus 5.5 (1M context) --- .../core/src/runtime/captureMotionBlur/composite.ts | 1 - .../core/src/runtime/captureMotionBlur/filterPlan.ts | 1 - packages/core/src/runtime/captureMotionBlur/frames.ts | 10 +--------- packages/core/src/runtime/captureMotionBlur/index.ts | 9 +++------ 4 files changed, 4 insertions(+), 17 deletions(-) diff --git a/packages/core/src/runtime/captureMotionBlur/composite.ts b/packages/core/src/runtime/captureMotionBlur/composite.ts index 31e21af871a..969bd03890c 100644 --- a/packages/core/src/runtime/captureMotionBlur/composite.ts +++ b/packages/core/src/runtime/captureMotionBlur/composite.ts @@ -14,7 +14,6 @@ import { type Affine, invert, sweep, tapWeights, thinTaps } from "./pose"; export interface MoverFrame { w: number; h: number; - /** Where the drawn box starts in the element's own px (its glow and overflow); default 0, 0. */ origin?: [number, number]; /** The pose the capture shows (frame time). */ now: Affine; diff --git a/packages/core/src/runtime/captureMotionBlur/filterPlan.ts b/packages/core/src/runtime/captureMotionBlur/filterPlan.ts index 00277e00e78..77ec7f9959a 100644 --- a/packages/core/src/runtime/captureMotionBlur/filterPlan.ts +++ b/packages/core/src/runtime/captureMotionBlur/filterPlan.ts @@ -82,7 +82,6 @@ export function planFilter(frame: MoverFrame, pad: number, frameW: number): Filt const xs = offsets.map((o) => o[0]); const ys = offsets.map((o) => o[1]); const margin = pad + 3 * Math.max(...sigma) + 2; - // The frame box can start before the element's own origin (overflowing content). const [ox, oy] = frame.origin ?? [0, 0]; const x = Math.min(0, ...xs) - margin; const y = Math.min(0, ...ys) - margin; diff --git a/packages/core/src/runtime/captureMotionBlur/frames.ts b/packages/core/src/runtime/captureMotionBlur/frames.ts index e5bed71ca06..b6003c991b8 100644 --- a/packages/core/src/runtime/captureMotionBlur/frames.ts +++ b/packages/core/src/runtime/captureMotionBlur/frames.ts @@ -11,12 +11,10 @@ import { translate, } from "./pose"; -/** Descendants read per mover per frame for its painted extent. */ const CONTENT_NODE_LIMIT = 256; type Box = [number, number, number, number]; -/** Maps client px back into the mover's own px: pose maps mover px to scene px, and the scene sits at its client rect, scaled to the frame. */ function clientToMover(pose: Affine, scene: HTMLElement, frameW: number): Affine | null { const sr = scene.getBoundingClientRect(); const s = frameW > 0 && sr.width > 0 ? sr.width / frameW : 1; @@ -30,7 +28,6 @@ function clientToMover(pose: Affine, scene: HTMLElement, frameW: number): Affine }); } -/** Grows `box` to hold client rect `r`, mapped into the mover's px by `back`. */ function growToRect(box: Box, back: Affine, r: DOMRect): void { for (const [x, y] of [ [r.left, r.top], @@ -47,11 +44,7 @@ function growToRect(box: Box, back: Affine, r: DOMRect): void { } } -/** - * The mover's painted extent in its own px, `[x0, y0, x1, y1]`: its border box, grown to every - * descendant that overflows it (text wider than the box, positioned children). The crop is cut to - * this box, so content outside it would be dropped from the redraw. - */ +/** The crop is cut to this box, so it must hold content that overflows the mover (wide text). */ function contentBox(el: HTMLElement, pose: Affine, scene: HTMLElement, frameW: number): Box { const box: Box = [0, 0, el.offsetWidth, el.offsetHeight]; const cs = getComputedStyle(el); @@ -79,7 +72,6 @@ export interface FrameRequest { taps: number | null; /** Px each mover's box grows by on every side (its glow); none for a mover not listed. */ pads?: ReadonlyMap; - /** The composition root the poses are relative to; with it, overflowing content is kept. */ scene?: HTMLElement; frameW: number; frameH: number; diff --git a/packages/core/src/runtime/captureMotionBlur/index.ts b/packages/core/src/runtime/captureMotionBlur/index.ts index 39c994f294e..3285eb85796 100644 --- a/packages/core/src/runtime/captureMotionBlur/index.ts +++ b/packages/core/src/runtime/captureMotionBlur/index.ts @@ -65,9 +65,8 @@ const HIDE_ATTR = "data-hf-motion-blur-hide"; /** On the root while the movers alone are captured; the movers carry MOVER_ATTR. */ const ONLY_ATTR = "data-hf-motion-blur-only"; const MOVER_ATTR = "data-hf-motion-blur-mover"; -/** SVG resources: drawn only through a reference (a mover's clip-path or mask), never on their own. */ -const SVG_RESOURCES = ["defs", "clipPath", "mask", "pattern", "marker", "symbol"]; -const IN_SVG_RESOURCE = SVG_RESOURCES.flatMap((tag) => [tag, `${tag} *`]).join(", "); +const SVG_DRAWN_BY_REFERENCE = ["defs", "clipPath", "mask", "pattern", "marker", "symbol"]; +const IN_SVG_RESOURCE = SVG_DRAWN_BY_REFERENCE.flatMap((tag) => [tag, `${tag} *`]).join(", "); const NESTED_CANVAS_KEEPS_ITS_PLACE = `:where([data-hf-motion-blur="capture"] canvas) { translate: 0 0 0; }`; interface Job { @@ -172,11 +171,9 @@ function createInstance(host: CaptureMotionBlurHost, config: Config, form: HtmlI `[${HIDE_ATTR}] { opacity: 0 !important; }`, // The root stays visible: a rotated or scaled mover under a hidden root is captured at the origin, untransformed. `[${ONLY_ATTR}] { background: none !important; border-color: transparent !important; box-shadow: none !important; outline-color: transparent !important; }`, - // A hidden SVG resource (even by inheritance from its ) empties the clip or mask of the - // mover that references it, so the mover would capture as nothing. `[${ONLY_ATTR}]::before, [${ONLY_ATTR}]::after, [${ONLY_ATTR}] *:not([${MOVER_ATTR}], [${MOVER_ATTR}] *, ${IN_SVG_RESOURCE}) { visibility: hidden !important; }`, `[${ONLY_ATTR}] [${MOVER_ATTR}] { visibility: visible !important; }`, - `[${ONLY_ATTR}] :is(${SVG_RESOURCES.join(", ")}) { visibility: visible !important; }`, + `[${ONLY_ATTR}] :is(${SVG_DRAWN_BY_REFERENCE.join(", ")}) { visibility: visible !important; }`, NESTED_CANVAS_KEEPS_ITS_PLACE, ].join("\n"); From 1ae71f4d032eea27869c3a26a3fd6dc707db7f37 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 3 Nov 2026 09:31:51 +0000 Subject: [PATCH 3/5] fix(core): measure overflowing text with a Range, ignore rects inside the mover A block child is as wide as its mover however long its no-wrap text, so reading child boxes missed the common overflow layout. Read the text's own rects through a Range. On a rotated mover each child's upright client rect covers the mover's corners, which doubled the box at 45 degrees and could pull a neighbouring mover into the trail; only rects that leave the mover's own client rect now grow it. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../runtime/captureMotionBlur/frames.test.ts | 80 ++++++++++++++++++- .../src/runtime/captureMotionBlur/frames.ts | 18 ++++- .../runtime/captureMotionBlur/index.test.ts | 4 + 3 files changed, 96 insertions(+), 6 deletions(-) diff --git a/packages/core/src/runtime/captureMotionBlur/frames.test.ts b/packages/core/src/runtime/captureMotionBlur/frames.test.ts index a855746ff72..87b83445256 100644 --- a/packages/core/src/runtime/captureMotionBlur/frames.test.ts +++ b/packages/core/src/runtime/captureMotionBlur/frames.test.ts @@ -1,6 +1,6 @@ -import { describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { readMoverFrames } from "./frames"; -import { IDENTITY, translate } from "./pose"; +import { type Affine, IDENTITY, translate } from "./pose"; /** Fixed layout for jsdom: an element's offset box and its client rect. */ function laidOut(el: HTMLElement, w: number, h: number, rect: [number, number, number, number]) { @@ -11,7 +11,48 @@ function laidOut(el: HTMLElement, w: number, h: number, rect: [number, number, n ({ left, top, right, bottom, width: right - left, height: bottom - top }) as DOMRect; } +const rect = ([left, top, right, bottom]: [number, number, number, number]) => + ({ left, top, right, bottom, width: right - left, height: bottom - top }) as DOMRect; + +/** jsdom has no Range rects: the text inside a node measures as `of(node)`. */ +function textRects(of: (node: Node) => [number, number, number, number]) { + let node: Node | null = null; + const proto = Range.prototype as unknown as Record; + proto.selectNodeContents = (n: Node) => { + node = n; + }; + proto.getBoundingClientRect = () => rect(node ? of(node) : [0, 0, 0, 0]); +} + +function read(mover: HTMLElement, scene: HTMLElement, pose: Affine) { + return readMoverFrames({ + movers: [mover], + reader: { pose: () => pose, reset: () => {} } as never, + seek: () => {}, + t: 0.5, + reads: 2, + shutterSeconds: 1 / 45, + taps: 4, + frameW: 1920, + frameH: 1080, + scene, + }).frames[0]; +} + +const RANGE_STUBS = ["selectNodeContents", "getBoundingClientRect"]; +const realRange = RANGE_STUBS.map( + (k) => (Range.prototype as unknown as Record)[k], +); + describe("readMoverFrames", () => { + beforeEach(() => textRects(() => [0, 0, 0, 0])); + afterEach(() => { + RANGE_STUBS.forEach((k, i) => { + (Range.prototype as unknown as Record)[k] = realRange[i]; + }); + document.body.innerHTML = ""; + }); + // A wordmark in a 540 px box whose one-line text runs to 1528 px: the box is the layout, the // text is the picture. Cropping to the box drew "HyperFra" for "HyperFrames Studio". it("sizes the redraw to content that overflows the mover's box", () => { @@ -44,7 +85,6 @@ describe("readMoverFrames", () => { expect(x).toBe(0.5); expect(frames[0]?.w).toBeGreaterThanOrEqual(1528); expect(frames[0]?.h).toBeGreaterThanOrEqual(240); - document.body.innerHTML = ""; }); it("keeps the box as it is when its content stays inside", () => { @@ -71,6 +111,38 @@ describe("readMoverFrames", () => { scene, }); expect(frames[0]).toMatchObject({ w: 200, h: 100, now: pose }); - document.body.innerHTML = ""; + }); + // A block child is as wide as the mover however long its text: only the text's own rects show it. + it("sizes the redraw to text that overflows a block child", () => { + const scene = document.createElement("div"); + const brand = document.createElement("div"); + const word = document.createElement("div"); + word.textContent = "HyperFrames Studio"; + brand.append(word); + scene.append(brand); + document.body.append(scene); + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); + laidOut(brand, 540, 110, [100, 50, 640, 160]); + laidOut(word, 540, 110, [100, 50, 640, 160]); + textRects(() => [100, 50, 981, 160]); + expect(read(brand, scene, translate(IDENTITY, 100, 50))?.w).toBe(881); + }); + + // Upright client rects of a rotated mover's children cover its corners, not its content. + it("keeps a rotated mover's box when its content stays inside", () => { + const scene = document.createElement("div"); + const box = document.createElement("div"); + const fill = document.createElement("div"); + box.append(fill); + scene.append(box); + document.body.append(scene); + const c = Math.SQRT1_2; + const upright: [number, number, number, number] = [1059, 600, 1341, 883]; + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); + laidOut(box, 200, 200, upright); + laidOut(fill, 200, 200, upright); + textRects(() => upright); + const frame = read(box, scene, { a: c, b: c, c: -c, d: c, e: 1200, f: 600 }); + expect(frame).toMatchObject({ w: 200, h: 200 }); }); }); diff --git a/packages/core/src/runtime/captureMotionBlur/frames.ts b/packages/core/src/runtime/captureMotionBlur/frames.ts index b6003c991b8..f3f1b71f63b 100644 --- a/packages/core/src/runtime/captureMotionBlur/frames.ts +++ b/packages/core/src/runtime/captureMotionBlur/frames.ts @@ -28,6 +28,14 @@ function clientToMover(pose: Affine, scene: HTMLElement, frameW: number): Affine }); } +const SUBPIXEL = 0.5; + +const within = (r: DOMRect, o: DOMRect): boolean => + r.left >= o.left - SUBPIXEL && + r.top >= o.top - SUBPIXEL && + r.right <= o.right + SUBPIXEL && + r.bottom <= o.bottom + SUBPIXEL; + function growToRect(box: Box, back: Affine, r: DOMRect): void { for (const [x, y] of [ [r.left, r.top], @@ -51,10 +59,16 @@ function contentBox(el: HTMLElement, pose: Affine, scene: HTMLElement, frameW: n if (cs.overflowX !== "visible" && cs.overflowY !== "visible") return box; const back = clientToMover(pose, scene, frameW); if (!back) return box; + const own = el.getBoundingClientRect(); + const growOutside = (r: DOMRect): void => { + if (r.width && r.height && !within(r, own)) growToRect(box, back, r); + }; + const text = el.ownerDocument.createRange(); + text.selectNodeContents(el); + growOutside(text.getBoundingClientRect()); const walker = el.ownerDocument.createTreeWalker(el, NodeFilter.SHOW_ELEMENT); for (let n = 0; n < CONTENT_NODE_LIMIT && walker.nextNode(); n++) { - const r = (walker.currentNode as Element).getBoundingClientRect(); - if (r.width && r.height) growToRect(box, back, r); + growOutside((walker.currentNode as Element).getBoundingClientRect()); } return [Math.floor(box[0]), Math.floor(box[1]), Math.ceil(box[2]), Math.ceil(box[3])]; } diff --git a/packages/core/src/runtime/captureMotionBlur/index.test.ts b/packages/core/src/runtime/captureMotionBlur/index.test.ts index 20f99512358..150b142768a 100644 --- a/packages/core/src/runtime/captureMotionBlur/index.test.ts +++ b/packages/core/src/runtime/captureMotionBlur/index.test.ts @@ -93,7 +93,10 @@ function host( const captureCanvas = (root: HTMLElement) => root.parentElement as HTMLCanvasElement; +const rangeRect = Range.prototype.getBoundingClientRect; + beforeEach(() => { + Range.prototype.getBoundingClientRect = () => new DOMRect(); setFilterFallbackForTests(false); contexts = []; vi.spyOn(console, "info").mockImplementation(() => {}); @@ -101,6 +104,7 @@ beforeEach(() => { }); afterEach(() => { + Range.prototype.getBoundingClientRect = rangeRect; resetCaptureMotionBlurForTests(); setFilterFallbackForTests(true); vi.restoreAllMocks(); From 88b2de100468c07d2aed852c415022f862e0c3d0 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 3 Nov 2026 09:37:00 +0000 Subject: [PATCH 4/5] fix(core): measure a rotated mover's overflow unrotated The upright client rect of a rotated mover has empty corners, so overflow that landed there passed the "stays inside" check and was still cropped. Measure a rotated mover with its own transform off and map the rects back through its layout pose, then restore its style. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../runtime/captureMotionBlur/frames.test.ts | 32 +++++++++++++++++ .../src/runtime/captureMotionBlur/frames.ts | 36 +++++++++++++++---- 2 files changed, 61 insertions(+), 7 deletions(-) diff --git a/packages/core/src/runtime/captureMotionBlur/frames.test.ts b/packages/core/src/runtime/captureMotionBlur/frames.test.ts index 87b83445256..4b33676736a 100644 --- a/packages/core/src/runtime/captureMotionBlur/frames.test.ts +++ b/packages/core/src/runtime/captureMotionBlur/frames.test.ts @@ -145,4 +145,36 @@ describe("readMoverFrames", () => { const frame = read(box, scene, { a: c, b: c, c: -c, d: c, e: 1200, f: 600 }); expect(frame).toMatchObject({ w: 200, h: 200 }); }); + + // Its unrotated text at local y 90-110 runs to x 260; rotated, that lands in the upright rect's corner. + it("sizes a rotated mover's redraw to text that overflows it", () => { + const scene = document.createElement("div"); + const box = document.createElement("div"); + box.setAttribute("style", "transform: rotate(45deg)"); + box.textContent = "HyperFrames"; + scene.append(box); + document.body.append(scene); + const c = Math.SQRT1_2; + const rotated: Affine = { a: c, b: c, c: -c, d: c, e: 1200, f: 600 }; + const unrotated = translate(IDENTITY, 1100, 600); + const off = () => box.getAttribute("style")?.includes("transform: none") ?? false; + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); + laidOut(box, 200, 200, [0, 0, 0, 0]); + box.getBoundingClientRect = () => rect(off() ? [1100, 600, 1300, 800] : [1059, 600, 1341, 883]); + textRects(() => (off() ? [1100, 690, 1360, 710] : [1122, 663, 1320, 862])); + const frame = readMoverFrames({ + movers: [box], + reader: { pose: () => (off() ? unrotated : rotated), reset: () => {} } as never, + seek: () => {}, + t: 0.5, + reads: 2, + shutterSeconds: 1 / 45, + taps: 4, + frameW: 1920, + frameH: 1080, + scene, + }).frames[0]; + expect(frame).toMatchObject({ w: 260, h: 200 }); + expect(box.getAttribute("style")).toBe("transform: rotate(45deg)"); + }); }); diff --git a/packages/core/src/runtime/captureMotionBlur/frames.ts b/packages/core/src/runtime/captureMotionBlur/frames.ts index f3f1b71f63b..f92e35ebd0c 100644 --- a/packages/core/src/runtime/captureMotionBlur/frames.ts +++ b/packages/core/src/runtime/captureMotionBlur/frames.ts @@ -14,6 +14,7 @@ import { const CONTENT_NODE_LIMIT = 256; type Box = [number, number, number, number]; +type PoseReader = ReturnType; function clientToMover(pose: Affine, scene: HTMLElement, frameW: number): Affine | null { const sr = scene.getBoundingClientRect(); @@ -52,11 +53,8 @@ function growToRect(box: Box, back: Affine, r: DOMRect): void { } } -/** The crop is cut to this box, so it must hold content that overflows the mover (wide text). */ -function contentBox(el: HTMLElement, pose: Affine, scene: HTMLElement, frameW: number): Box { +function grownBox(el: HTMLElement, pose: Affine, scene: HTMLElement, frameW: number): Box { const box: Box = [0, 0, el.offsetWidth, el.offsetHeight]; - const cs = getComputedStyle(el); - if (cs.overflowX !== "visible" && cs.overflowY !== "visible") return box; const back = clientToMover(pose, scene, frameW); if (!back) return box; const own = el.getBoundingClientRect(); @@ -73,9 +71,33 @@ function contentBox(el: HTMLElement, pose: Affine, scene: HTMLElement, frameW: n return [Math.floor(box[0]), Math.floor(box[1]), Math.ceil(box[2]), Math.ceil(box[3])]; } +const UNTRANSFORMED = + "transform: none !important; rotate: none !important; scale: none !important; translate: none !important"; + +/** The crop is cut to this box, so it must hold content that overflows the mover (wide text). */ +function contentBox(el: HTMLElement, reader: PoseReader, scene: HTMLElement, frameW: number): Box { + const cs = getComputedStyle(el); + if (cs.overflowX !== "visible" && cs.overflowY !== "visible") { + return [0, 0, el.offsetWidth, el.offsetHeight]; + } + const pose = reader.pose(el); + if (pose.b === 0 && pose.c === 0) return grownBox(el, pose, scene, frameW); + // Upright rects of rotated content leave its corners out, so it is measured unrotated. + const style = el.getAttribute("style"); + el.setAttribute("style", `${style ?? ""};${UNTRANSFORMED}`); + reader.reset(); + try { + return grownBox(el, reader.pose(el), scene, frameW); + } finally { + if (style === null) el.removeAttribute("style"); + else el.setAttribute("style", style); + reader.reset(); + } +} + export interface FrameRequest { movers: HTMLElement[]; - reader: ReturnType; + reader: PoseReader; seek: (t: number) => void; restore?: (t: number) => void; t: number; @@ -109,10 +131,10 @@ export function readMoverFrames(req: FrameRequest): { els: HTMLElement[]; frames r.reset(); const frames = visible.map((el, i) => { const pad = req.pads?.get(el) ?? 0; - const pose = r.pose(el); const [x0, y0, x1, y1] = req.scene - ? contentBox(el, pose, req.scene, req.frameW) + ? contentBox(el, r, req.scene, req.frameW) : [0, 0, el.offsetWidth, el.offsetHeight]; + const pose = r.pose(el); const w = x1 - x0 + 2 * pad; const h = y1 - y0 + 2 * pad; const ox = x0 - pad; From f755566d475aa49b8a5c008649c57b6144af20e2 Mon Sep 17 00:00:00 2001 From: James Date: Tue, 3 Nov 2026 09:41:19 +0000 Subject: [PATCH 5/5] fix(core): keep a rotated mover the containing block while measured With only transform: none, a mover with no position stops being the containing block of its absolute and fixed children, so they jump to an outer ancestor while measured and their real overflow is cropped. Add will-change: transform during the swap, and skip the swap when the mover has no transform of its own. Co-Authored-By: Claude Opus 5.5 (1M context) --- .../runtime/captureMotionBlur/frames.test.ts | 52 +++++++++++++++++++ .../src/runtime/captureMotionBlur/frames.ts | 8 +-- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/packages/core/src/runtime/captureMotionBlur/frames.test.ts b/packages/core/src/runtime/captureMotionBlur/frames.test.ts index 4b33676736a..979fae03797 100644 --- a/packages/core/src/runtime/captureMotionBlur/frames.test.ts +++ b/packages/core/src/runtime/captureMotionBlur/frames.test.ts @@ -177,4 +177,56 @@ describe("readMoverFrames", () => { expect(frame).toMatchObject({ w: 260, h: 200 }); expect(box.getAttribute("style")).toBe("transform: rotate(45deg)"); }); + + // transform: none alone would move an absolute child to an outer containing block while measured. + it("keeps a rotated mover the containing block while it is measured", () => { + const scene = document.createElement("div"); + const box = document.createElement("div"); + const tag = document.createElement("div"); + box.setAttribute("style", "transform: rotate(45deg)"); + box.append(tag); + scene.append(box); + document.body.append(scene); + const c = Math.SQRT1_2; + const held = () => box.getAttribute("style")?.includes("will-change: transform") ?? false; + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); + laidOut(box, 200, 200, [1100, 600, 1300, 800]); + laidOut(tag, 100, 20, [0, 0, 0, 0]); + tag.getBoundingClientRect = () => rect(held() ? [1400, 690, 1500, 710] : [300, 90, 400, 110]); + const rotated: Affine = { a: c, b: c, c: -c, d: c, e: 1200, f: 600 }; + const unrotated = translate(IDENTITY, 1100, 600); + const off = () => box.getAttribute("style")?.includes("transform: none") ?? false; + const frame = readMoverFrames({ + movers: [box], + reader: { pose: () => (off() ? unrotated : rotated), reset: () => {} } as never, + seek: () => {}, + t: 0.5, + reads: 2, + shutterSeconds: 1 / 45, + taps: 4, + frameW: 1920, + frameH: 1080, + scene, + }).frames[0]; + expect(frame).toMatchObject({ w: 400, h: 200, origin: [0, 0] }); + }); + + it("leaves a mover turned only by an ancestor untouched", () => { + const scene = document.createElement("div"); + const box = document.createElement("div"); + scene.append(box); + document.body.append(scene); + const c = Math.SQRT1_2; + const styles: Array = []; + laidOut(scene, 1920, 1080, [0, 0, 1920, 1080]); + laidOut(box, 200, 200, [1059, 600, 1341, 883]); + const measure = box.getBoundingClientRect; + box.getBoundingClientRect = () => { + styles.push(box.getAttribute("style")); + return measure(); + }; + read(box, scene, { a: c, b: c, c: -c, d: c, e: 1200, f: 600 }); + expect(styles.length).toBeGreaterThan(0); + expect(styles.every((s) => s === null)).toBe(true); + }); }); diff --git a/packages/core/src/runtime/captureMotionBlur/frames.ts b/packages/core/src/runtime/captureMotionBlur/frames.ts index f92e35ebd0c..c577673471d 100644 --- a/packages/core/src/runtime/captureMotionBlur/frames.ts +++ b/packages/core/src/runtime/captureMotionBlur/frames.ts @@ -71,8 +71,9 @@ function grownBox(el: HTMLElement, pose: Affine, scene: HTMLElement, frameW: num return [Math.floor(box[0]), Math.floor(box[1]), Math.ceil(box[2]), Math.ceil(box[3])]; } -const UNTRANSFORMED = - "transform: none !important; rotate: none !important; scale: none !important; translate: none !important"; +const OWN_TRANSFORM = ["transform", "rotate", "scale", "translate"] as const; +// will-change keeps the mover the containing block of its absolute children. +const UNTRANSFORMED = `${OWN_TRANSFORM.map((p) => `${p}: none !important`).join("; ")}; will-change: transform !important`; /** The crop is cut to this box, so it must hold content that overflows the mover (wide text). */ function contentBox(el: HTMLElement, reader: PoseReader, scene: HTMLElement, frameW: number): Box { @@ -81,7 +82,8 @@ function contentBox(el: HTMLElement, reader: PoseReader, scene: HTMLElement, fra return [0, 0, el.offsetWidth, el.offsetHeight]; } const pose = reader.pose(el); - if (pose.b === 0 && pose.c === 0) return grownBox(el, pose, scene, frameW); + const ownTransform = OWN_TRANSFORM.some((p) => (cs[p] || "none") !== "none"); + if ((pose.b === 0 && pose.c === 0) || !ownTransform) return grownBox(el, pose, scene, frameW); // Upright rects of rotated content leave its corners out, so it is measured unrotated. const style = el.getAttribute("style"); el.setAttribute("style", `${style ?? ""};${UNTRANSFORMED}`);