Skip to content

Write HTML. Render video. Built for agents.

fix(core): pair a staged scene copy's video with its own render frame (#4791)

Leonel Rivas7b0ec6970400
diff --git a/packages/core/src/runtime/colorGrading.test.ts b/packages/core/src/runtime/colorGrading.test.ts
index bfde533e59..c0a4215572 100644
--- a/packages/core/src/runtime/colorGrading.test.ts
+++ b/packages/core/src/runtime/colorGrading.test.ts
@@ -781,6 +781,47 @@ describe("createColorGradingRuntime", () => {
expect(canvas.style.opacity).toBe("0.75");
});
+ it("keeps a staged scene copy's canvas on its own render frame (#3994)", async () => {
+ // Page-side shader transitions clone the scene, ids included, into a staging
+ // layer. When both videos paired with the live frame, their canvases traded
+ // places after it on every mutation and the render stalled.
+ function makeRenderFrame(): HTMLImageElement {
+ const frame = document.createElement("img");
+ frame.id = "__render_frame_hero-video__";
+ frame.className = "__render_frame__";
+ Object.defineProperty(frame, "complete", { value: true, configurable: true });
+ Object.defineProperty(frame, "naturalWidth", { value: 640, configurable: true });
+ Object.defineProperty(frame, "naturalHeight", { value: 360, configurable: true });
+ return frame;
+ }
+ const live = document.createElement("div");
+ live.append(makeDrawableVideo(), makeRenderFrame());
+ document.body.appendChild(live);
+ runtime = createColorGradingRuntime();
+
+ let canvasMoves = 0;
+ const insertBefore = Node.prototype.insertBefore;
+ const spy = vi.spyOn(Node.prototype, "insertBefore").mockImplementation(function <
+ T extends Node,
+ >(this: Node, node: T, child: Node | null): T {
+ // Stop moving canvases after a bound, so the loop cannot hang the test.
+ if (node instanceof HTMLCanvasElement && ++canvasMoves > 100) return node;
+ return insertBefore.call(this, node, child) as T;
+ });
+ const staged = document.createElement("div");
+ staged.append(makeDrawableVideo(), makeRenderFrame());
+ document.body.appendChild(staged);
+ for (let i = 0; i < 5; i++) await new Promise((resolve) => window.setTimeout(resolve, 0));
+ runtime.redraw();
+ spy.mockRestore();
+
+ expect(canvasMoves).toBeLessThan(10);
+ for (const scene of [live, staged]) {
+ const frame = scene.querySelector("img.__render_frame__");
+ expect(frame?.nextElementSibling?.hasAttribute("data-hf-color-grading-canvas")).toBe(true);
+ }
+ });
+
it("hides the canvas when an ancestor clip goes out of its visibility window", () => {
// A graded <img> inside a timed sub-composition carries no data-start of its
// own, so nothing tells grading the clip left the screen — the canvas has to
diff --git a/packages/core/src/runtime/renderFrameSibling.test.ts b/packages/core/src/runtime/renderFrameSibling.test.ts
index e84fe0d67d..e5a39c4d00 100644
--- a/packages/core/src/runtime/renderFrameSibling.test.ts
+++ b/packages/core/src/runtime/renderFrameSibling.test.ts
@@ -67,6 +67,37 @@ describe("findInjectedRenderFrame", () => {
);
});
+ it("resolves a staged copy of a scene to the copy's own frame (#3994)", () => {
+ // Page-side shader transitions clone the scene, ids included, into a staging
+ // layer. The copy's video must pair with the copy's frame: pairing it with the
+ // live one sent two color-grading canvases fighting over one <img>.
+ const scene =
+ `<div class="scene"><video id="clip" ${MEDIA_RENDER_ID_ATTR}="clip"></video>` +
+ `<img id="__render_frame_clip__" class="__render_frame__"></div>`;
+ document.body.innerHTML = scene + `<div class="staging">${scene}</div>`;
+
+ const [liveVideo, stagedVideo] = Array.from(document.querySelectorAll("video"));
+ const [liveFrame, stagedFrame] = Array.from(document.querySelectorAll("img"));
+ expect(findInjectedRenderFrame(liveVideo!)).toBe(liveFrame);
+ expect(findInjectedRenderFrame(stagedVideo!)).toBe(stagedFrame);
+ });
+
+ it("finds its frame past a node inserted between them", () => {
+ document.body.innerHTML =
+ '<video id="solo"></video><canvas></canvas><img id="__render_frame_solo__" class="__render_frame__">';
+ expect(findInjectedRenderFrame(document.querySelector("video")!)).toBe(
+ document.querySelector("img"),
+ );
+ });
+
+ it("still resolves a frame that is not a sibling, by id", () => {
+ document.body.innerHTML =
+ '<div><video id="solo"></video></div><img id="__render_frame_solo__" class="__render_frame__">';
+ expect(findInjectedRenderFrame(document.querySelector("video")!)).toBe(
+ document.querySelector("img"),
+ );
+ });
+
it("returns null in preview, where no sibling exists", () => {
document.body.innerHTML = '<video id="solo"></video>';
expect(findInjectedRenderFrame(document.querySelector("video")!)).toBeNull();
diff --git a/packages/core/src/runtime/renderFrameSibling.ts b/packages/core/src/runtime/renderFrameSibling.ts
index a15b44f2be..365954591b 100644
--- a/packages/core/src/runtime/renderFrameSibling.ts
+++ b/packages/core/src/runtime/renderFrameSibling.ts
@@ -50,11 +50,14 @@ export function renderFrameElementId(media: Element): string | null {
/**
* The injected render-frame `<img>` for a media element, or null in preview
- * (where the producer never creates one).
+ * (where the producer never creates one). Siblings first: a staged scene copy repeats the id.
*/
export function findInjectedRenderFrame(media: Element): HTMLImageElement | null {
const frameId = renderFrameElementId(media);
- if (!frameId) return null;
- const frame = document.getElementById(frameId);
+ const byId = frameId ? document.getElementById(frameId) : null;
+ if (!byId) return null;
+ let frame: Element | null = media.nextElementSibling;
+ while (frame && frame.id !== frameId) frame = frame.nextElementSibling;
+ frame ??= byId;
return isImageElement(frame) ? frame : null;
}