import { CAPTURE_CANVAS_SELECTOR } from "./htmlInCanvas"; type UaWindow = { navigator: { userAgent: string } }; /** Major Chrome version from the user agent (HeadlessChrome included), or null when unknown. */ export function chromeMajor(win: UaWindow = window): number | null { const m = /Chrome\/(\d+)/.exec(win.navigator.userAgent); return m?.[1] ? Number(m[1]) : null; } export interface CaptureDefect { code: string; reason: string; } export interface DefectEnv { isAccelerated: (canvas: HTMLCanvasElement) => boolean; /** Result of `scanBackfaceDefect`, refreshed whenever the scene is rescanned. */ backface: boolean; } // Every defect blocks on every Chrome version: render and preview browsers can differ. interface Defect { code: string; reason: string; detect: (root: HTMLElement, env: DefectEnv) => boolean; } const hasHidden3dFace = (_root: HTMLElement, env: DefectEnv): boolean => env.backface; // The whole document: a render's shader-transition canvas sits in
, preview's in the root. const hasAcceleratedCanvas = (root: HTMLElement, env: DefectEnv): boolean => Array.from(root.ownerDocument.querySelectorAll("canvas")).some(env.isAccelerated); const IDENTITY_TRANSFORM = /^matrix\(\s*1,\s*0,\s*0,\s*1,\s*0,\s*0\s*\)$|^matrix3d\(\s*1,\s*0,\s*0,\s*0,\s*0,\s*1,\s*0,\s*0,\s*0,\s*0,\s*1,\s*0,\s*0,\s*0,\s*0,\s*1\s*\)$/; const moves = (value: string | undefined, identity: number): boolean => !!value && value !== "none" && !value.split(/\s+/).every((p) => parseFloat(p) === identity); // `scale`, `rotate` and `translate` transform the root just as `transform` does. const hasRootTransform = (root: HTMLElement): boolean => { const cs = getComputedStyle(root); const t = cs.transform.trim(); if (moves(cs.scale, 1) || moves(cs.rotate, 0) || moves(cs.translate, 0)) return true; return t !== "none" && t !== "" && !IDENTITY_TRANSFORM.test(t); }; const hasRootBlend = (root: HTMLElement): boolean => { const mode = getComputedStyle(root).mixBlendMode; return !!mode && mode !== "normal"; }; const hasRootOpacity = (root: HTMLElement): boolean => Number.parseFloat(getComputedStyle(root).opacity) < 1; // Moving the root tears these down and starts them again: a frame reloads, a custom element reconnects. const hasReinitialised = (root: HTMLElement): boolean => root.querySelector("iframe, object, embed") !== null || Array.from(root.querySelectorAll("*")).some( (el) => el.localName.includes("-") && customElements.get(el.localName) !== undefined, ); const DEFECTS: readonly Defect[] = [ { code: "nested-capture-canvas", reason: "the composition already uses html-in-canvas", detect: (root) => root.querySelector(CAPTURE_CANVAS_SELECTOR) !== null, }, { // Chrome <=156 captures the canvas frozen at its pre-wrap frame; 157 fixed it. code: "accelerated-canvas", reason: "the composition draws a WebGL/WebGPU canvas, which html-in-canvas captures frozen", detect: hasAcceleratedCanvas, }, { code: "backface-visibility", reason: "backface-visibility: hidden inside a 3D transform is drawn mirrored by html-in-canvas", detect: hasHidden3dFace, }, { code: "iframe-in-root", reason: "the composition contains an