{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"hello-zoom","type":"registry:component","title":"Hello Zoom","description":"Words flicker in place, one line types on, then the camera dives through its own letters into the next card","dependencies":["remotion","@remotion/google-fonts"],"docs":"Needs a Remotion project. If a frame renders black, the component is drawing on a transparent layer: put a background behind it, e.g. <AbsoluteFill style={{ background: \"#faf9f6\" }}><Component /></AbsoluteFill>. Pass mode=\"dark\" for dark frames.","registryDependencies":["https://kuchi.kalarislabs.com/r/kuchi-ui.json"],"files":[{"path":"components/kuchi/hello-zoom/index.tsx","type":"registry:component","target":"components/kuchi/hello-zoom/index.tsx","content":"/*! Portions Copyright (c) 2026 Sri Nath (snap-cn). MIT License. See THIRD_PARTY_LICENSES. */\n\"use client\";\r\n\r\nimport { loadFont } from \"@remotion/google-fonts/Inter\";\r\nimport type { CSSProperties, ReactNode } from \"react\";\r\nimport { useEffect, useId, useState } from \"react\";\r\nimport {\r\n  AbsoluteFill,\r\n  continueRender,\r\n  delayRender,\r\n  useCurrentFrame,\r\n  useVideoConfig,\r\n} from \"remotion\";\r\nimport {\r\n  resolveFont,\r\n  type KuchiTheme,\r\n  useKuchiTheme,\r\n} from \"@/lib/kuchi-ui\";\r\n\r\n/**\r\n * Hello Zoom — words flicker on a dark card and grow, one word lands huge on a\r\n * light card, the rest of its line types on, then the camera flies through a\r\n * letter. The type is a hole, not ink: the next card has been behind it all along.\r\n *\r\n * Every number is measured frame by frame off the reference. The scene is drawn\r\n * in a 1920-wide stage and scaled to the composition, so the numbers read as\r\n * reference px at any size.\r\n */\r\n\r\nconst { fontFamily: INTER } = loadFont(\"normal\", {\r\n  weights: [\"500\", \"600\", \"700\", \"800\"],\r\n  subsets: [\"latin\"],\r\n});\r\n\r\nconst STAGE_W = 1920;\r\n\r\n/** Piecewise-linear lookup over [frame, value] pairs, held at the ends. */\r\nexport const track = (\r\n  pts: readonly (readonly [number, number])[],\r\n  t: number,\r\n) => {\r\n  if (t <= pts[0]![0]) return pts[0]![1];\r\n  for (let i = 1; i < pts.length; i++) {\r\n    const [t1, v1] = pts[i]!;\r\n    const [t0, v0] = pts[i - 1]!;\r\n    if (t <= t1) return v0 + ((v1 - v0) * (t - t0)) / (t1 - t0);\r\n  }\r\n  return pts[pts.length - 1]![1];\r\n};\r\n\r\n// --- The shot list, in frames at 30fps ---------------------------------------\r\n\r\n/** The words share this many beats; the cut to the lead takes the last one. */\r\nconst FLICKER_BEATS = 21;\r\nconst LEAD_AT = 20;\r\nconst LINE_AT = 25;\r\nconst TYPE_FRAMES = 5;\r\nconst NEXT_AT = 49;\r\n\r\n/** Font size and baseline below centre of each word, read across the flicker. */\r\nconst WORD_SIZE = [75.4, 93.7, 115.9, 124.9, 150.2, 186.3, 225.1];\r\nconst WORD_DROP = [20.4, 25.3, 30.2, 31.9, 40.5, 49.9, 60.1];\r\n/** Each word sits this far off true centre in the reference, px. */\r\nconst WORD_NUDGE = [-0.3, -1.8, -2, -0.8, -0.3, -2.1, -2.9];\r\nconst WORD_TRACKING = -0.02;\r\n\r\nconst LEAD_SIZE = 433.14;\r\nconst LEAD_DROP = 156.57;\r\nconst LEAD_TRACKING = -0.047;\r\n/** The reference sets the lead this far left of true centre, px. */\r\nconst LEAD_NUDGE = -17.7;\r\n/** The lead starts to leave before the cut: px left of centre. */\r\nconst LEAD_DRIFT = [\r\n  [20, 0],\r\n  [21, 1.54],\r\n  [22, 4.37],\r\n  [23, 9.37],\r\n  [24, 18],\r\n] as const;\r\n\r\nconst LINE_SIZE = 210.38;\r\nconst LINE_TRACKING = -0.025;\r\n/** How much of the way from its resting spot to a centred lead the line starts. */\r\nconst LINE_START = 0.6893;\r\n/** The line's slide to rest, as the fraction of that distance still to go. */\r\nconst LINE_SLIDE = [\r\n  [25, 1],\r\n  [26, 0.6139],\r\n  [27, 0.3242],\r\n  [28, 0.183],\r\n  [29, 0.1012],\r\n  [30, 0.0503],\r\n  [31, 0.0203],\r\n  [32, 0.0047],\r\n  [33, 0],\r\n] as const;\r\n\r\n/** The push through the letter, about the middle of the stage. */\r\nconst ZOOM = [\r\n  [36, 1],\r\n  [37, 1.0036],\r\n  [38, 1.0155],\r\n  [39, 1.0371],\r\n  [40, 1.071],\r\n  [41, 1.1213],\r\n  [42, 1.1947],\r\n  [43, 1.3018],\r\n  [44, 1.4694],\r\n  [45, 1.7476],\r\n  [46, 2.2753],\r\n  [47, 3.5662],\r\n  [48, 11.7],\r\n] as const;\r\n\r\n/** The camera drifts off the stage's centre as it dives, px. */\r\nconst ZOOM_X = [\r\n  [37, 0],\r\n  [38, -0.3],\r\n  [44, -0.64],\r\n  [45, -0.98],\r\n  [46, -2.27],\r\n  [47, -1.25],\r\n  [48, -30.9],\r\n] as const;\r\nconst ZOOM_Y = [\r\n  [42, 0],\r\n  [43, 0.15],\r\n  [45, 0.18],\r\n  [46, 0.74],\r\n  [47, 2.13],\r\n  [48, 4.35],\r\n] as const;\r\n\r\nconst NEXT_SIZE = 74;\r\nconst NEXT_DROP = 19.7;\r\n/** The next word rushes up from nothing behind the type, then barely creeps. */\r\nconst NEXT_SCALE = [\r\n  [41, 0],\r\n  [42, 0.01],\r\n  [43, 0.0576],\r\n  [44, 0.1023],\r\n  [45, 0.1647],\r\n  [46, 0.262],\r\n  [47, 0.4035],\r\n  [48, 0.615],\r\n  [49, 0.868],\r\n  [50, 0.993],\r\n  [51, 1],\r\n  [75, 1.05],\r\n] as const;\r\n\r\n/** Copy never runs wider than this. */\r\nconst MAX_WIDTH = 1728;\r\n\r\n// --- Timeline helpers --------------------------------------------------------\r\n\r\n/** Which word of `count` is up at frame `f`. */\r\nexport const wordAt = (f: number, count: number) =>\r\n  Math.min(count - 1, Math.floor((f * count) / FLICKER_BEATS));\r\n\r\n/** How many characters of `length` have been typed at frame `f`. */\r\nexport const typedAt = (f: number, length: number) =>\r\n  Math.round(\r\n    Math.min(1, Math.max(0, Math.floor(f - LINE_AT) / TYPE_FRAMES)) * length,\r\n  );\r\n\r\n/**\r\n * The deepest point of ink between columns `from` and `to` of a raster, by\r\n * chamfer distance to the nearest paper. This is what the camera flies through:\r\n * aim at a letter's counter and it comes out the far side onto blank paper.\r\n * Ties go to the point nearest the middle of the span and of `midY`.\r\n */\r\nexport const deepestInk = (\r\n  alpha: ArrayLike<number>,\r\n  w: number,\r\n  h: number,\r\n  from: number,\r\n  to: number,\r\n  midY: number,\r\n) => {\r\n  const d = new Float32Array(w * h);\r\n  const at = (x: number, y: number) =>\r\n    x < 0 || y < 0 || x >= w || y >= h ? 0 : d[y * w + x]!;\r\n  for (let y = 0; y < h; y++)\r\n    for (let x = 0; x < w; x++)\r\n      if (alpha[y * w + x]! > 127)\r\n        d[y * w + x] =\r\n          1 +\r\n          Math.min(\r\n            at(x - 1, y),\r\n            at(x, y - 1),\r\n            at(x - 1, y - 1),\r\n            at(x + 1, y - 1),\r\n          );\r\n  for (let y = h - 1; y >= 0; y--)\r\n    for (let x = w - 1; x >= 0; x--)\r\n      if (d[y * w + x]! > 0)\r\n        d[y * w + x] = Math.min(\r\n          d[y * w + x]!,\r\n          1 +\r\n            Math.min(\r\n              at(x + 1, y),\r\n              at(x, y + 1),\r\n              at(x + 1, y + 1),\r\n              at(x - 1, y + 1),\r\n            ),\r\n        );\r\n  const midX = (from + to) / 2;\r\n  let best = { x: midX, y: midY, r: 0 };\r\n  let top = -Infinity;\r\n  for (let y = 0; y < h; y++)\r\n    for (let x = from; x < to; x++) {\r\n      const r = at(x, y);\r\n      const score = r - 0.01 * (Math.abs(x - midX) + Math.abs(y - midY));\r\n      if (r > 0 && score > top) {\r\n        top = score;\r\n        best = { x, y, r };\r\n      }\r\n    }\r\n  return best;\r\n};\r\n\r\ninterface Metrics {\r\n  /** Advance widths in em. */\r\n  words: number[];\r\n  lead: number;\r\n  /** The lead at the line's tracking. */\r\n  leadInLine: number;\r\n  line: number;\r\n  next: number;\r\n  /** The point of the line the camera flies through: em right of its start, up from its baseline. */\r\n  through: [number, number];\r\n}\r\n\r\n/** A canvas shapes type the way the page does, and rasters it for `deepestInk`. */\r\nconst measure = (\r\n  face: string,\r\n  weight: number,\r\n  words: string[],\r\n  lead: string,\r\n  line: string,\r\n  next: string,\r\n): Metrics => {\r\n  const P = 100; // raster px per em\r\n  const ctx = document\r\n    .createElement(\"canvas\")\r\n    .getContext(\"2d\", { willReadFrequently: true })!;\r\n  const setFont = (tracking: number) => {\r\n    ctx.font = `${weight} ${P}px ${face}`;\r\n    ctx.letterSpacing = `${tracking * P}px`;\r\n  };\r\n  const em = (text: string, tracking: number) => {\r\n    setFont(tracking);\r\n    return ctx.measureText(text).width / P;\r\n  };\r\n  const out = {\r\n    words: words.map((w) => em(w, WORD_TRACKING)),\r\n    lead: em(lead, LEAD_TRACKING),\r\n    leadInLine: em(lead, LINE_TRACKING),\r\n    line: em(line, LINE_TRACKING),\r\n    next: em(next, WORD_TRACKING),\r\n  };\r\n  const w = Math.ceil(out.line * P);\r\n  const h = 2 * P;\r\n  const base = 1.3 * P;\r\n  ctx.canvas.width = w;\r\n  ctx.canvas.height = h;\r\n  setFont(LINE_TRACKING); // resizing a canvas resets its state\r\n  ctx.fillText(line, 0, base);\r\n  const { data } = ctx.getImageData(0, 0, w, h);\r\n  const alpha = new Uint8ClampedArray(w * h);\r\n  for (let i = 0; i < alpha.length; i++) alpha[i] = data[i * 4 + 3]!;\r\n  // The middle of the line only: the point lands on the stage's centre, and one\r\n  // near an end would throw the whole line off to the side.\r\n  const p = deepestInk(\r\n    alpha,\r\n    w,\r\n    h,\r\n    Math.round(w * 0.35),\r\n    Math.round(w * 0.65),\r\n    base - 0.26 * P,\r\n  );\r\n  return { ...out, through: [p.x / P, (base - p.y) / P] };\r\n};\r\n\r\n/**\r\n * Chrome snaps a glyph's baseline to a whole device pixel, so a line moving\r\n * vertically climbs in steps and a resting one sits up to half a pixel off. Two\r\n * copies a pixel apart, summed in a group, land the baseline anywhere between.\r\n */\r\nconst Line = ({\r\n  x,\r\n  y,\r\n  scale = 1,\r\n  size,\r\n  tracking,\r\n  px,\r\n  fill,\r\n  anchor,\r\n  children,\r\n}: {\r\n  x: number;\r\n  y: number;\r\n  scale?: number;\r\n  size: number;\r\n  tracking: number;\r\n  px: number;\r\n  fill: string;\r\n  anchor?: \"middle\";\r\n  children: ReactNode;\r\n}) => {\r\n  const row = Math.floor(y * px);\r\n  const f = y * px - row;\r\n  const copy = (base: number, opacity: number) => (\r\n    <text\r\n      transform={`translate(${x} ${base / px}) scale(${scale})`}\r\n      fontSize={size}\r\n      fill={fill}\r\n      opacity={opacity}\r\n      textAnchor={anchor}\r\n      style={{ mixBlendMode: \"plus-lighter\", letterSpacing: `${tracking}em` }}\r\n    >\r\n      {children}\r\n    </text>\r\n  );\r\n  return (\r\n    <g style={{ isolation: \"isolate\" }}>\r\n      {copy(row, 1 - f)}\r\n      {copy(row + 1, f)}\r\n    </g>\r\n  );\r\n};\r\n\r\nexport interface HelloZoomProps {\r\n  /** The words that flicker on the first card. A comma-separated string works too. */\r\n  words?: string[] | string;\r\n  /** The word that lands huge on the second card. */\r\n  lead?: string;\r\n  /** Typed on after the lead. */\r\n  rest?: string;\r\n  /** What the camera finds behind the type. */\r\n  nextWord?: string;\r\n  /** A label from `fonts.ts` or a CSS family you have loaded. Defaults to Inter. */\r\n  fontFamily?: string;\r\n  fontWeight?: number;\r\n  /** The first and last cards, and the type on the second. Defaults to `foreground`. */\r\n  inkColor?: string;\r\n  /** The second card, and the type on the first and last. Defaults to `background`. */\r\n  paperColor?: string;\r\n  /**\r\n   * The point of the resting line the camera flies through, in em right of the\r\n   * line's start and up from its baseline. Unset, it finds the deepest ink near the middle.\r\n   */\r\n  through?: [number, number];\r\n  speed?: number;\r\n  theme?: Partial<KuchiTheme>;\r\n  mode?: \"light\" | \"dark\";\r\n}\r\n\r\nexport const HelloZoom = ({\r\n  words = [\r\n    \"React\",\r\n    \"Remotion\",\r\n    \"shadcn\",\r\n    \"Next.js\",\r\n    \"Vite\",\r\n    \"Tailwind\",\r\n    \"TypeScript\",\r\n  ],\r\n  lead = \"Hello\",\r\n  rest = \"kuchi.\",\r\n  nextWord = \"npx\",\r\n  fontFamily,\r\n  fontWeight = 700,\r\n  inkColor,\r\n  paperColor,\r\n  through,\r\n  speed = 1,\r\n  theme,\r\n  mode,\r\n}: HelloZoomProps) => {\r\n  const t = useKuchiTheme(theme, mode);\r\n  const f = useCurrentFrame() * speed;\r\n  const { width, height } = useVideoConfig();\r\n  const id = useId().replace(/:/g, \"\");\r\n  const ink = inkColor ?? t.foreground;\r\n  const paper = paperColor ?? t.background;\r\n  const face = resolveFont(fontFamily) ?? resolveFont(t.fontFamily) ?? INTER;\r\n  const list = (typeof words === \"string\" ? words.split(\",\") : words)\r\n    .map((w) => w.trim())\r\n    .filter(Boolean);\r\n  const line = `${lead} ${rest}`;\r\n  const joined = list.join(\"\\n\");\r\n\r\n  const [m, setM] = useState<Metrics | null>(null);\r\n  const [handle] = useState(() => delayRender(\"hello-zoom: measuring type\"));\r\n  useEffect(() => {\r\n    let live = true;\r\n    // measuring a fallback face measures the wrong face\r\n    document.fonts\r\n      .load(`${fontWeight} 100px ${face}`, joined + line + nextWord)\r\n      .catch(() => undefined)\r\n      .then(() => {\r\n        if (live)\r\n          setM(\r\n            measure(face, fontWeight, joined.split(\"\\n\"), lead, line, nextWord),\r\n          );\r\n      });\r\n    return () => {\r\n      live = false;\r\n    };\r\n  }, [face, fontWeight, joined, lead, line, nextWord]);\r\n  useEffect(() => {\r\n    if (m) continueRender(handle);\r\n  }, [m, handle]);\r\n\r\n  const W = STAGE_W;\r\n  const H = (STAGE_W * height) / width;\r\n  const cx = W / 2;\r\n  const cy = H / 2;\r\n  const px = width / W;\r\n\r\n  const type: CSSProperties = {\r\n    fontFamily: face,\r\n    fontWeight,\r\n    textRendering: \"geometricPrecision\",\r\n    whiteSpace: \"pre\",\r\n  };\r\n  const fit = (size: number, em: number) => Math.min(size, MAX_WIDTH / em);\r\n\r\n  const scene = () => {\r\n    if (!m) return null;\r\n\r\n    const nextScale = track(NEXT_SCALE, f);\r\n    const next = (\r\n      <Line\r\n        x={cx}\r\n        y={cy + NEXT_DROP * nextScale}\r\n        scale={nextScale}\r\n        size={fit(NEXT_SIZE, m.next)}\r\n        tracking={WORD_TRACKING}\r\n        px={px}\r\n        fill={paper}\r\n        anchor=\"middle\"\r\n      >\r\n        {nextWord}\r\n      </Line>\r\n    );\r\n\r\n    // ---- 1. the flicker\r\n    if (f < LEAD_AT) {\r\n      const i = wordAt(f, list.length);\r\n      const at = list.length > 1 ? (i / (list.length - 1)) * 6 : 0;\r\n      const row = WORD_SIZE.map((v, j) => [j, v] as const);\r\n      const drop = WORD_DROP.map((v, j) => [j, v] as const);\r\n      const nudge = WORD_NUDGE.map((v, j) => [j, v] as const);\r\n      return (\r\n        <Line\r\n          x={cx + track(nudge, at)}\r\n          y={cy + track(drop, at)}\r\n          size={fit(track(row, at), m.words[i]!)}\r\n          tracking={WORD_TRACKING}\r\n          px={px}\r\n          fill={paper}\r\n          anchor=\"middle\"\r\n        >\r\n          {list[i]}\r\n        </Line>\r\n      );\r\n    }\r\n\r\n    // ---- 3. through the letter\r\n    if (f >= NEXT_AT) return next;\r\n\r\n    // ---- 2. the light card, its type a window onto the next one\r\n    let lineType: ReactNode;\r\n    if (f < LINE_AT) {\r\n      const size = fit(LEAD_SIZE, m.lead);\r\n      lineType = (\r\n        <Line\r\n          x={cx - (m.lead * size) / 2 + LEAD_NUDGE - track(LEAD_DRIFT, f)}\r\n          y={cy + LEAD_DROP}\r\n          size={size}\r\n          tracking={LEAD_TRACKING}\r\n          px={px}\r\n          fill=\"#fff\"\r\n        >\r\n          {lead}\r\n        </Line>\r\n      );\r\n    } else {\r\n      const [tx, ty] = through ?? m.through;\r\n      const reach = Math.max(tx, m.line - tx);\r\n      const size = Math.min(LINE_SIZE, MAX_WIDTH / 2 / reach);\r\n      const rest0 = cx - tx * size;\r\n      const start = cx - (m.leadInLine * size) / 2;\r\n      const x = rest0 + track(LINE_SLIDE, f) * LINE_START * (start - rest0);\r\n      const y = cy + ty * size;\r\n      const s = track(ZOOM, f);\r\n      const n = typedAt(f, rest.length);\r\n      lineType = (\r\n        <Line\r\n          x={cx + s * (x - cx) + track(ZOOM_X, f)}\r\n          y={cy + s * (y - cy) + track(ZOOM_Y, f)}\r\n          scale={s}\r\n          size={size}\r\n          tracking={LINE_TRACKING}\r\n          px={px}\r\n          fill=\"#fff\"\r\n        >\r\n          {n > 0 ? `${lead} ${rest.slice(0, n)}` : lead}\r\n        </Line>\r\n      );\r\n    }\r\n    return (\r\n      <>\r\n        <mask\r\n          id={id}\r\n          maskUnits=\"userSpaceOnUse\"\r\n          x={0}\r\n          y={0}\r\n          width={W}\r\n          height={H}\r\n        >\r\n          {lineType}\r\n        </mask>\r\n        <g mask={`url(#${id})`}>\r\n          <rect width={W} height={H} fill={ink} />\r\n          {next}\r\n        </g>\r\n      </>\r\n    );\r\n  };\r\n\r\n  return (\r\n    <AbsoluteFill\r\n      style={{ background: f >= LEAD_AT && f < NEXT_AT ? paper : ink }}\r\n    >\r\n      <svg\r\n        viewBox={`0 0 ${W} ${H}`}\r\n        width={width}\r\n        height={height}\r\n        style={{ position: \"absolute\", inset: 0, ...type }}\r\n      >\r\n        {scene()}\r\n      </svg>\r\n    </AbsoluteFill>\r\n  );\r\n};\r\n\r\nexport default HelloZoom;\r\n"}]}