diff --git a/packages/core/src/runtime/captureMotionBlur/composite.ts b/packages/core/src/runtime/captureMotionBlur/composite.ts index bd2b53a053..969bd03890 100644 --- a/packages/core/src/runtime/captureMotionBlur/composite.ts +++ b/packages/core/src/runtime/captureMotionBlur/composite.ts @@ -14,6 +14,7 @@ import { type Affine, invert, sweep, tapWeights, thinTaps } from "./pose"; export interface MoverFrame { w: number; h: number; + 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 80a3ba695b..77ec7f9959 100644 --- a/packages/core/src/runtime/captureMotionBlur/filterPlan.ts +++ b/packages/core/src/runtime/captureMotionBlur/filterPlan.ts @@ -82,14 +82,15 @@ 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; + 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 a56aad1e2f..61e68e7ce2 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 0000000000..979fae0379 --- /dev/null +++ b/packages/core/src/runtime/captureMotionBlur/frames.test.ts @@ -0,0 +1,232 @@ +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { readMoverFrames } from "./frames"; +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]) { + 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; +} + +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", () => { + 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); + }); + + 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 }); + }); + // 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 }); + }); + + // 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)"); + }); + + // 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 651d25a394..c577673471 100644 --- a/packages/core/src/runtime/captureMotionBlur/frames.ts +++ b/packages/core/src/runtime/captureMotionBlur/frames.ts @@ -6,13 +6,100 @@ import { type createPoseReader, IDENTITY, autoTaps, + invert, tapsFromReads, translate, } from "./pose"; +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(); + 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, + }); +} + +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], + [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); + } +} + +function grownBox(el: HTMLElement, pose: Affine, scene: HTMLElement, frameW: number): Box { + const box: Box = [0, 0, el.offsetWidth, el.offsetHeight]; + 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++) { + growOutside((walker.currentNode as Element).getBoundingClientRect()); + } + return [Math.floor(box[0]), Math.floor(box[1]), Math.ceil(box[2]), Math.ceil(box[3])]; +} + +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 { + const cs = getComputedStyle(el); + if (cs.overflowX !== "visible" && cs.overflowY !== "visible") { + return [0, 0, el.offsetWidth, el.offsetHeight]; + } + const pose = reader.pose(el); + 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}`); + 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; @@ -23,6 +110,7 @@ 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; + scene?: HTMLElement; frameW: number; frameH: number; } @@ -45,14 +133,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 [x0, y0, x1, y1] = req.scene + ? 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; + 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 cedd037a0c..150b142768 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(); @@ -1981,3 +1985,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 f21954bd45..3285eb8579 100644 --- a/packages/core/src/runtime/captureMotionBlur/index.ts +++ b/packages/core/src/runtime/captureMotionBlur/index.ts @@ -65,6 +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"; +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 { @@ -169,8 +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; }`, - `[${ONLY_ATTR}]::before, [${ONLY_ATTR}]::after, [${ONLY_ATTR}] *:not([${MOVER_ATTR}], [${MOVER_ATTR}] *) { visibility: hidden !important; }`, + `[${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_DRAWN_BY_REFERENCE.join(", ")}) { visibility: visible !important; }`, NESTED_CANVAS_KEEPS_ITS_PLACE, ].join("\n"); @@ -280,6 +283,7 @@ function createInstance(host: CaptureMotionBlurHost, config: Config, form: HtmlI taps: config.samples, pads, frameW: width, + scene: root, frameH: height, }); }