{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "rubberband-word",
  "title": "Rubberband Word",
  "description": "A cursor grabs a word by its last letter, stretches it and snaps it into a new word.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/rubberband-word/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/Sora\";\nimport type React from \"react\";\nimport { useId } 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: [\"500\"],\n  subsets: [\"latin\"],\n});\n\nexport interface RubberbandWordProps {\n  /** The word the cursor grabs by its last letter. */\n  word?: string;\n  /** What the word becomes on the snap. Leave empty to keep the word and only accent it. */\n  snapWord?: string;\n  /** Close the final word with an accent period. */\n  period?: boolean;\n  accent?: string;\n  theme?: \"dark\" | \"light\";\n  speed?: number;\n}\n\n// Sora 500, per 1000 em, chars 32–126 (harfbuzz, wght 500).\nconst ADV = [\n  222, 303, 452, 702, 652, 864, 702, 264, 382, 382, 568, 592, 266, 506, 266,\n  365, 750, 423, 623, 620, 652, 630, 668, 585, 647, 668, 266, 266, 592, 590,\n  592, 556, 1124, 765, 684, 797, 788, 598, 560, 832, 803, 320, 641, 703, 549,\n  937, 858, 865, 649, 865, 710, 682, 602, 785, 719, 1052, 705, 654, 652, 382,\n  365, 382, 587, 581, 300, 581, 694, 608, 694, 616, 380, 679, 644, 317, 324,\n  595, 296, 973, 644, 678, 694, 694, 414, 543, 429, 634, 572, 873, 574, 555,\n  493, 382, 375, 382, 511,\n];\nconst RSB = [\n  0, 81, 81, 35, 50, 31, 25, 80, 13, 64, 45, 60, 51, 59, 65, 23, 67, 110, 53,\n  55, 24, 42, 47, 31, 47, 66, 65, 48, 74, 60, 74, 43, 61, 23, 60, 33, 41, 59,\n  44, 31, 98, 100, 86, -9, 26, 98, 98, 51, 32, 51, 21, 49, 24, 84, 28, 27, 15,\n  15, 43, 50, 15, 102, 60, 0, 86, 77, 48, 35, 86, 40, 10, 80, 77, 88, 84, 1, 86,\n  77, 77, 48, 48, 86, 18, 42, 41, 88, 30, 34, 16, 24, 34, 43, 136, 52, 20,\n];\nconst LSB = [\n  0, 82, 81, 35, 49, 31, 29, 81, 64, 13, 45, 61, 20, 59, 65, 23, 67, 39, 62, 41,\n  43, 53, 66, 43, 47, 47, 65, 23, 74, 61, 74, 36, 61, 23, 98, 52, 98, 98, 98,\n  52, 98, 100, 37, 98, 98, 98, 98, 52, 98, 52, 98, 48, 25, 84, 22, 27, 14, 13,\n  54, 102, 15, 50, 61, 0, 0, 41, 86, 48, 48, 48, 28, 48, 86, 33, 0, 86, 21, 86,\n  86, 48, 86, 48, 86, 39, 12, 74, 21, 23, 23, 14, 49, 52, 136, 43, 20,\n];\n// Lowercase + period kerning pairs, |k| >= 10/1000.\nconst KERN = new Map(\n  [\n    \"bv-11 bw-11 bx-12 by-11 b.-13 ff-38 fl12 fv17 fw17 fy17 f.-43 kc-15\",\n    \"kd-15 ke-15 kg-15 ko-15 kq-15 ks-10 ov-11 ow-11 ox-12 oy-11 o.-13\",\n    \"pv-11 pw-11 px-12 py-11 p.-13 qj15 rt11 rv18 rw18 ry18 r.-61 vc-11\",\n    \"vd-11 ve-11 vg-11 vo-11 vq-11 vt10 v.-37 wc-11 wd-11 we-11 wg-11 wo-11\",\n    \"wq-11 wt10 w.-37 xa-10 xc-12 xd-12 xe-12 xg-12 xo-12 xq-12 xu-12 yc-11\",\n    \"yd-11 ye-11 yg-11 yo-11 yq-11 yt10 y.-37 .c-13 .d-13 .e-13 .g-13 .o-13\",\n    \".q-13 .u-17 .v-37 .w-37 .y-37\",\n  ]\n    .join(\" \")\n    .split(\" \")\n    .map((p) => [p.slice(0, 2), Number(p.slice(2)) / 1000] as const),\n);\n// Sora's hhea box is 0.97 + 0.29 em; with lineHeight 1 the baseline sits at 0.84.\nconst TRACK = -0.03;\nconst BASELINE = 0.84;\nconst XHEIGHT = 0.534;\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\" palette (#fff4ec #ffb36b #e2457a #3d4dff), warm end at\n// the word, accent end at the pulled letter. The band carries it while pulled;\n// the snapped word inherits it. Light swaps the pale amber for one that reads.\nconst THEMES = {\n  dark: { bg: \"#0A0A0B\", fg: \"#F3F2EF\", halo: [\"#FFB36B\", \"#E2457A\"] },\n  light: { bg: \"#F5F4F0\", fg: \"#121214\", halo: [\"#E8743A\", \"#D83A71\"] },\n} as const;\n\n// Beats (frames at speed 1).\nconst GRAB = 22;\nconst DRAG_END = 56;\nconst RELEASE = 64;\n\nfunction adv(ch: string) {\n  const c = ch.charCodeAt(0);\n  return (c >= 32 && c < 127 ? ADV[c - 32] : 600) / 1000;\n}\nfunction lsb(ch: string) {\n  const c = ch.charCodeAt(0);\n  return (c >= 32 && c < 127 ? LSB[c - 32] : 50) / 1000;\n}\nfunction rsb(ch: string) {\n  const c = ch.charCodeAt(0);\n  return (c >= 32 && c < 127 ? RSB[c - 32] : 50) / 1000;\n}\n\n/** Glyph left edges and widths in em, plus the total advance. */\nfunction layout(text: string) {\n  const xs: number[] = [];\n  const ws: number[] = [];\n  let x = 0;\n  for (let i = 0; i < text.length; i++) {\n    if (i > 0) x += (KERN.get(text[i - 1] + text[i]) ?? 0) + TRACK;\n    xs.push(x);\n    ws.push(adv(text[i]));\n    x += adv(text[i]);\n  }\n  return { xs, ws, width: x };\n}\n\n/**\n * The elastic. Pulled (`s > 0`): the grabbed last glyph travels with the\n * cursor, the letters before it spread a little toward it and squash. On the\n * overshoot (`s < 0`) the whole word compresses evenly, so nothing overlaps.\n */\nfunction elastic(\n  i: number,\n  count: number,\n  xs: number[],\n  s: number,\n  sMax: number,\n) {\n  const last = count - 1;\n  const u = count > 1 ? (xs[i] - xs[0]) / Math.max(1e-3, xs[last] - xs[0]) : 1;\n  if (s >= 0) {\n    const k = s / sMax;\n    if (i === last) return { dx: s, sx: 1 + 0.07 * k, sy: 1 - 0.07 * k };\n    return {\n      dx: s * 0.16 * u ** 1.3,\n      sx: 1 + 0.05 * k * u,\n      sy: 1 - 0.05 * k * u,\n    };\n  }\n  const k = Math.max(-1, s / (sMax * 0.14));\n  return { dx: s * 0.5 * u, sx: 1 + 0.05 * k * u, sy: 1 - 0.07 * k * u };\n}\n\n/** Taffy band between the word and the pulled letter: thins and pinches as it grows. */\nfunction bandPath(\n  xL: number,\n  xR: number,\n  cy: number,\n  xh: number,\n  F: number,\n  thinAs: number,\n) {\n  const L = Math.max(0, xR - xL);\n  const thin = (1 / (1 + Math.max(0, thinAs) / (0.55 * F))) ** 0.85;\n  const a = xh * 0.5 * Math.max(0.2, thin * 0.75);\n  const b = xh * 0.5 * Math.max(0.4, 0.95 * thin ** 0.4);\n  const c = a * (0.62 + 0.38 * thin);\n  const h1 = xL + L * 0.38;\n  const h2 = xR - L * 0.38;\n  return [\n    `M${xL} ${cy - a}`,\n    `C${h1} ${cy - c} ${h2} ${cy - c} ${xR} ${cy - b}`,\n    `L${xR} ${cy + b}`,\n    `C${h2} ${cy + c} ${h1} ${cy + c} ${xL} ${cy + a}`,\n    `A${a} ${a} 0 0 1 ${xL} ${cy - a}Z`,\n  ].join(\"\");\n}\n\nfunction Cursor({ size, pressed }: { size: number; pressed: number }) {\n  return (\n    <svg\n      width={size * 0.7}\n      height={size}\n      viewBox=\"0 0 17.5 25\"\n      style={{\n        position: \"absolute\",\n        left: 0,\n        top: 0,\n        overflow: \"visible\",\n        transformOrigin: \"6% 6%\",\n        transform: `scale(${1 - 0.1 * pressed})`,\n        filter: \"drop-shadow(0 1.5px 2px rgba(0,0,0,0.32))\",\n      }}\n      aria-hidden\n    >\n      <path\n        d=\"M1.5 1.5 L1.5 20.6 L6.1 16.3 L9.1 23.2 L12.6 21.7 L9.7 14.9 L15.9 14.9 Z\"\n        fill=\"#0A0A0B\"\n        stroke=\"#FFFFFF\"\n        strokeWidth={1.4}\n        strokeLinejoin=\"round\"\n      />\n    </svg>\n  );\n}\n\nexport function RubberbandWord({\n  word = \"tension\",\n  snapWord = \"release\",\n  period = true,\n  accent = \"#3B5BFD\",\n  theme = \"dark\",\n  speed = 1,\n}: RubberbandWordProps) {\n  const frame = useCurrentFrame() * speed;\n  const { width: W, height: H, fps } = useVideoConfig();\n  const uid = useId().replace(/[^a-zA-Z0-9_-]/g, \"\");\n  const t = THEMES[theme];\n\n  const from = word.trim() || \"tension\";\n  const to = snapWord.trim() || from;\n  const swaps = to !== from;\n\n  const A = layout(from);\n  const B = layout(to);\n  const dotW = period ? adv(\".\") + TRACK : 0;\n  const longest = Math.max(A.width, B.width + dotW);\n  const F = Math.min(H * 0.27, (W * 0.54) / longest);\n  const xh = XHEIGHT * F;\n  const baseline = H * 0.5 + xh * 0.5;\n  const cy = baseline - xh * 0.5;\n  const leftA = (W - A.width * F) / 2;\n  const leftB = swaps ? (W - (B.width + dotW) * F) / 2 : leftA;\n  const sMax = W * 0.21;\n\n  // Pull: drag out, hold the tension, then let go with one damped overshoot.\n  const drag = interpolate(frame, [GRAB + 4, DRAG_END], [0, 1], {\n    ...CLAMP,\n    easing: EASE_IO,\n  });\n  const creep = interpolate(frame, [DRAG_END, RELEASE], [0, 1], {\n    ...CLAMP,\n    easing: Easing.bezier(0.22, 1, 0.36, 1),\n  });\n  const held = sMax * (0.95 * drag + 0.05 * creep);\n  const snap = spring({\n    frame: frame - RELEASE,\n    fps,\n    config: { damping: 15, stiffness: 230, mass: 1 },\n  });\n  const s = frame < RELEASE ? held : sMax * (1 - snap);\n\n  // Grab point: the last glyph's right stem, at mid x-height.\n  const lastA = from.length - 1;\n  const grabX0 =\n    leftA + (A.xs[lastA] + A.ws[lastA] - rsb(from[lastA])) * F - F * 0.05;\n  const press = interpolate(\n    frame,\n    [GRAB, GRAB + 3, RELEASE, RELEASE + 4],\n    [0, 1, 1, 0],\n    CLAMP,\n  );\n  const approach = interpolate(frame, [0, GRAB], [0, 1], {\n    ...CLAMP,\n    easing: EASE_IO,\n  });\n  const startX = W * 0.8;\n  const startY = H * 0.86;\n  const arc = Math.sin(approach * Math.PI) * H * 0.05;\n  const away = interpolate(frame, [RELEASE + 1, RELEASE + 26], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const lift = H * 0.015 * (0.95 * drag + 0.05 * creep);\n  let cx = startX + (grabX0 - startX) * approach + arc * 0.5;\n  let cyCursor = startY + (cy - startY) * approach + arc;\n  if (frame >= GRAB) {\n    cx = grabX0 + held;\n    cyCursor = cy - lift;\n  }\n  if (frame >= RELEASE) {\n    cx = grabX0 + sMax + W * 0.03 * away;\n    cyCursor = cy - H * 0.015 + H * 0.06 * away;\n  }\n  const cursorOpacity = interpolate(\n    frame,\n    [RELEASE + 8, RELEASE + 22],\n    [1, 0],\n    CLAMP,\n  );\n  const cursorSize = H * 0.085;\n\n  // The swap rides the snap: the old word squashes flat onto its baseline,\n  // the new one springs up out of that line with one overshoot.\n  const SWAP = RELEASE + 4;\n  const clipTop = baseline - F * 0.98;\n  const clipH = F * 1.24;\n  const squash = (i: number, count: number, incoming: boolean) => {\n    const start = SWAP + (count - 1 - i) * 0.5 + (incoming ? 4 : 0);\n    if (!incoming) {\n      return interpolate(frame, [start, start + 5], [1, 0], {\n        ...CLAMP,\n        easing: Easing.bezier(0.55, 0, 1, 0.45),\n      });\n    }\n    return spring({\n      frame: frame - start,\n      fps,\n      config: { damping: 12, stiffness: 240, mass: 0.8 },\n    });\n  };\n  const halo = [t.halo[0], t.halo[1], accent];\n  const wordGradient = `linear-gradient(90deg, ${halo[0]} 0%, ${halo[1]} 52%, ${halo[2]} 100%)`;\n  // The fill is a touch wider than the word and slides a few percent so the\n  // last letter settles on the accent, next to the accent period.\n  const gradW = B.width * F * 1.12;\n  const gradShift =\n    B.width *\n    F *\n    0.12 *\n    interpolate(frame, [RELEASE, RELEASE + 60], [0, 1], {\n      ...CLAMP,\n      easing: Easing.bezier(0.45, 0, 0.55, 1),\n    });\n  const accentOn = interpolate(frame, [GRAB, GRAB + 3], [0, 1], CLAMP) > 0.5;\n\n  const glyphs = (\n    text: string,\n    L: ReturnType<typeof layout>,\n    left: number,\n    incoming: boolean,\n  ) =>\n    text.split(\"\").map((ch, i) => {\n      const g = elastic(i, text.length, L.xs, s, sMax);\n      const q = swaps ? squash(i, text.length, incoming) : 1;\n      if (q <= 0.01) return null;\n      const isLast = i === text.length - 1;\n      const color =\n        !incoming && isLast && accentOn && (swaps || frame < RELEASE + 6)\n          ? accent\n          : t.fg;\n      const w = L.ws[i] * F;\n      // The snapped word wears the gradient; without a swap the word itself does.\n      const fill: React.CSSProperties =\n        incoming || (!swaps && frame >= RELEASE + 6)\n          ? {\n              color: \"transparent\",\n              backgroundImage: wordGradient,\n              backgroundRepeat: \"no-repeat\",\n              backgroundSize: `${gradW}px ${F}px`,\n              backgroundPosition: `${-L.xs[i] * F - gradShift}px 0px`,\n              WebkitBackgroundClip: \"text\",\n              backgroundClip: \"text\",\n            }\n          : { color };\n      return (\n        <span\n          key={`${uid}-${incoming ? \"b\" : \"a\"}-${i}`}\n          style={{\n            position: \"absolute\",\n            left: left + L.xs[i] * F + g.dx,\n            top: baseline - clipTop - BASELINE * F,\n            width: w,\n            fontSize: F,\n            lineHeight: 1,\n            ...fill,\n            opacity: Math.min(1, q * 6),\n            transformOrigin: `50% ${BASELINE * F}px`,\n            transform: `scale(${g.sx}, ${g.sy * q})`,\n            whiteSpace: \"pre\",\n          }}\n        >\n          {ch}\n        </span>\n      );\n    });\n\n  // Band: from just inside the previous glyph to the pulled glyph's left stem.\n  let band = \"\";\n  let bandX: [number, number] = [0, 1];\n  const bandOpacity =\n    interpolate(s, [sMax * 0.02, sMax * 0.1], [0, 1], CLAMP) *\n    interpolate(frame, [RELEASE, RELEASE + 5], [1, 0], CLAMP);\n  if (from.length > 1 && bandOpacity > 0) {\n    const prev = lastA - 1;\n    const gp = elastic(prev, from.length, A.xs, s, sMax);\n    const gl = elastic(lastA, from.length, A.xs, s, sMax);\n    const wp = A.ws[prev] * F;\n    const wl = A.ws[lastA] * F;\n    const xL =\n      leftA +\n      A.xs[prev] * F +\n      gp.dx +\n      wp / 2 +\n      (wp / 2 - rsb(from[prev]) * F) * gp.sx -\n      F * 0.1;\n    const xR =\n      leftA +\n      A.xs[lastA] * F +\n      gl.dx +\n      wl / 2 -\n      (wl / 2 - lsb(from[lastA]) * F) * gl.sx +\n      F * 0.05;\n    // After the release the band stays thin while it whips back and fades.\n    const thinAs = frame >= RELEASE ? Math.max(xR - xL, sMax * 0.8) : xR - xL;\n    band = bandPath(xL, xR, cy, xh * gl.sy, F, thinAs);\n    bandX = [xL - xh * 0.3, xR];\n  }\n\n  const dotIn = interpolate(frame, [SWAP + 10, SWAP + 20], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const lastB = to.length - 1;\n  const gB = elastic(lastB, to.length, B.xs, s, sMax);\n  const dotLeft = leftB + (B.xs[lastB] + B.ws[lastB] + TRACK) * F + gB.dx;\n\n  return (\n    <AbsoluteFill style={{ background: t.bg }}>\n      {band ? (\n        <svg\n          width={W}\n          height={H}\n          style={{ position: \"absolute\", inset: 0 }}\n          aria-hidden\n        >\n          <defs>\n            <linearGradient\n              id={`${uid}-band`}\n              gradientUnits=\"userSpaceOnUse\"\n              x1={bandX[0]}\n              y1={0}\n              x2={bandX[1]}\n              y2={0}\n            >\n              <stop offset=\"0\" stopColor={halo[0]} />\n              <stop offset=\"0.55\" stopColor={halo[1]} />\n              <stop offset=\"1\" stopColor={halo[2]} />\n            </linearGradient>\n          </defs>\n          <path d={band} fill={`url(#${uid}-band)`} opacity={bandOpacity} />\n        </svg>\n      ) : null}\n      <div\n        style={{\n          position: \"absolute\",\n          left: 0,\n          top: clipTop,\n          width: W,\n          height: clipH,\n          overflow: \"hidden\",\n          fontFamily: sans,\n          fontWeight: 500,\n        }}\n      >\n        {glyphs(from, A, leftA, false)}\n        {swaps ? glyphs(to, B, leftB, true) : null}\n        {period ? (\n          <span\n            style={{\n              position: \"absolute\",\n              left: dotLeft,\n              top: baseline - clipTop - BASELINE * F,\n              fontSize: F,\n              lineHeight: 1,\n              color: accent,\n              opacity: dotIn,\n              transformOrigin: `30% ${BASELINE * F}px`,\n              transform: `scale(${0.6 + 0.4 * dotIn})`,\n            }}\n          >\n            .\n          </span>\n        ) : null}\n      </div>\n      <div\n        style={{\n          position: \"absolute\",\n          left: cx - cursorSize * 0.06,\n          top: cyCursor - cursorSize * 0.06,\n          opacity: cursorOpacity,\n        }}\n      >\n        <Cursor size={cursorSize} pressed={press} />\n      </div>\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/rubberband-word.tsx"
    }
  ],
  "type": "registry:component"
}
