diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0255de1a06..042006a818 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1181,6 +1181,11 @@ jobs: node packages/studio/tests/e2e/timeline-snap-release.mjs || rc=1 STUDIO_URL="http://localhost:5316/#project/aroll-backdrop" \ node packages/studio/tests/e2e/aroll-backdrop.mjs || rc=1 + rm -rf packages/studio/data/projects/graded-video-transitions + cp -R packages/studio/tests/e2e/fixtures/graded-video-transitions \ + packages/studio/data/projects/graded-video-transitions + STUDIO_URL="http://localhost:5316/#project/graded-video-transitions" \ + node packages/studio/tests/e2e/graded-video-transitions.mjs || rc=1 exit "$rc" - name: Measure the caret of a text edited in place if: success() || failure() diff --git a/packages/core/src/runtime/colorGrading.test.ts b/packages/core/src/runtime/colorGrading.test.ts index 6a59ba09b5..6e0e05d296 100644 --- a/packages/core/src/runtime/colorGrading.test.ts +++ b/packages/core/src/runtime/colorGrading.test.ts @@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { mediaElementTime } from "./mediaClock"; import { PREVIEW_SEEK_HOLD_ATTR } from "../studioPreviewMark"; import { + COLOR_GRADING_SOURCE_HIDDEN_ATTR, HF_COLOR_GRADING_ATTR, HF_COLOR_GRADING_EFFECT_KEYS, serializeHfColorGrading, @@ -312,34 +313,30 @@ describe("createColorGradingRuntime", () => { runtime?.redraw(); } - it("restores the authored inline opacity captured before animation transients", () => { + it("hides its source without touching the source's own opacity, which the canvas copies", () => { const video = makeDrawableVideo(); - // Parse-time capture stamped the authored value; by hide time GSAP has - // already left a from()-tween transient (0) in the inline style. - video.setAttribute("data-hf-authored-opacity", "0.75"); - video.style.opacity = "0"; - startRuntimeWithVideo(video); + video.style.opacity = "0.75"; + const { canvas } = startRuntimeWithVideo(video); - expect(video.style.getPropertyPriority("opacity")).toBe("important"); - - runtime?.destroy(); - runtime = null; - - // Restore must use the authored 0.75, not the GSAP transient 0. + expect(video.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe("true"); expect(video.style.getPropertyValue("opacity")).toBe("0.75"); expect(video.style.getPropertyPriority("opacity")).toBe(""); - }); - - it("restores no inline opacity when the authored capture recorded none", () => { - const video = makeDrawableVideo(); - video.setAttribute("data-hf-authored-opacity", ""); - video.style.opacity = "0"; - startRuntimeWithVideo(video); + expect(canvas.style.opacity).toBe("0.75"); runtime?.destroy(); runtime = null; - expect(video.style.getPropertyValue("opacity")).toBe(""); + expect(video.hasAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe(false); + expect(video.style.getPropertyValue("opacity")).toBe("0.75"); + }); + + it("hides its source with a filter, keeping its hit area, with one rule for the whole document", () => { + startRuntimeWithVideo(); + const rules = [...document.querySelectorAll("style")].filter((style) => + style.textContent?.includes(`[${COLOR_GRADING_SOURCE_HIDDEN_ATTR}]`), + ); + expect(rules).toHaveLength(1); + expect(rules[0]?.textContent).toContain("filter: opacity(0) !important"); }); it("re-syncs the graded canvas when the source's inline transform changes", async () => { @@ -391,8 +388,7 @@ describe("createColorGradingRuntime", () => { expect(canvas.id).toBe("__hf_color_grading_hero-video"); expect(video.style.getPropertyValue("visibility")).toBe(""); - expect(video.style.getPropertyValue("opacity")).toBe("0"); - expect(video.style.getPropertyPriority("opacity")).toBe("important"); + expect(video.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe("true"); expect(video.hasAttribute("data-hf-color-grading-source-hidden")).toBe(true); expect(canvas?.style.visibility).toBe("visible"); expect(canvas?.style.opacity).toBe("1"); @@ -412,8 +408,7 @@ describe("createColorGradingRuntime", () => { expect(recreated).not.toBeNull(); expect(recreated).toBe(canvas); expect(webglContexts()).toBe(1); - expect(video.style.getPropertyValue("opacity")).toBe("0"); - expect(video.style.getPropertyPriority("opacity")).toBe("important"); + expect(video.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe("true"); }); it("draws every graded clip through one WebGL context, however often a seek run shows and hides them", () => { @@ -949,7 +944,7 @@ describe("createColorGradingRuntime", () => { expect(runtime.isGraded(canvas)).toBe(true); expect(runtime.getStatus("drawn")).toMatchObject({ state: "active" }); - expect(canvas.style.getPropertyValue("opacity")).toBe("0"); + expect(canvas.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe("true"); expect(canvasUploads(canvas)).toBe(1); expect(runtime.redrawAnimated()).toBe(1); @@ -1002,8 +997,7 @@ describe("createColorGradingRuntime", () => { runtime.redraw(); expect(canvas.style.display).toBe("block"); - expect(video.style.getPropertyValue("opacity")).toBe("0"); - expect(video.style.getPropertyPriority("opacity")).toBe("important"); + expect(video.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe("true"); }); it("caps a clip shown far larger than the screen to what a GPU can draw, keeping its shape", () => { @@ -1029,7 +1023,7 @@ describe("createColorGradingRuntime", () => { const { video } = startRuntimeWithVideo(); - expect(video.style.getPropertyValue("opacity")).not.toBe("0"); + expect(video.hasAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe(false); }); it("gives a drawn clip back to its source when a later draw outgrows the drawing buffer", () => { @@ -1038,14 +1032,14 @@ describe("createColorGradingRuntime", () => { type === "webgl" ? createMockWebGl({ drawingBuffer }) : type === "2d" ? mock2d() : null, ); const { video, canvas } = startRuntimeWithVideo(); - expect(video.style.getPropertyValue("opacity")).toBe("0"); + expect(video.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe("true"); drawingBuffer.width = 100; drawingBuffer.height = 100; Object.defineProperty(video, "offsetWidth", { value: 3000, configurable: true }); video.dispatchEvent(new Event("seeked")); - expect(video.style.getPropertyValue("opacity")).not.toBe("0"); + expect(video.hasAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe(false); expect(canvas.style.display).toBe("none"); }); @@ -1099,7 +1093,7 @@ describe("createColorGradingRuntime", () => { expect(canvas.style.opacity).toBe("0.75"); }); - it("drops the injected render frame's border while grading draws over it", () => { + it("hides the injected render frame while grading draws over it, and shows it after", () => { const video = makeDrawableVideo(); Object.defineProperty(video, "readyState", { value: HTMLMediaElement.HAVE_METADATA, @@ -1125,12 +1119,12 @@ describe("createColorGradingRuntime", () => { runtime.redraw(); expect(canvas.style.display).toBe("block"); - expect(frame.style.borderStyle).toBe("none"); + expect(frame.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe("true"); - video.style.border = "16px dashed rgb(255, 0, 0)"; video.removeAttribute(HF_COLOR_GRADING_ATTR); runtime.refresh(); - expect(frame.style.borderStyle).toBe("dashed"); + expect(frame.hasAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe(false); + expect(frame.style.borderStyle).toBe("solid"); }); it("keeps a staged scene copy's canvas on its own render frame (#3994)", async () => { @@ -1199,7 +1193,6 @@ describe("createColorGradingRuntime", () => { runtime.redraw(); expect(canvas.style.visibility).toBe("visible"); - // Grading still owns the source's opacity hide, so the canvas keeps full opacity. expect(canvas.style.opacity).toBe("1"); }); @@ -1224,7 +1217,7 @@ describe("createColorGradingRuntime", () => { expect(runtime.isGraded(image)).toBe(true); expect(image.nextElementSibling?.hasAttribute("data-hf-color-grading-canvas")).toBe(true); - expect(image.style.getPropertyValue("opacity")).toBe("0"); + expect(image.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe("true"); }); it.each([ @@ -1251,34 +1244,16 @@ describe("createColorGradingRuntime", () => { expect(runtime.isGraded(image)).toBe(false); }); - it("updates canvas opacity through grading's own hide when CSS animation progresses (#3329)", () => { - const video = makeDrawableVideo(); - // Simulate a CSS entrance animation: the authored opacity is empty (no - // inline opacity), but at parse time the element has opacity 0 from the - // animation's initial keyframe. - video.setAttribute("data-hf-authored-opacity", ""); - document.body.appendChild(video); - - runtime = createColorGradingRuntime(); - const canvas = document.querySelector("[data-hf-color-grading-canvas]"); - if (!canvas) throw new Error("Expected color grading canvas"); - - // Source is hidden by color grading. - expect(video.style.getPropertyValue("opacity")).toBe("0"); - expect(video.style.getPropertyPriority("opacity")).toBe("important"); - - // The canvas opacity should NOT be frozen at "0" — after the hide is - // temporarily lifted, jsdom's getComputedStyle returns "" (no inline - // opacity set), which the code normalizes to "1". + it("copies the source's animated opacity to the canvas on every redraw (#3329)", () => { + const { video, canvas } = startRuntimeWithVideo(); + expect(video.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe("true"); expect(canvas.style.opacity).toBe("1"); - // Simulate the CSS animation progressing by re-reading after a redraw. - runtime.redraw(); - expect(canvas.style.opacity).toBe("1"); + video.style.opacity = "0.4"; + runtime?.redraw(); - // Source remains hidden throughout. - expect(video.style.getPropertyValue("opacity")).toBe("0"); - expect(video.style.getPropertyPriority("opacity")).toBe("important"); + expect(canvas.style.opacity).toBe("0.4"); + expect(video.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe("true"); }); it("allows a drawable producer render frame to initialize hidden source grading", () => { @@ -2324,7 +2299,7 @@ describe("createColorGradingRuntime", () => { expect(runtime?.getStatus("#hero-video").state).toBe("active"); expect(video.hasAttribute("data-hf-color-grading-source-hidden")).toBe(true); - expect(video.style.getPropertyValue("opacity")).toBe("0"); + expect(video.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)).toBe("true"); }); it("does not rescan the document when only text changes", async () => { diff --git a/packages/core/src/runtime/colorGrading.ts b/packages/core/src/runtime/colorGrading.ts index 4b43aa5f1f..8f0f470cb4 100644 --- a/packages/core/src/runtime/colorGrading.ts +++ b/packages/core/src/runtime/colorGrading.ts @@ -232,9 +232,7 @@ interface ColorGradingEntry extends ColorGradingRenderer { touchedParent: HTMLElement | null; parentInlinePosition: string | null; sourceHidden: boolean; - borderlessFrame: HTMLElement | null; - sourceInlineOpacity: string | null; - sourceInlineOpacityPriority: string; + hiddenFrame: HTMLElement | null; sourceOpacityForCanvas: string; sourceVisibleForCanvas: boolean; hasDrawn: boolean; @@ -2213,36 +2211,51 @@ function adoptContext( else drawEntry(entry); } -function hideFrameBorder(entry: ColorGradingEntry, frame: HTMLElement): void { - if (entry.borderlessFrame !== frame) restoreFrameBorder(entry); - entry.borderlessFrame = frame; - frame.style.borderStyle = "none"; +const HIDE_STYLE_ATTR = "data-hf-color-grading-hide"; + +// A hidden source keeps its own opacity, which the canvas copies, and its hit area, so a click still picks it. +function hideFromView(element: HTMLElement): void { + const doc = element.ownerDocument; + if (!doc.querySelector(`style[${HIDE_STYLE_ATTR}]`)) { + const style = doc.createElement("style"); + style.setAttribute(HIDE_STYLE_ATTR, ""); + style.textContent = `[${COLOR_GRADING_SOURCE_HIDDEN_ATTR}] { filter: opacity(0) !important; }`; + (doc.head ?? doc.documentElement).appendChild(style); + } + element.setAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR, "true"); } -function restoreFrameBorder(entry: ColorGradingEntry): void { - const frame = entry.borderlessFrame; - if (!frame) return; - entry.borderlessFrame = null; - frame.style.borderStyle = window.getComputedStyle(entry.element).borderStyle; +/** The filter the page gives `element`, read with grading's hide lifted for that one synchronous read. */ +function ownFilter(element: HTMLElement): string { + const sheet = element.ownerDocument.querySelector( + `style[${HIDE_STYLE_ATTR}]`, + )?.sheet; + if (!sheet || !element.hasAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR)) { + return window.getComputedStyle(element).filter; + } + sheet.disabled = true; + try { + return window.getComputedStyle(element).filter; + } finally { + sheet.disabled = false; + } +} + +function hideFrame(entry: ColorGradingEntry, frame: HTMLElement): void { + if (entry.hiddenFrame !== frame) restoreFrame(entry); + entry.hiddenFrame = frame; + hideFromView(frame); +} + +function restoreFrame(entry: ColorGradingEntry): void { + entry.hiddenFrame?.removeAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR); + entry.hiddenFrame = null; } function restoreSourceElement(entry: ColorGradingEntry): void { - restoreFrameBorder(entry); + restoreFrame(entry); if (!entry.sourceHidden) return; entry.element.removeAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR); - const opacity = entry.element.style.getPropertyValue("opacity"); - const priority = entry.element.style.getPropertyPriority("opacity"); - if (opacity === "0" && priority === "important") { - if (entry.sourceInlineOpacity === null) { - entry.element.style.removeProperty("opacity"); - } else { - entry.element.style.setProperty( - "opacity", - entry.sourceInlineOpacity, - entry.sourceInlineOpacityPriority, - ); - } - } entry.sourceHidden = false; } @@ -2934,6 +2947,7 @@ function updateCanvasLayout( const computed = window.getComputedStyle(styleSource); copyMediaVisualStyles(canvas.style, computed); + canvas.style.filter = ownFilter(element); canvas.style.borderStyle = "none"; canvas.style.pointerEvents = "none"; canvas.style.position = "absolute"; @@ -3244,24 +3258,7 @@ function applyUniforms( } function hideSourceElement(entry: ColorGradingEntry): void { - if (!entry.sourceHidden) { - // Prefer the parse-time authored capture: by the time the first hide runs, - // the inline opacity is usually an animation-engine transient (a from() - // tween's 0 at playhead 0), and restoring THAT when grading is removed - // would leave the element invisible. Fall back to the live inline value - // for documents loaded without the capture installed. - // `null` = never captured; "" = captured, authored none (store as null). - const authored = entry.element.getAttribute(COLOR_GRADING_AUTHORED_OPACITY_ATTR); - if (authored !== null) { - entry.sourceInlineOpacity = authored === "" ? null : authored; - entry.sourceInlineOpacityPriority = ""; - } else { - entry.sourceInlineOpacity = entry.element.style.getPropertyValue("opacity") || null; - entry.sourceInlineOpacityPriority = entry.element.style.getPropertyPriority("opacity"); - } - } - entry.element.setAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR, "true"); - entry.element.style.setProperty("opacity", "0", "important"); + hideFromView(entry.element); entry.sourceHidden = true; } @@ -3434,50 +3431,18 @@ function drawableFrame(entry: ColorGradingEntry): DrawableFrame | null { return size ? { source, pixels, size } : null; } -function restoreAuthoredOpacity(entry: ColorGradingEntry): void { - if (entry.sourceInlineOpacity === null) { - entry.element.style.removeProperty("opacity"); - return; - } - entry.element.style.setProperty( - "opacity", - entry.sourceInlineOpacity, - entry.sourceInlineOpacityPriority || "", - ); -} - -function isHiddenByColorGrading(entry: ColorGradingEntry): boolean { - const { style } = entry.element; - return ( - entry.sourceHidden && - style.getPropertyValue("opacity") === "0" && - style.getPropertyPriority("opacity") === "important" - ); -} - function syncSourceAppearance( entry: ColorGradingEntry, source: TexImageSource, injectedFrameSource: boolean, ): void { - const hiddenByColorGrading = isHiddenByColorGrading(entry); const sourceVisibility = entry.element.style.getPropertyValue("visibility"); - // When color grading hid the source (opacity:0 !important), temporarily - // restore the authored inline opacity so getComputedStyle reflects the CSS - // animation's current value instead of our own hide. Without this, a CSS - // entrance animation (opacity: 0→1) freezes at its first-frame value for - // the entire render because the !important blocks all animation effects on - // the property (#3329). The restore–read–rehide is synchronous, so no - // repaint occurs between the style writes. - const restoresOpacity = hiddenByColorGrading && !injectedFrameSource; - if (restoresOpacity) restoreAuthoredOpacity(entry); const computed = window.getComputedStyle( injectedFrameSource ? (source as HTMLImageElement) : entry.element, ); // Grading never writes `visibility`, so the source's computed visibility // always tracks the clip window and has to be re-read every frame. entry.sourceOpacityForCanvas = computed.opacity || "1"; - if (restoresOpacity) entry.element.style.setProperty("opacity", "0", "important"); entry.sourceVisibleForCanvas = (injectedFrameSource || sourceVisibility !== "hidden") && computed.visibility !== "hidden"; } @@ -3574,7 +3539,7 @@ function drawEntry(entry: ColorGradingEntry): boolean { try { renderGradedFrame(entry, pixels, layout, uv); hideSourceElement(entry); - if (injectedFrameSource) hideFrameBorder(entry, source); + if (injectedFrameSource) hideFrame(entry, source); entry.hasDrawn = true; entry.drawError = null; return true; @@ -4034,9 +3999,7 @@ export function createColorGradingRuntime(pausedMediaLease?: { touchedParent: null, parentInlinePosition: null, sourceHidden: false, - borderlessFrame: null, - sourceInlineOpacity: null, - sourceInlineOpacityPriority: "", + hiddenFrame: null, sourceOpacityForCanvas: window.getComputedStyle(element).opacity || "1", sourceVisibleForCanvas: window.getComputedStyle(element).visibility !== "hidden", hasDrawn: false, diff --git a/packages/engine/src/services/screenshotService.test.ts b/packages/engine/src/services/screenshotService.test.ts index 892fc7f78a..63bdfc65f8 100644 --- a/packages/engine/src/services/screenshotService.test.ts +++ b/packages/engine/src/services/screenshotService.test.ts @@ -727,9 +727,9 @@ describe("video-frame injection respects ancestor visibility", () => { expect(sibling?.style.visibility).toBe("visible"); }); - it("does not copy color-grading source suppression opacity to the injected frame", async () => { + it("copies a color-graded video's animated opacity to the injected frame", async () => { const { teardown, setup } = withGlobals( - setupHostHiddenScenario({}, { videoStyle: { opacity: "0" } }), + setupHostHiddenScenario({}, { videoStyle: { opacity: "0.4" } }), ); setup.video.setAttribute("data-hf-color-grading-source-hidden", "true"); @@ -747,15 +747,15 @@ describe("video-frame injection respects ancestor visibility", () => { const sibling = setup.video.nextElementSibling as HTMLElement | null; expect(sibling?.classList.contains("__render_frame__")).toBe(true); - expect(sibling?.style.opacity).toBe("1"); + expect(sibling?.style.opacity).toBe("0.4"); }); - it("repairs stale injected-frame opacity while syncing color-graded active videos", async () => { + it("leaves the injected frame's opacity alone while syncing color-graded active videos", async () => { const { teardown, setup } = withGlobals(setupHostHiddenScenario({})); setup.video.setAttribute("data-hf-color-grading-source-hidden", "true"); const seededImg = setup.document.createElement("img"); seededImg.classList.add("__render_frame__"); - seededImg.style.opacity = "0"; + seededImg.style.opacity = "0.4"; setup.video.parentNode?.insertBefore(seededImg, setup.video.nextSibling); const setSourceVisibility = vi.fn(); (setup.window as unknown as { __hf: unknown }).__hf = { @@ -768,7 +768,7 @@ describe("video-frame injection respects ancestor visibility", () => { teardown(); } - expect(seededImg.style.opacity).toBe("1"); + expect(seededImg.style.opacity).toBe("0.4"); expect(seededImg.style.visibility).toBe("visible"); expect(setSourceVisibility).toHaveBeenCalledWith(setup.video, true); }); diff --git a/packages/engine/src/services/screenshotService.ts b/packages/engine/src/services/screenshotService.ts index 99065b9280..96616edcb3 100644 --- a/packages/engine/src/services/screenshotService.ts +++ b/packages/engine/src/services/screenshotService.ts @@ -9,7 +9,6 @@ import { type Page } from "puppeteer-core"; import { withVideoFrameSources, type VideoFrameUpdate } from "./videoFrameTransport.js"; import { type CaptureOptions } from "../types.js"; -import { COLOR_GRADING_SOURCE_HIDDEN_ATTR } from "@hyperframes/core/color-grading"; import { HF_COLOR_GRADING_CANVAS_ID_PREFIX, MEDIA_RENDER_ID_ATTR, @@ -699,7 +698,6 @@ async function injectResolvedVideoFramesBatch( async ( items: Array & { videoId: string; frameId: string }>, visualProperties: string[], - colorGradingSourceHiddenAttr: string, ) => { const injectedIds: string[] = []; const pendingDecodes: Array> = []; @@ -783,11 +781,7 @@ async function injectResolvedVideoFramesBatch( // `opacity: 0`), so its computed opacity is preserved across seeks // and accurately reflects the user's intent on every frame. const opacityParsed = parseFloat(computedStyle.opacity); - const computedOpacity = video.hasAttribute(colorGradingSourceHiddenAttr) - ? 1 - : Number.isNaN(opacityParsed) - ? 1 - : opacityParsed; + const computedOpacity = Number.isNaN(opacityParsed) ? 1 : opacityParsed; // Measure first: an in-flow bordered sibling would shrink the video's flex box. const videoRect = video.getBoundingClientRect(); @@ -888,7 +882,6 @@ async function injectResolvedVideoFramesBatch( }, items, [...MEDIA_VISUAL_STYLE_PROPERTIES], - COLOR_GRADING_SOURCE_HIDDEN_ATTR, ); } @@ -898,7 +891,7 @@ export async function syncVideoFrameVisibility( ): Promise { await page.evaluate( // fallow-ignore-next-line complexity - (ids: string[], colorGradingSourceHiddenAttr: string) => { + (ids: string[]) => { // Mirror the ancestor-visibility guard from `injectVideoFramesBatch`. // See that copy for the full rationale on why `visibility: hidden` is // narrowed to sub-composition hosts only — keep these two functions in @@ -943,10 +936,7 @@ export async function syncVideoFrameVisibility( // its computed opacity. video.style.setProperty("visibility", "hidden", "important"); video.style.setProperty("pointer-events", "none", "important"); - if (hasImg) { - if (video.hasAttribute(colorGradingSourceHiddenAttr)) img.style.opacity = "1"; - img.style.visibility = "visible"; - } + if (hasImg) img.style.visibility = "visible"; } else { // Inactive (or ancestor-hidden) video: hide both. Use visibility only // (never opacity) so we never clobber GSAP-controlled inline opacity. @@ -965,6 +955,5 @@ export async function syncVideoFrameVisibility( } }, activeVideoIds, - COLOR_GRADING_SOURCE_HIDDEN_ATTR, ); } diff --git a/packages/producer/tests/graded-video-transitions/meta.json b/packages/producer/tests/graded-video-transitions/meta.json new file mode 100644 index 0000000000..d7f0a720b8 --- /dev/null +++ b/packages/producer/tests/graded-video-transitions/meta.json @@ -0,0 +1,13 @@ +{ + "name": "graded-video-transitions", + "description": "Color-graded videos keep their animation: a red-to-blue opacity dissolve (2-4 s) and a green clip-path wipe from the left (4-6 s). Before the fix the dissolve exported as a hard cut.", + "tags": ["regression", "color-grading", "video"], + "minPsnr": 30, + "maxFrameFailures": 0, + "minAudioCorrelation": 0, + "maxAudioLagWindows": 1, + "renderConfig": { + "fps": 30, + "workers": 1 + } +} diff --git a/packages/producer/tests/graded-video-transitions/output/compiled.html b/packages/producer/tests/graded-video-transitions/output/compiled.html new file mode 100644 index 0000000000..367573e221 --- /dev/null +++ b/packages/producer/tests/graded-video-transitions/output/compiled.html @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7c4ccb303b2513887b06da4bcb3e4dc87ff8fbd975b04f2cb2ae76d6030aeb21 +size 76383 diff --git a/packages/producer/tests/graded-video-transitions/output/output.mp4 b/packages/producer/tests/graded-video-transitions/output/output.mp4 new file mode 100644 index 0000000000..76344aa0a2 --- /dev/null +++ b/packages/producer/tests/graded-video-transitions/output/output.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5a7f3457aeefc74732eb16dfb4dbb8984986d2cc5864475d849b5a149e797479 +size 14560 diff --git a/packages/producer/tests/graded-video-transitions/src/assets/blue.mp4 b/packages/producer/tests/graded-video-transitions/src/assets/blue.mp4 new file mode 100644 index 0000000000..31b7ec8874 --- /dev/null +++ b/packages/producer/tests/graded-video-transitions/src/assets/blue.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6d4f7010380fb66130b6b868ef6a436145f9794efc2b5eb2aee8e451dc70b7ea +size 5779 diff --git a/packages/producer/tests/graded-video-transitions/src/assets/green.mp4 b/packages/producer/tests/graded-video-transitions/src/assets/green.mp4 new file mode 100644 index 0000000000..c65016c229 --- /dev/null +++ b/packages/producer/tests/graded-video-transitions/src/assets/green.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e2ed7bf0fb6055e7611173cec80e8b574be56c3465b115a130eca18639f9ad8c +size 3665 diff --git a/packages/producer/tests/graded-video-transitions/src/assets/red.mp4 b/packages/producer/tests/graded-video-transitions/src/assets/red.mp4 new file mode 100644 index 0000000000..3a3dd3253b --- /dev/null +++ b/packages/producer/tests/graded-video-transitions/src/assets/red.mp4 @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:99056628b491e3f3d2d80c214c0c0c635df3258ec902dd9e331ba9018aa1cef3 +size 5779 diff --git a/packages/producer/tests/graded-video-transitions/src/index.html b/packages/producer/tests/graded-video-transitions/src/index.html new file mode 100644 index 0000000000..2e8a7e9e68 --- /dev/null +++ b/packages/producer/tests/graded-video-transitions/src/index.html @@ -0,0 +1,56 @@ + + + + + Graded video dissolve and clip-path wipe + + + + +
+ + + +
+ + + diff --git a/packages/producer/tests/shard-schedule.json b/packages/producer/tests/shard-schedule.json index 36e133d83a..f30f1983ca 100644 --- a/packages/producer/tests/shard-schedule.json +++ b/packages/producer/tests/shard-schedule.json @@ -23,6 +23,7 @@ "font-variant-numeric": 7, "gsap-letters-render-compat": 9, "gsap-redraw-start-and-overlap": 6, + "graded-video-transitions": 10, "hdr-hlg-audio-windows": 150, "hdr-hlg-regression": 125, "hdr-regression": 207, diff --git a/packages/studio/src/hooks/gsapRuntimePatch.ts b/packages/studio/src/hooks/gsapRuntimePatch.ts index 7768fb011e..454b29fcaf 100644 --- a/packages/studio/src/hooks/gsapRuntimePatch.ts +++ b/packages/studio/src/hooks/gsapRuntimePatch.ts @@ -250,9 +250,8 @@ function changeTouchesOpacity(change: RuntimeTweenChange): boolean { /** * A tween re-initialization (invalidate, or kill+recreate for keyframe-rebuild) - * captures opacity from the element's CURRENT inline style — for a color-graded - * source (hidden with `opacity: 0 !important`) or a mid-flight tween that's a - * runtime transient, not the authored value, and the capture makes it permanent. + * captures opacity from the element's CURRENT inline style — for a mid-flight + * tween that's a runtime transient, not the authored value, and the capture makes it permanent. * Restore the runtime's parse-time authored capture (data-hf-authored-opacity) * first; the re-seek after the patch re-renders the animated value anyway. * Duck-typed (no instanceof): the targets live in the preview iframe's realm. diff --git a/packages/studio/src/hooks/gsapRuntimeReaders.test.ts b/packages/studio/src/hooks/gsapRuntimeReaders.test.ts index 4e2f5485ea..2162a2ab8a 100644 --- a/packages/studio/src/hooks/gsapRuntimeReaders.test.ts +++ b/packages/studio/src/hooks/gsapRuntimeReaders.test.ts @@ -2,10 +2,7 @@ import { afterEach, describe, expect, it } from "vitest"; import { gsap } from "gsap"; import type { GsapAnimation } from "@hyperframes/core/gsap-parser"; -import { - COLOR_GRADING_SOURCE_HIDDEN_ATTR, - HF_COLOR_GRADING_CANVAS_ID_PREFIX, -} from "@hyperframes/core/color-grading"; +import { COLOR_GRADING_SOURCE_HIDDEN_ATTR } from "@hyperframes/core/color-grading"; import { readAllAnimatedProperties, readGsapProperty } from "./gsapRuntimeReaders"; /** @@ -68,47 +65,15 @@ describe("readAllAnimatedProperties group filter", () => { }); describe("color-grading opacity truth", () => { - function gradedElement(): HTMLElement { + it("reads a graded source's opacity from GSAP, as for any element", () => { const el = document.createElement("img"); el.id = "clip"; - el.setAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR, ""); - const canvas = document.createElement("canvas"); - canvas.id = `${HF_COLOR_GRADING_CANVAS_ID_PREFIX}clip`; - canvas.style.opacity = "0.98"; - document.body.append(el, canvas); - return el; - } - - it("resolves opacity from the grading canvas, not the runtime hide", () => { - const el = gradedElement(); - const iframe = fakeIframe(el, { gsapValues: { opacity: 0 } }); - const anim = { - id: "#clip-from-200-visual", - targetSelector: "#clip", - method: "from", - properties: { opacity: 0.5 }, - } as unknown as GsapAnimation; - - const result = readAllAnimatedProperties(iframe, "#clip", anim); - - expect(result.opacity).toBe(0.98); - }); - - it("readGsapProperty takes the same detour", () => { - const el = gradedElement(); - const iframe = fakeIframe(el, { gsapValues: { opacity: 0 } }); + el.setAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR, "true"); + document.body.appendChild(el); + const iframe = fakeIframe(el, { gsapValues: { opacity: 0.98 } }); expect(readGsapProperty(iframe, "#clip", "opacity")).toBe(0.98); }); - - it("reads GSAP directly when the source is not grading-hidden", () => { - const el = document.createElement("div"); - el.id = "clip"; - document.body.appendChild(el); - const iframe = fakeIframe(el, { gsapValues: { opacity: 0.3 } }); - - expect(readGsapProperty(iframe, "#clip", "opacity")).toBe(0.3); - }); }); describe("readAllAnimatedProperties with a sibling tween", () => { diff --git a/packages/studio/src/hooks/gsapRuntimeReaders.ts b/packages/studio/src/hooks/gsapRuntimeReaders.ts index 0bc7203ac4..ac1feeb437 100644 --- a/packages/studio/src/hooks/gsapRuntimeReaders.ts +++ b/packages/studio/src/hooks/gsapRuntimeReaders.ts @@ -3,34 +3,10 @@ */ import type { GsapAnimation } from "@hyperframes/core/gsap-parser"; import { classifyPropertyGroup, type PropertyGroupName } from "@hyperframes/core/gsap-parser"; -import { - COLOR_GRADING_SOURCE_HIDDEN_ATTR, - HF_COLOR_GRADING_CANVAS_ID_PREFIX, -} from "@hyperframes/core/color-grading"; -import { getIframeGsap, queryIframeElement, type IframeGsap } from "./gsapShared"; +import { getIframeGsap, queryIframeElement } from "./gsapShared"; import { gsapWritesChannels } from "./gsapRuntimeKeyframes"; import { roundTo3 } from "../utils/rounding"; -/** - * The element's live value for `prop` as GSAP drives it. Opacity on a - * color-grading-hidden source needs a detour: the runtime hides the source - * with inline `opacity: 0 !important`, so computed opacity is the hide, not - * the animated value. The grading canvas mirrors the source's effective - * opacity every frame, so it is the truth for that one property — reading the - * raw 0 here is what bakes `opacity: 0` into committed keyframes. - */ -function readLiveGsapValue(gsap: IframeGsap, el: Element, prop: string): number { - if (prop === "opacity" && el.getAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR) != null && el.id) { - const canvas = el.ownerDocument.getElementById(HF_COLOR_GRADING_CANVAS_ID_PREFIX + el.id); - const win = el.ownerDocument.defaultView; - if (canvas && win) { - const val = Number(win.getComputedStyle(canvas).opacity); - if (Number.isFinite(val)) return val; - } - } - return Number(gsap.getProperty(el, prop)); -} - export function readGsapProperty( iframe: HTMLIFrameElement | null, selector: string | null, @@ -42,7 +18,7 @@ export function readGsapProperty( const el = queryIframeElement(iframe, selector); if (!el) return null; try { - const val = readLiveGsapValue(gsap, el, prop); + const val = Number(gsap.getProperty(el, prop)); if (!Number.isFinite(val)) return null; return POSITION_PROPS.has(prop) ? Math.round(val) : roundTo3(val); } catch { @@ -89,7 +65,7 @@ export function readAllAnimatedProperties( const groupedPropKeys = new Set([...propKeys].filter(inGroup)); for (const prop of groupedPropKeys) { - const val = readLiveGsapValue(gsap, el, prop); + const val = Number(gsap.getProperty(el, prop)); if (Number.isFinite(val)) { result[prop] = POSITION_PROPS.has(prop) ? Math.round(val) : roundTo3(val); } diff --git a/packages/studio/src/utils/gsapSoftReload.ts b/packages/studio/src/utils/gsapSoftReload.ts index e43482abb4..5049b47170 100644 --- a/packages/studio/src/utils/gsapSoftReload.ts +++ b/packages/studio/src/utils/gsapSoftReload.ts @@ -1,4 +1,3 @@ -import { COLOR_GRADING_SOURCE_HIDDEN_ATTR } from "@hyperframes/core/color-grading"; import { parseGsapScript } from "@hyperframes/parsers/gsap-parser"; import { GSAP_BASE_URL_META, motionPathPluginUrl } from "@hyperframes/core/gsap-cdn"; import { findAuthoredElement } from "./authoredSource"; @@ -464,24 +463,13 @@ export function applySoftReload( s.cssText = css; restoreAuthoredStyle(s, findAuthoredStyle(el), targets.get(el)!); // The restored cssText carries RUNTIME opacity, not authored opacity: - // a mid-flight tween's interpolated value, or the color-grading hide - // (`opacity: 0 !important`). The re-run script's tweens re-initialize + // a mid-flight tween's interpolated value. The re-run script's tweens re-initialize // against it — a from() captures it as its END, a to() as its START — // turning the transient into the tween's permanent bound (dimmed or // invisible elements). Put the AUTHORED inline opacity back; the seek // below re-renders the correct animated value either way. const authored = readAuthoredOpacity(el); - if (authored !== null) { - applyAuthoredInlineOpacity(s, authored); - } else if ( - el.hasAttribute(COLOR_GRADING_SOURCE_HIDDEN_ATTR) && - s.getPropertyValue("opacity") === "0" && - s.getPropertyPriority("opacity") === "important" - ) { - // Authored value unknown, but this is definitely the grading hide — - // never let a from() capture 0; fall back to the CSS cascade. - s.removeProperty("opacity"); - } + if (authored !== null) applyAuthoredInlineOpacity(s, authored); } } forgetLiveSets(targets); diff --git a/packages/studio/tests/e2e/fixtures/graded-video-transitions/assets/blue.mp4 b/packages/studio/tests/e2e/fixtures/graded-video-transitions/assets/blue.mp4 new file mode 100644 index 0000000000..56dadef38e Binary files /dev/null and b/packages/studio/tests/e2e/fixtures/graded-video-transitions/assets/blue.mp4 differ diff --git a/packages/studio/tests/e2e/fixtures/graded-video-transitions/assets/green.mp4 b/packages/studio/tests/e2e/fixtures/graded-video-transitions/assets/green.mp4 new file mode 100644 index 0000000000..4bf99d2c7f Binary files /dev/null and b/packages/studio/tests/e2e/fixtures/graded-video-transitions/assets/green.mp4 differ diff --git a/packages/studio/tests/e2e/fixtures/graded-video-transitions/assets/red.mp4 b/packages/studio/tests/e2e/fixtures/graded-video-transitions/assets/red.mp4 new file mode 100644 index 0000000000..459c0f2fdf Binary files /dev/null and b/packages/studio/tests/e2e/fixtures/graded-video-transitions/assets/red.mp4 differ diff --git a/packages/studio/tests/e2e/fixtures/graded-video-transitions/hyperframes.json b/packages/studio/tests/e2e/fixtures/graded-video-transitions/hyperframes.json new file mode 100644 index 0000000000..c4816b5eaa --- /dev/null +++ b/packages/studio/tests/e2e/fixtures/graded-video-transitions/hyperframes.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://hyperframes.heygen.com/schema/hyperframes.json", + "paths": { + "blocks": "compositions", + "components": "compositions/components", + "assets": "assets" + } +} diff --git a/packages/studio/tests/e2e/fixtures/graded-video-transitions/index.html b/packages/studio/tests/e2e/fixtures/graded-video-transitions/index.html new file mode 100644 index 0000000000..144a16a91f --- /dev/null +++ b/packages/studio/tests/e2e/fixtures/graded-video-transitions/index.html @@ -0,0 +1,93 @@ + + + + + Graded video dissolve and clip-path wipe + + + + +
+ + + +
+ + + diff --git a/packages/studio/tests/e2e/graded-video-transitions.mjs b/packages/studio/tests/e2e/graded-video-transitions.mjs new file mode 100644 index 0000000000..a2e9cb850b --- /dev/null +++ b/packages/studio/tests/e2e/graded-video-transitions.mjs @@ -0,0 +1,117 @@ +#!/usr/bin/env node +// Color-graded videos keep their animation in the preview: a red-to-blue opacity dissolve (2-4 s) and a +// green clip-path wipe from the left (4-6 s). Grading once froze the dissolve, and its hide once blacked it out. +// A click on a graded clip still picks it. +import { mkdirSync, writeFileSync } from "node:fs"; +import { launchStudioChrome } from "./chrome-executable.mjs"; + +const STUDIO_URL = process.env.STUDIO_URL; +const OUT_DIR = process.env.OUT_DIR; + +if (!STUDIO_URL) { + console.error("STUDIO_URL is required and must point at the graded-video-transitions fixture"); + process.exit(2); +} +if (OUT_DIR) mkdirSync(OUT_DIR, { recursive: true }); + +// Share of red in red + blue at the dissolve's quarter points, and which colour leads either side of the wipe. +const DISSOLVE = [ + { time: 2.5, red: 0.75 }, + { time: 3, red: 0.5 }, + { time: 3.5, red: 0.25 }, +]; +const WIPE = { time: 5, left: "green", right: "blue" }; +const TOLERANCE = 0.12; + +const { browser } = await launchStudioChrome(); +const failures = []; +const evidence = {}; +try { + const page = await browser.newPage(); + await page.setViewport({ width: 1440, height: 900 }); + await page.goto(STUDIO_URL, { waitUntil: "domcontentloaded" }); + await page.waitForSelector('button[aria-label="Play"]:not([disabled])', { timeout: 60_000 }); + const handle = await page.waitForFunction( + () => document.querySelector("hyperframes-player")?.shadowRoot?.querySelector("iframe"), + { timeout: 60_000 }, + ); + const iframe = handle.asElement(); + const frame = await iframe.contentFrame(); + await frame.waitForFunction(() => window.__playerReady, { timeout: 60_000 }); + + /** RGB at fractions of the film's width and height, read from a screenshot of the preview at `time`. */ + const sample = async (name, time, points) => { + await frame.evaluate((t) => window.__player.seek(t, { settled: true }), time); + await frame.evaluate( + () => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))), + ); + const film = await frame.evaluate(() => { + const r = document.getElementById("film").getBoundingClientRect(); + return { x: r.x, y: r.y, width: r.width, height: r.height }; + }); + const png = await iframe.screenshot({ encoding: "base64" }); + if (OUT_DIR) writeFileSync(`${OUT_DIR}/${name}.png`, Buffer.from(png, "base64")); + const box = await iframe.boundingBox(); + const scale = box.width / (await frame.evaluate(() => window.innerWidth)); + return page.evaluate( + async (data, film, scale, points) => { + const img = new Image(); + img.src = `data:image/png;base64,${data}`; + await img.decode(); + const canvas = new OffscreenCanvas(img.width, img.height); + const ctx = canvas.getContext("2d"); + ctx.drawImage(img, 0, 0); + return points.map(([fx, fy]) => { + const x = Math.round((film.x + film.width * fx) * scale); + const y = Math.round((film.y + film.height * fy) * scale); + const [r, g, b] = ctx.getImageData(x, y, 1, 1).data; + return { r, g, b }; + }); + }, + png, + film, + scale, + points, + ); + }; + const dark = ({ r, g, b }) => Math.max(r, g, b) < 40; + const leads = ({ r, g, b }) => (g > r && g > b ? "green" : b > r ? "blue" : "red"); + + for (const { time, red } of DISSOLVE) { + const [centre] = await sample(`dissolve-${time}`, time, [[0.5, 0.5]]); + const share = centre.r / Math.max(1, centre.r + centre.b); + evidence[`dissolve-${time}`] = { ...centre, share: Number(share.toFixed(3)) }; + if (dark(centre)) failures.push(`dissolve at ${time} s: black (${JSON.stringify(centre)})`); + else if (Math.abs(share - red) > TOLERANCE) + failures.push(`dissolve at ${time} s: red share ${share.toFixed(2)}, expected ${red}`); + } + + // A graded clip stays under the pointer, so a click in the preview still picks it. + await frame.evaluate(() => window.__player.seek(1, { settled: true })); + const picked = await frame.evaluate(() => { + const r = document.getElementById("film").getBoundingClientRect(); + return document.elementsFromPoint(r.x + r.width / 2, r.y + r.height / 2).map((el) => el.id); + }); + evidence.pickAt1 = picked; + if (!picked.includes("a")) + failures.push(`at 1 s the graded clip is not under the pointer: ${picked}`); + + const [left, right] = await sample("wipe-5", WIPE.time, [ + [0.25, 0.5], + [0.75, 0.5], + ]); + evidence.wipe = { left, right }; + for (const [side, pixel, colour] of [ + ["left", left, WIPE.left], + ["right", right, WIPE.right], + ]) { + if (dark(pixel)) failures.push(`wipe at ${WIPE.time} s, ${side}: black`); + else if (leads(pixel) !== colour) + failures.push(`wipe at ${WIPE.time} s, ${side}: ${leads(pixel)} leads, expected ${colour}`); + } +} finally { + await browser.close(); +} + +console.log(JSON.stringify({ evidence, failures }, null, 2)); +if (failures.length > 0) process.exit(1);