Skip to content

Commit ab533b3

Browse files
committed
fix(engine): preserve video frame replacement geometry
1 parent e59089b commit ab533b3

2 files changed

Lines changed: 134 additions & 26 deletions

File tree

‎packages/engine/src/services/screenshotService.test.ts‎

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// @vitest-environment node
22
import { describe, it, expect, vi } from "vitest";
3+
import { parseHTML } from "linkedom";
34
import { type Page } from "puppeteer-core";
4-
import { pageScreenshotCapture, cdpSessionCache } from "./screenshotService.js";
5+
import {
6+
pageScreenshotCapture,
7+
cdpSessionCache,
8+
injectVideoFramesBatch,
9+
} from "./screenshotService.js";
510

611
// Stub a Page + CDPSession just enough that pageScreenshotCapture can call
712
// `client.send("Page.captureScreenshot", ...)` and we can inspect the args.
@@ -90,3 +95,97 @@ describe("pageScreenshotCapture supersample plumbing", () => {
9095
expect(params.clip?.scale).toBe(3);
9196
});
9297
});
98+
99+
describe("injectVideoFramesBatch replacement layout", () => {
100+
it("does not copy opposing inset constraints onto the injected frame image", async () => {
101+
const { window, document } = parseHTML(
102+
'<html><body><div id="root"><video id="clip" style="position:absolute;inset:0;width:100%;height:100%;object-fit:cover"></video></div></body></html>',
103+
);
104+
105+
Object.defineProperty(window.HTMLImageElement.prototype, "decode", {
106+
configurable: true,
107+
value: () => Promise.resolve(),
108+
});
109+
110+
const video = document.getElementById("clip") as HTMLVideoElement;
111+
Object.defineProperties(video, {
112+
offsetLeft: { configurable: true, get: () => 0 },
113+
offsetTop: { configurable: true, get: () => 0 },
114+
offsetWidth: { configurable: true, get: () => 1920 },
115+
offsetHeight: { configurable: true, get: () => 1080 },
116+
});
117+
video.getBoundingClientRect = () =>
118+
({
119+
x: 0,
120+
y: 0,
121+
left: 0,
122+
top: 0,
123+
right: 1920,
124+
bottom: 1080,
125+
width: 1920,
126+
height: 1080,
127+
toJSON: () => ({}),
128+
}) as DOMRect;
129+
130+
const computedStyle = document.createElement("div").style;
131+
computedStyle.position = "absolute";
132+
computedStyle.width = "1920px";
133+
computedStyle.height = "1080px";
134+
computedStyle.top = "0px";
135+
computedStyle.left = "0px";
136+
computedStyle.right = "0px";
137+
computedStyle.bottom = "0px";
138+
computedStyle.inset = "0px";
139+
computedStyle.objectFit = "cover";
140+
computedStyle.objectPosition = "center center";
141+
computedStyle.zIndex = "3";
142+
computedStyle.opacity = "1";
143+
Object.defineProperty(window, "getComputedStyle", {
144+
configurable: true,
145+
value: () => computedStyle,
146+
});
147+
148+
const globals = globalThis as unknown as {
149+
window?: typeof window;
150+
document?: Document;
151+
};
152+
const previousWindow = globals.window;
153+
const previousDocument = globals.document;
154+
globals.window = window;
155+
globals.document = document;
156+
try {
157+
const page = {
158+
evaluate: async (
159+
fn: (
160+
updates: Array<{ videoId: string; dataUri: string }>,
161+
visualProperties: string[],
162+
) => Promise<void>,
163+
updates: Array<{ videoId: string; dataUri: string }>,
164+
visualProperties: string[],
165+
) => fn(updates, visualProperties),
166+
} as unknown as Page;
167+
168+
await injectVideoFramesBatch(page, [
169+
{
170+
videoId: "clip",
171+
dataUri:
172+
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkAAIAAAoAAv/lxKUAAAAASUVORK5CYII=",
173+
},
174+
]);
175+
} finally {
176+
globals.window = previousWindow;
177+
globals.document = previousDocument;
178+
}
179+
180+
const img = video.nextElementSibling as HTMLImageElement | null;
181+
expect(img).not.toBeNull();
182+
expect(img?.style.position).toBe("absolute");
183+
expect(img?.style.left).toBe("0px");
184+
expect(img?.style.top).toBe("0px");
185+
expect(img?.style.width).toBe("1920px");
186+
expect(img?.style.height).toBe("1080px");
187+
expect(img?.style.right).toBe("auto");
188+
expect(img?.style.bottom).toBe("auto");
189+
expect(img?.style.inset).toBe("auto");
190+
});
191+
});

