{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "kerning-snap",
  "title": "Kerning Snap",
  "description": "Loose letters snap to correct kerning one by one in a gradient serif word.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/kerning-snap/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/Fraunces\";\nimport { useId, useMemo } from \"react\";\nimport {\n  AbsoluteFill,\n  Easing,\n  interpolate,\n  spring,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst { fontFamily: sans } = loadFont(\"normal\", {\n  weights: [\"400\"],\n  subsets: [\"latin\"],\n});\n\nexport interface KerningSnapProps {\n  word?: string;\n  interval?: number;\n  accent?: string;\n  theme?: \"dark\" | \"light\";\n  speed?: number;\n}\n\n/** Fraunces 400 advance widths (per 1000 em) for ASCII 32–126. */\nconst FRAUNCES_400 = [\n  224, 308, 336, 648, 590, 738, 743, 157, 345, 345, 624, 556, 252, 416, 242,\n  492, 648, 448, 594, 539, 595, 565, 592, 506, 581, 596, 248, 259, 512, 556,\n  512, 495, 884, 712, 678, 696, 782, 646, 595, 757, 828, 381, 470, 758, 614,\n  898, 776, 794, 664, 794, 714, 590, 674, 743, 701, 1032, 722, 681, 636, 326,\n  492, 330, 481, 476, 229, 542, 592, 508, 604, 521, 366, 558, 621, 301, 301,\n  594, 304, 936, 621, 583, 606, 592, 451, 468, 364, 606, 550, 810, 530, 554,\n  474, 388, 264, 388, 636,\n];\n/** Fraunces 400 GPOS kerning (|k| ≥ 30/1000), \"pairValue\". */\nconst KERN_PAIRS = [\n  \"AT-45 AV-44 AW-44 AY-55 Av-35 Aw-35 Ay-35 FA-39 FJ-44 Fa-40 Fg-38 LT-45\",\n  \"LV-59 LW-59 LY-59 PA-44 RY-30 TA-45 TJ-38 Ta-50 Tc-50 Td-50 Te-50 Tg-50\",\n  \"To-50 Tq-50 Ts-50 VA-45 VJ-38 Va-55 Vc-55 Vd-55 Ve-55 Vg-54 Vi-42 Vm-42\",\n  \"Vn-42 Vo-55 Vp-38 Vq-55 Vr-42 Vs-53 Vt-30 Vu-30 Vv-30 Vw-30 Vx-30 Vy-30\",\n  \"Vz-46 WA-45 WJ-38 Wa-55 Wc-55 Wd-55 We-55 Wg-54 Wi-42 Wm-42 Wn-42 Wo-55\",\n  \"Wp-38 Wq-55 Wr-42 Ws-53 Wt-30 Wu-30 Wv-30 Ww-30 Wx-30 Wy-30 Wz-46 YA-55\",\n  \"YJ-38 Ya-57 Yc-60 Yd-60 Ye-60 Yg-61 Yi-38 Ym-38 Yn-38 Yo-60 Yp-50 Yq-60\",\n  \"Yr-38 Ys-61 Yt-31 Yu-31 Yv-38 Yw-38 Yx-38 Yy-38 Yz-43 aT-40 aV-44 aW-44\",\n  \"aY-52 bT-50 bV-54 bW-54 bY-60 cY-36 eY-36 hV-38 hW-38 hY-38 kY-30 mV-38\",\n  \"mW-38 mY-38 nV-38 nW-38 nY-38 oT-50 oV-54 oW-54 oY-60 pT-50 pV-54 pW-54\",\n  \"pY-60 qV-37 qW-37 qY-50 rV-34 rW-34 rY-50 sV-35 sW-35 sY-50 tY-30 vA-35\",\n  \"vY-37 wA-35 wY-37 xV-30 xW-30 xY-37 yA-35 yY-37 zV-37 zW-37 zY-37\",\n].join(\" \");\nconst KERN = new Map(\n  KERN_PAIRS.split(\" \").map((p) => [p.slice(0, 2), Number(p.slice(2)) / 1000]),\n);\nconst TRACK = -0.02;\nconst CAP = 0.7;\nconst BASELINE = 0.8615;\n/** Padding around each glyph box so the clip-text gradient covers overhangs and descenders. */\nconst PAD_X = 0.12;\nconst PAD_Y = 0.2;\n\nconst FIRST_SNAP = 30;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\n// gradients.sh \"halo\": one continuous fill across the word. Loose letters carry\n// their own slice of it, so the gradient only reads whole once the word is set.\nconst THEMES = {\n  dark: {\n    bg: \"#030306\",\n    fg: \"#F6F1EC\",\n    grain: 0.07,\n    fill: \"linear-gradient(100deg, #FFF4EC 0%, #FFB36B 34%, #E2457A 70%, #8F6BFF 100%)\",\n    glow: \"radial-gradient(60% 42% at 50% 54%, rgba(226,69,122,0.13) 0%, rgba(61,77,255,0.06) 45%, transparent 72%)\",\n  },\n  light: {\n    bg: \"#F6F3EF\",\n    fg: \"#141216\",\n    grain: 0.045,\n    fill: \"linear-gradient(100deg, #F07A2A 0%, #E2457A 45%, #3D4DFF 100%)\",\n    glow: \"radial-gradient(60% 42% at 50% 54%, rgba(255,179,107,0.16) 0%, rgba(226,69,122,0.06) 45%, transparent 72%)\",\n  },\n} as const;\n\nfunction hash(i: number, salt: number) {\n  const s = Math.sin(i * 127.1 + salt * 311.7) * 43758.5453;\n  return s - Math.floor(s);\n}\n\nfunction advance(ch: string) {\n  const code = ch.charCodeAt(0);\n  return (code >= 32 && code < 127 ? FRAUNCES_400[code - 32] : 600) / 1000;\n}\n\nfunction Grain({ opacity }: { opacity: number }) {\n  const id = `g${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n  return (\n    <svg\n      width=\"100%\"\n      height=\"100%\"\n      style={{ position: \"absolute\", inset: 0, opacity, pointerEvents: \"none\" }}\n      aria-hidden\n    >\n      <filter id={id}>\n        <feTurbulence\n          type=\"fractalNoise\"\n          baseFrequency=\"0.9\"\n          numOctaves={2}\n          seed={17}\n          stitchTiles=\"stitch\"\n        />\n        <feColorMatrix values=\"0 0 0 0 0.5  0 0 0 0 0.5  0 0 0 0 0.5  0 0 0 0.9 0\" />\n      </filter>\n      <rect width=\"100%\" height=\"100%\" filter={`url(#${id})`} />\n    </svg>\n  );\n}\n\nfunction Tick({\n  side,\n  F,\n  color,\n  flash,\n}: {\n  side: \"left\" | \"right\";\n  F: number;\n  color: string;\n  flash: number;\n}) {\n  if (flash <= 0.001) return null;\n  const h = CAP * F * 1.36;\n  const top = PAD_Y * F + BASELINE * F - CAP * F - (h - CAP * F) / 2;\n  const cap = Math.max(8, F * 0.08);\n  const line = Math.max(2, F / 72);\n  return (\n    <div\n      style={{\n        position: \"absolute\",\n        top,\n        [side]: PAD_X * F - line / 2,\n        width: line,\n        height: h,\n        opacity: flash,\n        transform: `scaleY(${0.82 + 0.18 * flash})`,\n      }}\n    >\n      <div style={{ position: \"absolute\", inset: 0, background: color }} />\n      {[0, 1].map((k) => (\n        <div\n          key={k}\n          style={{\n            position: \"absolute\",\n            left: (line - cap) / 2,\n            width: cap,\n            height: line,\n            top: k === 0 ? 0 : h - line,\n            background: color,\n          }}\n        />\n      ))}\n    </div>\n  );\n}\n\nexport function KerningSnap({\n  word = \"Typeset\",\n  interval = 7,\n  accent = \"#3B5BFD\",\n  theme = \"dark\",\n  speed = 1,\n}: KerningSnapProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H, fps } = useVideoConfig();\n  const c = THEMES[theme];\n\n  const chars = useMemo(() => Array.from(word), [word]);\n  const n = chars.length;\n  const kerns = useMemo(\n    () =>\n      chars.map((ch, i) => (i === 0 ? 0 : (KERN.get(chars[i - 1] + ch) ?? 0))),\n    [chars],\n  );\n  const finalEm = useMemo(\n    () => chars.reduce((s, ch, i) => s + advance(ch) + TRACK + kerns[i], 0),\n    [chars, kerns],\n  );\n  const F = Math.min(H * 0.28, (W * 0.62) / finalEm);\n\n  const finalCenters = useMemo(() => {\n    let x = -(finalEm * F) / 2;\n    return chars.map((ch, i) => {\n      x += kerns[i] * F;\n      const w = (advance(ch) + TRACK) * F;\n      const cx = x + w / 2;\n      x += w;\n      return cx;\n    });\n  }, [chars, kerns, finalEm, F]);\n\n  const finalLefts = useMemo(\n    () =>\n      chars.map(\n        (ch, i) =>\n          finalCenters[i] - ((advance(ch) + TRACK) * F) / 2 + (finalEm * F) / 2,\n      ),\n    [chars, finalCenters, finalEm, F],\n  );\n\n  const anchor = Math.floor((n - 1) / 2);\n  const order = useMemo(() => {\n    const seq: number[] = [];\n    for (let d = 1; seq.length < n - 1; d++) {\n      if (anchor + d < n) seq.push(anchor + d);\n      if (anchor - d >= 0) seq.push(anchor - d);\n    }\n    return seq;\n  }, [anchor, n]);\n  const snapAt = useMemo(() => {\n    const at = new Array<number>(n).fill(FIRST_SNAP - interval);\n    order.forEach((j, k) => {\n      at[j] = FIRST_SNAP + k * interval;\n    });\n    return at;\n  }, [order, n, interval]);\n\n  const spread = useMemo(() => {\n    const span = W * 0.78;\n    const raw: number[] = [];\n    let acc = 0;\n    for (let i = 0; i < n; i++) {\n      if (i > 0) acc += 0.75 + hash(i, 3) * 0.55;\n      raw.push(acc);\n    }\n    const total = raw[n - 1] || 1;\n    return raw.map((r) => -span / 2 + (r / total) * span);\n  }, [n, W]);\n\n  const lastSnap = FIRST_SNAP + (n - 2) * interval;\n  const gather = interpolate(t, [0, lastSnap + 4], [0, 1], {\n    ...CLAMP,\n    easing: EASE_IO,\n  });\n  const push = interpolate(t, [0, lastSnap + 60], [0, 1], CLAMP);\n\n  const snap = (i: number) =>\n    i === anchor\n      ? 1\n      : spring({\n          frame: t - snapAt[i],\n          fps,\n          config: { damping: 19, stiffness: 420, mass: 0.55 },\n        });\n  const flash = (j: number) => {\n    const d = t - snapAt[j] - 3;\n    if (d < 0) return 0;\n    return interpolate(d, [0, 2, 12], [0, 1, 0], {\n      ...CLAMP,\n      easing: EASE_OUT,\n    });\n  };\n\n  const anchorDrift = (spread[anchor] - finalCenters[anchor]) * (1 - gather);\n  const anchorLift = (hash(anchor, 7) - 0.5) * F * 0.12 * (1 - gather);\n\n  return (\n    <AbsoluteFill style={{ background: c.bg, overflow: \"hidden\" }}>\n      <AbsoluteFill\n        style={{ background: c.glow, opacity: 0.4 + 0.6 * gather }}\n      />\n      <AbsoluteFill\n        style={{\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"center\",\n          transform: `scale(${1 + push * 0.02})`,\n        }}\n      >\n        <div\n          style={{\n            display: \"flex\",\n            alignItems: \"baseline\",\n            whiteSpace: \"pre\",\n            fontFamily: sans,\n            fontWeight: 400,\n            fontSize: F,\n            lineHeight: 1,\n            letterSpacing: `${TRACK}em`,\n            fontKerning: \"none\",\n            color: c.fg,\n            transform: `translateY(${F * 0.04}px)`,\n          }}\n        >\n          {chars.map((ch, i) => {\n            const s = snap(i);\n            const loose = 1 - s;\n            const startOff =\n              spread[i] -\n              finalCenters[i] -\n              (spread[anchor] - finalCenters[anchor]);\n            const wander = 1 - 0.72 * gather;\n            const dx = anchorDrift + startOff * wander * loose;\n            const dy =\n              anchorLift + (hash(i, 7) - 0.5) * F * 0.5 * wander * loose;\n            const rot = (hash(i, 11) - 0.5) * 12 * wander * loose;\n            const o = interpolate(t, [2 + i * 2, 16 + i * 2], [0, 1], {\n              ...CLAMP,\n              easing: EASE_OUT,\n            });\n            const leftTick = i > anchor ? flash(i) : 0;\n            const rightTick = i < anchor ? flash(i) : 0;\n            return (\n              <span\n                key={`${i}-${ch}`}\n                style={{\n                  position: \"relative\",\n                  display: \"inline-block\",\n                  marginLeft: (kerns[i] - PAD_X) * F,\n                  marginRight: -PAD_X * F,\n                  padding: `${PAD_Y * F}px ${PAD_X * F}px`,\n                  color: \"transparent\",\n                  backgroundImage: c.fill,\n                  backgroundSize: `${(finalEm + PAD_X * 2) * F}px 100%`,\n                  backgroundPosition: `${-finalLefts[i]}px 0px`,\n                  backgroundRepeat: \"no-repeat\",\n                  WebkitBackgroundClip: \"text\",\n                  backgroundClip: \"text\",\n                  opacity: o,\n                  transform: `translate(${dx}px, ${dy}px) rotate(${rot}deg)`,\n                }}\n              >\n                {ch}\n                <Tick side=\"left\" F={F} color={accent} flash={leftTick} />\n                <Tick side=\"right\" F={F} color={accent} flash={rightTick} />\n              </span>\n            );\n          })}\n        </div>\n      </AbsoluteFill>\n      <Grain opacity={c.grain} />\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/kerning-snap.tsx"
    }
  ],
  "type": "registry:component"
}
