Skip to content
← Public packages

@kentcdodds/kody-connect-talk

Kent C. Dodds's Cloudflare Connect 2026 talk: why your agents' stuff needs a common house, and how Kody builds one on Cloudflare.

FollowForkOpen app

Files

page.js

355 lines · 12.6 KB · JavaScript
// Inlined into the generated composition before the per-slide animation code.
// `tl` is the one paused deck timeline; `H` holds the shared motion vocabulary
// so every slide moves the same way. Positions are absolute deck seconds.
const tl = gsap.timeline({ paused: true });
const EASE = "expo.out";
const H = {
  // arrive: rise out of a soft blur
  in(sel, t, o = {}) {
    tl.fromTo(
      sel,
      { opacity: 0, x: o.x ?? 0, y: o.y ?? 28, scale: o.scale ?? 1, filter: "blur(10px)" },
      {
        opacity: 1,
        x: 0,
        y: 0,
        scale: 1,
        filter: "blur(0px)",
        duration: o.d ?? H_DEFAULTS.in,
        ease: o.ease ?? EASE,
        stagger: o.stagger ?? 0,
      },
      t,
    );
  },
  fade(sel, t, o = {}) {
    tl.fromTo(sel, { opacity: 0 }, { opacity: o.to ?? 1, duration: o.d ?? H_DEFAULTS.fade, ease: "power1.out", stagger: o.stagger ?? 0 }, t);
  },
  dim(sel, t, o = {}) {
    tl.to(sel, { opacity: o.to ?? 0.28, filter: o.blur ? `blur(${o.blur}px)` : "none", duration: o.d ?? H_DEFAULTS.dim, ease: "power2.out" }, t);
  },
  type(sel, t, chars, o = {}) {
    tl.fromTo(
      sel,
      { width: "0px" },
      { width: "auto", duration: o.d ?? Math.min(H_DEFAULTS.typeMax, chars * H_DEFAULTS.typePerChar), ease: `steps(${chars})` },
      t,
    );
  },
  to(sel, t, vars) {
    tl.to(sel, { duration: H_DEFAULTS.tween, ...vars }, t);
  },
  fromTo(sel, t, from, to) {
    tl.fromTo(sel, from, { duration: H_DEFAULTS.tween, ...to }, t);
  },

  // A step's view of H: times are offsets from the step's start, and targets
  // are looked up inside the slide only (so a reused class elsewhere can never
  // change a stagger). deck/timing.mjs measures steps with the same rules.
  at(base, sceneId) {
    const scene = document.getElementById(`s-${sceneId}`);
    const q = (sel) => (Array.isArray(sel) ? sel : [sel]).flatMap((one) => Array.from(scene.querySelectorAll(one)));
    return {
      in: (sel, t, o) => H.in(q(sel), base + t, o),
      fade: (sel, t, o) => H.fade(q(sel), base + t, o),
      dim: (sel, t, o) => H.dim(q(sel), base + t, o),
      type: (sel, t, chars, o) => H.type(q(sel), base + t, chars, o),
      to: (sel, t, vars) => H.to(q(sel), base + t, vars),
      fromTo: (sel, t, from, to) => H.fromTo(q(sel), base + t, from, to),
    };
  },

  // leave: lift away and soften
  out(scene, t) {
    tl.to(`#s-${scene} .inner`, { opacity: 0, y: -20, filter: "blur(8px)", duration: 0.45, ease: "power2.in" }, t);
  },
  // the topic hue grows from a point until it fills the frame
  wipeIn(scene, t, x = 960, y = 540) {
    tl.fromTo(`#s-${scene} .wipe`, { opacity: 0 }, { opacity: 1, duration: 0.01 }, t);
    tl.fromTo(
      `#s-${scene} .wipe`,
      { clipPath: `circle(0px at ${x}px ${y}px)` },
      { clipPath: `circle(2300px at ${x}px ${y}px)`, duration: 0.8, ease: "power3.in" },
      t,
    );
  },
  // ...and collapses into the topic badge on the next slide
  wipeOut(scene, t, x = 960, y = 540) {
    tl.fromTo(`#s-${scene} .wipe`, { opacity: 1 }, { opacity: 0, duration: 0.01 }, t + 0.89);
    tl.fromTo(
      `#s-${scene} .wipe`,
      { clipPath: `circle(2300px at ${x}px ${y}px)` },
      { clipPath: `circle(0px at ${x}px ${y}px)`, duration: 0.9, ease: "expo.inOut" },
      t,
    );
  },
};

