Skip to content

Write HTML. Render video. Built for agents.

3.5 KB
Raw Blame
import { refreshSvgSelectorAliases } from "../compiler/svgSelectorAliases";
import { recordStartupError } from "./diagnostics";
import {
  initSandboxRuntimeModular,
  installAuthoredMediaCapture,
  installFlatGsapTransforms,
} from "./init";
import { installAuthoredOpacityCapture } from "./colorGrading";
import { deferMediaUntilDue } from "./preloadMedia";
import { hideTimedClipsUntilFirstPass } from "./timedClipHide";
import { fitTextFontSize } from "../text/fitTextFontSize";
import { pretext } from "../text/pretext";
import { assetUrl } from "./assetUrl";
import { getVariables } from "./getVariables";
import { clearRuntimeData, registerRuntimeDataHandler, setRuntimeData } from "./runtimeData";
import { runScriptsAfterFonts } from "./afterFonts";
import { AFTER_FONTS_SCRIPTS } from "../compiler/scriptRuns";
import { hasFrameSources, registerFrameSource } from "./frameSources";
import { createFilmBridge } from "./filmBridge";
import { trackAcceleratedCanvases } from "./captureMotionBlur";

type HyperframeWindow = Window & {
  __hyperframeRuntimeBootstrapped?: boolean;
  __hyperframes?: {
    assetUrl: typeof assetUrl;
    fitTextFontSize: typeof fitTextFontSize;
    getVariables: typeof getVariables;
    pretext: typeof pretext;
    registerRuntimeDataHandler: typeof registerRuntimeDataHandler;
    setRuntimeData: typeof setRuntimeData;
    clearRuntimeData: typeof clearRuntimeData;
    registerFrameSource: typeof registerFrameSource;
    createFilmBridge: typeof createFilmBridge;
  };
};

// Inline composition scripts can run before DOMContentLoaded.
// Ensure timeline registry exists at script evaluation time.
(window as HyperframeWindow).__timelines = (window as HyperframeWindow).__timelines || {};

// Stamp color-graded elements with their authored inline opacity BEFORE the
// composition's animation scripts (and the grading hide) mutate it — must run
// at script evaluation time, while the document is still parsing.
installAuthoredOpacityCapture();
installAuthoredMediaCapture();
installFlatGsapTransforms();
trackAcceleratedCanvases();
window.__hfHasFrameSources = hasFrameSources;

hideTimedClipsUntilFirstPass();
deferMediaUntilDue();

// Expose runtime helpers immediately so composition scripts can use them
// before DOMContentLoaded (font sizing runs during script evaluation, and
// getVariables is read by composition setup before the timeline is built).
(window as HyperframeWindow).__hyperframes = {
  assetUrl,
  fitTextFontSize,
  getVariables,
  pretext,
  registerRuntimeDataHandler,
  setRuntimeData,
  clearRuntimeData,
  registerFrameSource,
  createFilmBridge,
};

function bootstrapHyperframeRuntime(): void {
  const win = window as HyperframeWindow;
  if (win.__hyperframeRuntimeBootstrapped) {
    return;
  }
  win.__hyperframeRuntimeBootstrapped = true;
  initSandboxRuntimeModular();
}

// Compiled composition scripts wait for web fonts, so what they measure matches every run.
function startAfterCompositionScripts(): void {
  try {
    refreshSvgSelectorAliases();
    const deferred = Array.from(document.querySelectorAll(AFTER_FONTS_SCRIPTS));
    if (deferred.length === 0) bootstrapHyperframeRuntime();
    else {
      void runScriptsAfterFonts(deferred, bootstrapHyperframeRuntime).catch((err: unknown) => {
        recordStartupError(err);
        reportError(err);
      });
    }
  } catch (err) {
    recordStartupError(err);
    throw err;
  }
}

if (document.readyState === "loading") {
  document.addEventListener("DOMContentLoaded", startAfterCompositionScripts, { once: true });
} else {
  startAfterCompositionScripts();
}