Skip to content

Commit 01350bf

Browse files
committed
fix(shader-transitions): pin clones to their measured live box, not composition size
Addresses review feedback on #2254: the previous fix hardcoded every clone to the full composition width/height regardless of the source scene's own sizing, silently overriding any authored explicit width/height. Measuring each scene's getBoundingClientRect() while still live in the document (its inset:0 already resolves correctly against the real ancestor chain there) and reapplying that exact box to the clone fixes the 0x0 collapse without ever clobbering an author's own layout.
1 parent 0b49bf3 commit 01350bf

2 files changed

Lines changed: 70 additions & 11 deletions

File tree

‎packages/shader-transitions/src/engineModePageComposite.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { afterEach, describe, expect, it, vi } from "vitest";
22
import {
3+
clonePinStyleFor,
34
isPageSideCompositingSupported,
45
PAGE_COMPOSITOR_BUILD_CANARY,
56
PAGE_COMPOSITOR_CANVAS_ID,
@@ -69,6 +70,26 @@ describe("isPageSideCompositingSupported", () => {
6970
});
7071
});
7172

73+
describe("clonePinStyleFor", () => {
74+
it("fixes a 0x0 inset:0 scene root to its live-measured box (the collapse this exists to prevent)", () => {
75+
// A scene root sized only by `position:absolute; inset:0` measures as
76+
// the full composition frame in the live document (its containing block
77+
// there is the real ancestor chain) — collapses to 0x0 only once cloned
78+
// into the staging canvas's own layout subtree.
79+
const pin = clonePinStyleFor({ left: 0, top: 0, width: 1080, height: 1920 });
80+
expect(pin).toEqual({ left: "0px", top: "0px", width: "1080px", height: "1920px" });
81+
});
82+
83+
it("preserves an authored explicit width/height and offset instead of overriding it", () => {
84+
// A scene root with its own explicit size/position (e.g. a picture-in-
85+
// picture panel) measures as that exact box in the live document —
86+
// clonePinStyleFor must reproduce it verbatim, not the full composition
87+
// frame, or the clone would silently grow to fill the canvas.
88+
const pin = clonePinStyleFor({ left: 120, top: 240, width: 400, height: 300 });
89+
expect(pin).toEqual({ left: "120px", top: "240px", width: "400px", height: "300px" });
90+
});
91+
});
92+
7293
describe("page-side compositor exported constants", () => {
7394
it("exports a stable canary string used by the bundled-CLI smoke", () => {
7495
expect(PAGE_COMPOSITOR_BUILD_CANARY).toBe("__hf_page_compositor_v1__");

‎packages/shader-transitions/src/engineModePageComposite.ts‎

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,36 @@ interface ResolvedTransition {
7676
export const PAGE_COMPOSITOR_CANVAS_ID = "__hf-page-side-compositor";
7777
export const PAGE_COMPOSITOR_BUILD_CANARY = "__hf_page_compositor_v1__";
7878

79+
export interface ClonePinStyle {
80+
left: string;
81+
top: string;
82+
width: string;
83+
height: string;
84+
}
85+
86+
/**
87+
* Style values to pin a cloned scene root to the box its source measured
88+
* WHILE STILL LIVE in the document — never the composition's full pixel size.
89+
* A live-document `getBoundingClientRect()` already resolves `inset:0` (and
90+
* any authored explicit width/height) correctly against the real ancestor
91+
* chain; reapplying that exact box to the clone fixes the 0x0 collapse a
92+
* detached `inset:0` clone would otherwise have inside the staging canvas's
93+
* layout subtree, without ever overriding an author's own sizing.
94+
*/
95+
export function clonePinStyleFor(rect: {
96+
left: number;
97+
top: number;
98+
width: number;
99+
height: number;
100+
}): ClonePinStyle {
101+
return {
102+
left: `${rect.left}px`,
103+
top: `${rect.top}px`,
104+
width: `${rect.width}px`,
105+
height: `${rect.height}px`,
106+
};
107+
}
108+
79109
export function isPageSideCompositingSupported(): boolean {
80110
if (typeof window === "undefined" || typeof document === "undefined") return false;
81111
if (!isHtmlInCanvasCaptureSupported()) return false;
@@ -232,6 +262,17 @@ export function installPageSideCompositor(options: PageCompositorInstallOptions)
232262
pWin.__hf_page_composite_pending = false;
233263
return false;
234264
}
265+
// Measure each scene's rendered box WHILE STILL LIVE — a scene root sized
266+
// only by `position:absolute; inset:0` resolves to 0x0 once cloned into
267+
// the staging canvas's layout subtree (no containing-block dimensions
268+
// there), and the transition textures blank out (wild report: explicit
269+
// 1080x1920 anchors fixed both transitions). The live document already
270+
// resolves inset:0 (and any authored explicit width/height) correctly
271+
// against the real ancestor chain, so pinning the clone to THIS measured
272+
// box fixes the collapse without ever overriding an author's own sizing.
273+
const fromPin = clonePinStyleFor(fromEl.getBoundingClientRect());
274+
const toPin = clonePinStyleFor(toEl.getBoundingClientRect());
275+
235276
while (fromStaging.firstChild) fromStaging.removeChild(fromStaging.firstChild);
236277
while (toStaging.firstChild) toStaging.removeChild(toStaging.firstChild);
237278
const fromClone = fromEl.cloneNode(true) as HTMLElement;
@@ -244,20 +285,17 @@ export function installPageSideCompositor(options: PageCompositorInstallOptions)
244285
// paint record" and the shader degrades to a hard cut. The shader blends from
245286
// full-opacity textures via u_progress, so force the clones visible. Cf.
246287
// forceSceneVisibleInClone (html2canvas path).
247-
for (const clone of [fromClone, toClone]) {
288+
for (const [clone, pin] of [
289+
[fromClone, fromPin],
290+
[toClone, toPin],
291+
] as const) {
248292
clone.style.opacity = "1";
249293
clone.style.visibility = "visible";
250-
// A scene root sized only by `position:absolute; inset:0` resolves to
251-
// 0x0 inside the staging canvas's layout subtree (no containing-block
252-
// dimensions there) — the clone then paints nothing and the transition
253-
// textures are blank (wild report: explicit 1080x1920 anchors fixed
254-
// both transitions). Pin the clone to the composition's pixel size;
255-
// scenes that already carry explicit dimensions are unaffected.
256294
clone.style.position = "absolute";
257-
clone.style.left = "0";
258-
clone.style.top = "0";
259-
clone.style.width = `${width}px`;
260-
clone.style.height = `${height}px`;
295+
clone.style.left = pin.left;
296+
clone.style.top = pin.top;
297+
clone.style.width = pin.width;
298+
clone.style.height = pin.height;
261299
clone.querySelectorAll<HTMLElement>("[data-start]").forEach((el) => {
262300
el.style.opacity = "1";
263301
el.style.visibility = "visible";

0 commit comments

Comments
 (0)