// Ambient motion runs on its own clock, outside the seekable deck timeline, so
// a slide at rest still breathes. It only touches inner elements the deck
// timeline never animates (`.amb-*`, `.wire`, `.caret`).
function startAmbient() {
  if (matchMedia("(prefers-reduced-motion: reduce)").matches) return;
  gsap.utils.toArray(".amb-float").forEach((el, i) => {
    gsap.fromTo(el, { y: 0 }, { y: -12, duration: 2.6 + (i % 5) * 0.4, delay: (i % 7) * 0.37, yoyo: true, repeat: -1, ease: "sine.inOut" });
  });
  gsap.utils.toArray(".amb-sway").forEach((el, i) => {
    gsap.fromTo(
      el,
      { rotation: -1.6, y: 0 },
      { rotation: 1.6, y: -10, duration: 3.4 + (i % 3) * 0.5, yoyo: true, repeat: -1, ease: "sine.inOut", transformOrigin: "50% 100%" },
    );
  });
  gsap.utils.toArray(".wire").forEach((el) => {
    gsap.to(el, { strokeDashoffset: -32, duration: 0.8, repeat: -1, ease: "none" });
  });
  gsap.utils.toArray(".amb-pulse").forEach((el, i) => {
    gsap.fromTo(el, { opacity: 0.45 }, { opacity: 1, duration: 1.3, delay: i * 0.25, yoyo: true, repeat: -1, ease: "sine.inOut" });
  });
  gsap.utils.toArray(".caret").forEach((el) => {
    gsap.fromTo(el, { opacity: 1 }, { opacity: 0, duration: 0.55, repeat: -1, yoyo: true, ease: "steps(1)" });
  });
  gsap.utils.toArray(".amb-spin").forEach((el) => {
    gsap.to(el, { rotation: 360, duration: 80, repeat: -1, ease: "none" });
  });
}

