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