diff --git a/packages/engine/src/services/videoFrameInjector.ts b/packages/engine/src/services/videoFrameInjector.ts index 758a98f1a5..3eb62a159a 100644 --- a/packages/engine/src/services/videoFrameInjector.ts +++ b/packages/engine/src/services/videoFrameInjector.ts @@ -262,6 +262,22 @@ export async function queryElementStacking( // positioned ancestor with a z-index. CSS z-index only applies to // positioned elements; video elements inside positioned wrappers // inherit the wrapper's stacking context. + // + // ## Supported subset + // + // This implementation looks for explicit `z-index` on positioned + // (non-static) ancestors. It does NOT detect the CSS stacking contexts + // created implicitly by other properties — including `opacity < 1`, + // `transform`, `filter`, `will-change`, `isolation: isolate`, and + // `mix-blend-mode`. GSAP routinely sets `transform` on wrappers, which + // creates an implicit stacking context with auto z-index; an HDR video + // inside such a wrapper with no explicit z-index will return the + // wrapper-of-the-wrapper's z-index here, potentially reordering layers + // incorrectly relative to sibling stacking contexts. + // + // The workaround is to set explicit `z-index` on the positioned wrapper + // when you want it treated as a compositing layer root. This matches + // what compositions need to do anyway for deterministic z-ordering. function getEffectiveZIndex(node: Element): number { let current: Element | null = node; while (current) { diff --git a/packages/engine/src/utils/layerCompositor.test.ts b/packages/engine/src/utils/layerCompositor.test.ts index 9e8bc5b4a7..013eb38a43 100644 --- a/packages/engine/src/utils/layerCompositor.test.ts +++ b/packages/engine/src/utils/layerCompositor.test.ts @@ -3,7 +3,21 @@ import { groupIntoLayers } from "./layerCompositor.js"; import type { ElementStackingInfo } from "../services/videoFrameInjector.js"; function makeEl(id: string, zIndex: number, isHdr: boolean): ElementStackingInfo { - return { id, zIndex, x: 0, y: 0, width: 1920, height: 1080, opacity: 1, visible: true, isHdr }; + return { + id, + zIndex, + x: 0, + y: 0, + width: 1920, + height: 1080, + layoutWidth: 1920, + layoutHeight: 1080, + opacity: 1, + visible: true, + isHdr, + transform: "none", + borderRadius: [0, 0, 0, 0], + }; } describe("groupIntoLayers", () => { @@ -97,4 +111,31 @@ describe("groupIntoLayers", () => { expect(layers[0]!.elementIds).toEqual(["bg", "hidden-sdr"]); } }); + + it("returns an empty array for empty input", () => { + expect(groupIntoLayers([])).toEqual([]); + }); + + it("handles negative z-index (valid CSS back layers)", () => { + const elements = [makeEl("fg", 1, false), makeEl("bg", -5, false)]; + const layers = groupIntoLayers(elements); + expect(layers).toHaveLength(1); + expect(layers[0]!.type).toBe("dom"); + if (layers[0]!.type === "dom") { + expect(layers[0]!.elementIds).toEqual(["bg", "fg"]); + } + }); + + it("preserves input order for equal z-index (stable tie-break)", () => { + const elements = [ + makeEl("first", 0, false), + makeEl("second", 0, false), + makeEl("third", 0, false), + ]; + const layers = groupIntoLayers(elements); + expect(layers).toHaveLength(1); + if (layers[0]!.type === "dom") { + expect(layers[0]!.elementIds).toEqual(["first", "second", "third"]); + } + }); }); diff --git a/packages/engine/src/utils/layerCompositor.ts b/packages/engine/src/utils/layerCompositor.ts index 670e7e605f..550d207ae4 100644 --- a/packages/engine/src/utils/layerCompositor.ts +++ b/packages/engine/src/utils/layerCompositor.ts @@ -15,11 +15,22 @@ export type CompositeLayer = | { type: "hdr"; element: ElementStackingInfo }; /** - * Group z-sorted elements into composite layers. Adjacent DOM elements - * merge into a single layer. Each HDR video is its own layer. + * Group z-sorted elements into composite layers. Adjacent DOM elements merge + * into a single layer; each HDR video/image is its own layer. * - * Elements are sorted by zIndex ascending (back to front) before grouping. - * Invisible elements are filtered out. + * Elements are sorted by \`zIndex\` ascending (back to front). Ties fall + * through to V8's stable sort, which preserves \`querySelectorAll\` DOM order — + * this is the same order Chrome uses for equal-z elements in a stacking + * context, so the blit order matches what the user sees in-browser. + * + * The DOM merge doesn't lose information: DOM layers are rendered via a + * full-page screenshot with non-layer elements hidden, so within-layer + * z-order is handled by Chrome itself. + * + * Invisible elements ARE included (video elements are hidden by the frame + * injector, but their injected \`\` replacements are visible — they must + * stay in the correct z-ordered layer so sibling layers' DOM screenshots + * hide them). */ export function groupIntoLayers(elements: ElementStackingInfo[]): CompositeLayer[] { // Include ALL elements regardless of visibility. Video elements are hidden by