Skip to content

Video as code: canvas scenes rendered with WebCodecs in headless Chrome, a review viewer, and agent skills that build the video from your references.

3.5 KB
Raw Blame
<!doctype html><html><head><meta charset="utf-8"><title>motlab transition</title>
<style>html,body{margin:0;background:#000} iframe{position:fixed;left:-99999px;top:0;border:0;visibility:hidden}</style></head>
<body><canvas id="c"></canvas>
<script type="module">
// The frames where one scene turns into the next, rendered like any scene page (served at /__motlab/transition.html).
// Both scene pages are loaded in iframes. Frame k shows frame (offset * fps + k) of `from` and frame k of `to`,
// mixed by src/page/transitions.js, the same code the viewer uses.
// Query: from, to (page paths from the served root), offset (seconds into `from`), frames, fps, type (a built-in name or
// a .glsl path from the served root), ease, params (JSON), scale. With `layer` (a module's path) instead of `type`, the
// scenes do not overlap: the first frames are the end of `from`, the rest the start of `to`, with the layer drawn on top.
import { createMixer, layerFrames, layerProgress, loadLayer, loadTransition, transitionProgress } from '/__motlab/transitions.js';
const q = new URLSearchParams(location.search), fps = +q.get('fps'), n = +q.get('frames'), offset = +q.get('offset');
const type = q.get('type'), easing = q.get('ease') || 'inOutCubic', params = JSON.parse(q.get('params') || '{}'), scale = q.get('scale') || '1';
const cv = document.getElementById('c'), ctx = cv.getContext('2d');
const layerPath = q.get('layer'), split = layerFrames(n);
let A, B, mix, def, layer;
const readText = async path => {
  const r = await fetch('/' + path.split('/').map(encodeURIComponent).join('/'));
  if (!r.ok) throw new Error(`transition ${path} did not load (HTTP ${r.status})`);
  return r.text();
};

const open = path => new Promise((ok, fail) => {
  const f = document.createElement('iframe'); f.src = `/${path}?scale=${scale}`;
  f.onload = async () => {
    try {
      const w = f.contentWindow, c = w.document.getElementById('c');
      if (!c || typeof w.renderAt !== 'function') throw new Error(`${path} does not keep the page contract`);
      await w.ready; ok({ w, c });
    } catch (e) { fail(e); }
  };
  f.onerror = () => fail(new Error(`${path} did not load`));
  document.body.append(f);
});

window.TOTAL = n / fps;
window.ready = (async () => {
  if (layerPath) layer = await loadLayer('/' + layerPath.split('/').map(encodeURIComponent).join('/'), layerPath);
  else { def = await loadTransition(type, readText); transitionProgress(0, n, easing); }   // a wrong name fails here, before any frame
  [A, B] = await Promise.all([open(q.get('from')), open(q.get('to'))]);
  cv.width = A.c.width; cv.height = A.c.height;
  if (!layer) { mix = createMixer(); mix.check(def); }
  return true;
})();
window.renderAt = async t => {
  await window.ready;
  const k = Math.min(n - 1, Math.max(0, Math.round(t * fps)));
  if (layer) {   // the scene on this side of the cut, then the layer over it in the page's own pixels
    const side = k < split.before ? A : B;
    await (side === A ? A.w.renderAt(offset + k / fps) : B.w.renderAt((k - split.before) / fps));
    ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.drawImage(side.c, 0, 0, cv.width, cv.height);
    const s = Number(scale) || 1; ctx.save(); ctx.setTransform(s, 0, 0, s, 0, 0);
    try { await layer.draw(ctx, layerProgress(k, n), cv.width / s, cv.height / s); } finally { ctx.restore(); }
    return;
  }
  await A.w.renderAt(offset + k / fps); await B.w.renderAt(k / fps);
  ctx.drawImage(mix.draw(def, A.c, B.c, transitionProgress(k, n, easing), params), 0, 0, cv.width, cv.height);
};
</script></body></html>