// Title slide, while the room waits: "Personal" tears into "Team" and back, and
// the subtitle types a bad idea, holds, backspaces, and types the next one in a
// fresh random order every load. Runs on its own clock like the ambient motion.
function startWaitingRoom() {
  const word = document.querySelector("[data-swap]");
  const typer = document.querySelector("[data-typer]");
  const source = document.querySelector("#cx-lines");
  if (!word || !typer || !source) return;
  const soft = matchMedia("(prefers-reduced-motion: reduce)").matches;
  // The wrapper page drops a fresh seed on every load (see index.html), so the
  // jokes come in a new order each time; standalone renders fall back to a fixed one.
  let seed = 0x2f6b9d1;
  try {
    seed = Number(localStorage.getItem("kody-deck-seed")) | 0 || seed;
  } catch (e) {}
  const rand = () => {
    seed = (seed + 0x6d2b79f5) | 0;
    let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
  const later = (ms, fn) => setTimeout(fn, ms);
  const scale = soft ? 1.7 : 1;

  // --- glitching word
  const words = ["Personal", "Team"];
  let which = 0;
  const paint = (text) => {
    word.textContent = text;
    word.setAttribute("data-text", text);
  };
  const band = (p) => {
    const top = Math.floor(rand() * 68);
    const h = 16 + Math.floor(rand() * 36);
    word.style.setProperty(`${p}-t`, `${top}%`);
    word.style.setProperty(`${p}-b`, `${Math.max(0, 100 - top - h)}%`);
    word.style.setProperty(`${p}-x`, `${(rand() * 24 - 12).toFixed(1)}px`);
    word.style.setProperty(`${p}-y`, `${(rand() * 8 - 4).toFixed(1)}px`);
  };
  const clear = () => {
    word.removeAttribute("data-glitch");
    word.removeAttribute("data-drop");
    word.style.removeProperty("--w-x");
    word.style.removeProperty("--w-y");
  };
  function glitchTo(next, done) {
    const from = words[which];
    const target = words[next];
    const frames = soft ? 5 : 8;
    let i = 0;
    if (!soft) word.setAttribute("data-glitch", "1");
    (function frame() {
      const mid = Math.floor(frames / 2);
      if (!soft && (i === 0 || i === mid)) word.setAttribute("data-drop", "1");
      else word.removeAttribute("data-drop");
      const p = (i + 1) / frames;
      const len = Math.max(target.length, Math.round(from.length + (target.length - from.length) * p));
      const alphabet = from + target;
      const src = p > 0.7 ? target : from;
      let out = "";
      for (let c = 0; c < len; c++) {
        out += c < src.length && rand() < p * 0.8 ? src[c] : alphabet[Math.floor(rand() * alphabet.length)];
      }
      paint(out);
      if (!soft) {
        band("--a");
        band("--b");
        word.style.setProperty("--w-x", `${(rand() * 10 - 5).toFixed(1)}px`);
        word.style.setProperty("--w-y", `${(rand() * 6 - 3).toFixed(1)}px`);
      }
      i += 1;
      if (i < frames) return later(soft ? 90 : 46, frame);
      paint(target);
      which = next;
      clear();
      if (soft) return done();
      later(160, () => {
        word.setAttribute("data-glitch", "1");
        band("--a");
        band("--b");
        later(80, () => {
          clear();
          done();
        });
      });
    })();
  }
  let firstHold = true;
  (function holdThenSwap() {
    const wait = ((firstHold ? 2800 : which === 0 ? 4600 : 3000) + rand() * 500) * (soft ? 1.35 : 1);
    firstHold = false;
    later(wait, () => glitchTo((which + 1) % 2, holdThenSwap));
  })();

  // --- typed bad ideas
  const lines = JSON.parse(source.textContent);
  for (let s = lines.length - 1; s > 0; s--) {
    const pick = Math.floor(rand() * (s + 1));
    [lines[s], lines[pick]] = [lines[pick], lines[s]];
  }
  let burst = 0;
  const charDelay = (ch) => {
    let ms;
    if (ch === " ") {
      burst = 3;
      ms = 78 + rand() * 55;
    } else if (",.!?".includes(ch)) {
      burst = 0;
      ms = 170 + rand() * 90;
    } else if (burst > 0) {
      burst -= 1;
      ms = 26 + rand() * 24;
    } else {
      ms = 38 + rand() * 46;
      if (rand() < 0.12) ms += 100 + rand() * 140;
    }
    return ms * scale;
  };
  const typeFrom = (text, i, done) => {
    if (i >= text.length) return done();
    typer.textContent = text.slice(0, i + 1);
    later(charDelay(text[i]), () => typeFrom(text, i + 1, done));
  };
  const erase = (done, n = 0) => {
    const cur = typer.textContent;
    if (!cur.length) return done();
    typer.textContent = cur.slice(0, -1);
    later((n < 2 ? 90 + rand() * 36 : 34 + rand() * 16) * scale, () => erase(done, n + 1));
  };
  let index = 0;
  (function cycle() {
    const line = lines[index];
    typeFrom(line, 0, () =>
      later((2000 + line.length * 22 + rand() * 500) * (soft ? 1.25 : 1), () =>
        erase(() => {
          index = (index + 1) % lines.length;
          later(360 * scale, cycle);
        }),
      ),
    );
  })();
}

// Agents: every load picks a fresh random cast from the pool of logos. Slots that
// share a group get different agents; a group + index always means the same agent,
// so "your" agent stays yours across the house story.
const AGENT_POOL = Array.from(document.querySelectorAll('symbol[id^="ag-"]'), (s) => s.id.slice(3)).filter(
  (id) => id !== "cloudflare",
);
function seededRand(salt) {
  let seed = 0x2f6b9d1;
  try {
    seed = Number(localStorage.getItem("kody-deck-seed")) | 0 || seed;
  } catch (e) {}
  seed ^= salt;
  return () => {
    seed = (seed + 0x6d2b79f5) | 0;
    let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}
function showAgent(el, name) {
  el.querySelector("use").setAttribute("href", `#ag-${name}`);
  el.dataset.agent = name;
}
function assignAgents() {
  const rand = seededRand(0x51ab);
  const groups = new Map();
  for (const el of document.querySelectorAll("[data-agent-group]")) {
    const g = el.dataset.agentGroup;
    if (!groups.has(g)) groups.set(g, []);
    groups.get(g).push(el);
  }
  for (const slots of groups.values()) {
    const pool = AGENT_POOL.slice();
    for (let i = pool.length - 1; i > 0; i--) {
      const j = Math.floor(rand() * (i + 1));
      [pool[i], pool[j]] = [pool[j], pool[i]];
    }
    for (const el of slots) showAgent(el, pool[Number(el.dataset.agentIndex) % pool.length]);
  }
}

// Swap one logo for a different agent: it sinks away and the new one drops
// in. Never picks an agent already showing in the same group.
const swapRand = seededRand(0x7e11);
function swapAgent(el, done) {
  if (el.dataset.swapping) return;
  el.dataset.swapping = "1";
  const group = Array.from(document.querySelectorAll("[data-agent-group]")).filter((s) => s.dataset.agentGroup === el.dataset.agentGroup);
  const taken = new Set(group.map((s) => s.dataset.agent));
  const options = AGENT_POOL.filter((a) => !taken.has(a));
  const next = options[Math.floor(swapRand() * options.length)];
  const soft = matchMedia("(prefers-reduced-motion: reduce)").matches;
  gsap.to(el, {
    opacity: 0,
    y: soft ? 0 : 26,
    duration: 0.35,
    ease: "power2.in",
    onComplete: () => {
      showAgent(el, next);
      gsap.fromTo(
        el,
        { opacity: 0, y: soft ? 0 : -26 },
        {
          opacity: 1,
          y: 0,
          duration: 0.5,
          ease: "expo.out",
          onComplete: () => {
            delete el.dataset.swapping;
            done?.();
          },
        },
      );
    },
  });
}

// Click any agent logo to swap it.
document.addEventListener("click", (event) => {
  const el = event.target.closest?.(".agent-logo[data-agent-group]");
  if (el) swapAgent(el);
});

// On the common house, people keep changing agents on their own too.
function startAgentSwaps() {
  if (matchMedia("(prefers-reduced-motion: reduce)").matches) return;
  const slots = Array.from(document.querySelectorAll(".agent-swap"));
  if (!slots.length) return;
  (function loop() {
    setTimeout(() => {
      const el = slots[Math.floor(swapRand() * slots.length)];
      if (el.dataset.swapping) return loop();
      swapAgent(el, loop);
    }, (2.2 + swapRand() * 1.8) * 1000);
  })();
}