From ba1fa8b12aefcb9cd94ce2e79b62b0cc0cb79cf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Tue, 20 Oct 2026 17:31:45 +0000 Subject: [PATCH] fix(cli): a scoped keepsMoving capture ignores the page background behind it (#5966) The root (and propagated body) background paints the page canvas whatever its visibility, so an animated page background could make a still scope read as moving. Scoped captures now clear those backgrounds and keep the resulting transparency, and the frame comparison reads alpha, so dark paint revealed on the cleared page still counts. --- .../src/commands/motion-signature.browser.js | 3 ++- packages/cli/src/utils/checkBrowser.ts | 10 +++++++-- .../cli/src/utils/checkSweep.chromium.test.ts | 21 +++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/commands/motion-signature.browser.js b/packages/cli/src/commands/motion-signature.browser.js index 24760de4b7..0bd54ddcab 100644 --- a/packages/cli/src/commands/motion-signature.browser.js +++ b/packages/cli/src/commands/motion-signature.browser.js @@ -428,7 +428,8 @@ return ( Math.abs(a[i] - b[i]) > FRAME_CHANNEL_NOISE || Math.abs(a[i + 1] - b[i + 1]) > FRAME_CHANNEL_NOISE || - Math.abs(a[i + 2] - b[i + 2]) > FRAME_CHANNEL_NOISE + Math.abs(a[i + 2] - b[i + 2]) > FRAME_CHANNEL_NOISE || + Math.abs(a[i + 3] - b[i + 3]) > FRAME_CHANNEL_NOISE ); } diff --git a/packages/cli/src/utils/checkBrowser.ts b/packages/cli/src/utils/checkBrowser.ts index 35b1deece5..ddbf9cd6be 100644 --- a/packages/cli/src/utils/checkBrowser.ts +++ b/packages/cli/src/utils/checkBrowser.ts @@ -1013,14 +1013,19 @@ async function captureFrame(page: Page, scope?: string): Promise { try { const box = await page.evaluate(isolateScopePaint, scope); if (!box || box.width < 1 || box.height < 1) return ""; - return await page.screenshot({ encoding: "base64", type: "png", clip: box }); + return await page.screenshot({ + encoding: "base64", + type: "png", + clip: box, + omitBackground: true, + }); } finally { await page.evaluate(restoreScopePaint); } } // Liveness signs only the scope's subtree, so its frame must show only that paint: anything else behind or -// over the box (a moving background, grain) would read as the scope moving. Hidden like the contrast pass's glyphs. +// over the box (a moving background, grain, the page canvas) would read as the scope moving; the canvas stays clear. function isolateScopePaint( scope: string, ): { x: number; y: number; width: number; height: number } | null { @@ -1036,6 +1041,7 @@ function isolateScopePaint( const style = document.createElement("style"); style.id = "__hf_capture_scope_style"; style.textContent = `:not([data-hf-capture-scope], [data-hf-capture-scope] *) { visibility: hidden !important; } + :root:not([data-hf-capture-scope]), body:not([data-hf-capture-scope]) { background: none !important; } [data-hf-capture-scope] { visibility: ${visibility} !important; }`; document.head.append(style); const rect = target.getBoundingClientRect(); diff --git a/packages/cli/src/utils/checkSweep.chromium.test.ts b/packages/cli/src/utils/checkSweep.chromium.test.ts index ce018a6e11..70cf5fb7a8 100644 --- a/packages/cli/src/utils/checkSweep.chromium.test.ts +++ b/packages/cli/src/utils/checkSweep.chromium.test.ts @@ -150,6 +150,27 @@ describe.skipIf(!RUNS_CHROMIUM)("frozen-sweep guard in Chromium", () => { expect(result.frozen).toEqual([expect.stringContaining("nothing moves within #title")]); }); + it("keeps a still title frozen under keepsMoving while the page's root background changes", async () => { + const html = film( + `

Still

`, + `document.documentElement.style.background = "rgb(" + Math.round(t * 40) + ", 0, 80)";`, + ); + const result = await findings(html, [{ kind: "keepsMoving", withinSelector: "#title" }]); + expect(result.frozen).toEqual([expect.stringContaining("nothing moves within #title")]); + }); + + it("sees dark paint revealed inside a scope, where only its alpha tells it from the cleared page", async () => { + const html = film( + ` + + `, + `document.getElementById("hole").setAttribute("r", String(t * 60));`, + ); + const result = await findings(html, [{ kind: "keepsMoving", withinSelector: "#scene" }]); + expect(result.frozen).toEqual([]); + }); + it("still warns on a canvas that animates by the wall clock, not by the seek", async () => { const html = film( `