‎packages/engine/src/services/screenshotService.ts‎

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,15 @@ export async function injectVideoFramesBatch(
382382
await page.evaluate(
383383
async (items: Array<{ videoId: string; dataUri: string }>, visualProperties: string[]) => {
384384
const pendingDecodes: Array<Promise<void>> = [];
385+
const replacementLayoutProperties = new Set([
386+
"width",
387+
"height",
388+
"top",
389+
"left",
390+
"right",
391+
"bottom",
392+
"inset",
393+
]);
385394
for (const item of items) {
386395
const video = document.getElementById(item.videoId) as HTMLVideoElement | null;
387396
if (!video) continue;
@@ -395,7 +404,6 @@ export async function injectVideoFramesBatch(
395404
// and accurately reflects the user's intent on every frame.
396405
const opacityParsed = parseFloat(computedStyle.opacity);
397406
const computedOpacity = Number.isNaN(opacityParsed) ? 1 : opacityParsed;
398-
const sourceIsStatic = !computedStyle.position || computedStyle.position === "static";
399407

400408
if (isNewImage) {
401409
img = document.createElement("img");
@@ -406,10 +414,35 @@ export async function injectVideoFramesBatch(
406414
}
407415
if (!img) continue;
408416

417+
for (const property of visualProperties) {
418+
// Opacity is handled explicitly via `computedOpacity` below — copying
419+
// via the generic loop would race against the opacity:0 hide applied
420+
// to the <video> at the end of this function. GSAP may animate
421+
// opacity either on a wrapper (the <img> inherits via the stacking
422+
// context) or directly on the <video> (we must copy it to the <img>
423+
// since they are siblings). Reading computedStyle.opacity before
424+
// hiding the <video> handles both cases correctly.
425+
if (property === "opacity") continue;
426+
// Layout is set from the video's used box below. Copying authored
427+
// opposing constraints such as `inset: 0` / `right: 0` onto the
428+
// replacement <img> can overconstrain replaced-image sizing and make
429+
// some Chrome capture paths resample the frame anisotropically.
430+
if (replacementLayoutProperties.has(property)) {
431+
continue;
432+
}
433+
const value = computedStyle.getPropertyValue(property);
434+
if (value) {
435+
img.style.setProperty(property, value);
436+
}
437+
}
438+
409439
// Always use absolute positioning so the <img> overlays the <video>
410440
// instead of flowing below it. With position:relative, both elements
411441
// stack vertically — the <img> lands below the video and gets clipped
412442
// by any overflow:hidden ancestor (e.g., border-radius wrappers).
443+
//
444+
// Apply this after visual style copying so the measured used box is
445+
// the final authority for replacement frame geometry.
413446
{
414447
const videoRect = video.getBoundingClientRect();
415448
const offsetLeft = Number.isFinite(video.offsetLeft) ? video.offsetLeft : 0;
@@ -429,30 +462,6 @@ export async function injectVideoFramesBatch(
429462
img.style.objectPosition = computedStyle.objectPosition;
430463
img.style.zIndex = computedStyle.zIndex;
431464

432-
for (const property of visualProperties) {
433-
// Opacity is handled explicitly via `computedOpacity` below — copying
434-
// via the generic loop would race against the opacity:0 hide applied
435-
// to the <video> at the end of this function. GSAP may animate
436-
// opacity either on a wrapper (the <img> inherits via the stacking
437-
// context) or directly on the <video> (we must copy it to the <img>
438-
// since they are siblings). Reading computedStyle.opacity before
439-
// hiding the <video> handles both cases correctly.
440-
if (property === "opacity") continue;
441-
if (
442-
sourceIsStatic &&
443-
(property === "top" ||
444-
property === "left" ||
445-
property === "right" ||
446-
property === "bottom" ||
447-
property === "inset")
448-
) {
449-
continue;
450-
}
451-
const value = computedStyle.getPropertyValue(property);
452-
if (value) {
453-
img.style.setProperty(property, value);
454-
}
455-
}
456465
img.decoding = "sync";
457466
if (img.getAttribute("src") !== item.dataUri) {
458467
img.src = item.dataUri;

0 commit comments

Comments
 (0)