{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "glass-capsule",
  "title": "Glass Capsule",
  "description": "A glass capsule slides over the focus word of a two-line headline, magnifying it in italic.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/glass-capsule/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont as loadSans } from \"@remotion/google-fonts/InstrumentSans\";\nimport { loadFont as loadSerif } from \"@remotion/google-fonts/InstrumentSerif\";\nimport type React from \"react\";\nimport { Fragment, useEffect, useId, useMemo, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  interpolate,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst SANS = loadSans(\"normal\", { weights: [\"500\"], subsets: [\"latin\"] });\nconst SERIF = loadSerif(\"italic\", { weights: [\"400\"], subsets: [\"latin\"] });\n\n/* Lines are Instrument Sans 500; the focus word is Instrument Serif italic. */\nconst SANS_TRACK = -0.035;\nconst SERIF_TRACK = -0.01;\nconst SANS_FONT = (px: number) => `500 ${px}px \"${SANS.fontFamily}\"`;\nconst SERIF_FONT = (px: number) => `italic 400 ${px}px \"${SERIF.fontFamily}\"`;\n\n/* gradients.sh \"silk\" palette, as the clip-text fill the focus word lights up into. */\nconst SILK = [\"#3b3fd8\", \"#f3d6c0\", \"#8a7bff\"] as const;\n\nfunction silkFill(theme: \"dark\" | \"light\", d: number) {\n  const [c0, c1, c2] = SILK;\n  const o = (d * 18).toFixed(1);\n  return theme === \"dark\"\n    ? `linear-gradient(100deg, ${c2} ${o}%, ${c1} ${(52 + d * 10).toFixed(1)}%, ${c2} ${(104 + d * 10).toFixed(1)}%)`\n    : `linear-gradient(100deg, ${c0} ${o}%, ${c2} ${(56 + d * 10).toFixed(1)}%, ${c0} ${(108 + d * 10).toFixed(1)}%)`;\n}\n\nexport interface GlassCapsuleProps {\n  lines?: string[];\n  focus?: string;\n  accent?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst EASE_IO = Easing.bezier(0.65, 0, 0.35, 1);\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_GLIDE = Easing.bezier(0.33, 0, 0.12, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nconst THEMES = {\n  dark: {\n    bg: \"#0A0A0B\",\n    fg: \"#F3F2EF\",\n    inner: \"#0A0A0B\",\n    tint: \"rgba(255,255,255,0.03)\",\n    rimHi: 0.9,\n    rimLo: 0.24,\n    shadow: \"rgba(0,0,0,0.75)\",\n    edgeLine: \"rgba(255,255,255,0.06)\",\n    innerShade: \"rgba(0,0,0,0.45)\",\n    sheen: 0.12,\n    grain: 0.06,\n  },\n  light: {\n    bg: \"#F5F4F0\",\n    fg: \"#121214\",\n    inner: \"#F5F4F0\",\n    tint: \"rgba(255,255,255,0.08)\",\n    rimHi: 1,\n    rimLo: 0.5,\n    shadow: \"rgba(60,52,40,0.26)\",\n    edgeLine: \"rgba(18,18,20,0.08)\",\n    innerShade: \"rgba(60,52,40,0.16)\",\n    sheen: 0.5,\n    grain: 0.045,\n  },\n} as const;\n\ntype Palette = (typeof THEMES)[keyof typeof THEMES];\n\nlet measureCtx: CanvasRenderingContext2D | null = null;\n\n/** (ascent - descent) in em of the face's line metrics, for baseline matching. */\nfunction emLift(serif: boolean, ready: boolean) {\n  if (!ready || typeof document === \"undefined\") return 0;\n  if (!measureCtx)\n    measureCtx = document.createElement(\"canvas\").getContext(\"2d\");\n  if (!measureCtx) return 0;\n  measureCtx.font = serif ? SERIF_FONT(100) : SANS_FONT(100);\n  const m = measureCtx.measureText(\"Hg\");\n  return (m.fontBoundingBoxAscent - m.fontBoundingBoxDescent) / 100;\n}\n\n/** Advance width in em of `s` (tracking included); a rough table until fonts load. */\nfunction emWidth(s: string, serif: boolean, ready: boolean) {\n  const track = serif ? SERIF_TRACK : SANS_TRACK;\n  if (ready && typeof document !== \"undefined\") {\n    if (!measureCtx)\n      measureCtx = document.createElement(\"canvas\").getContext(\"2d\");\n    if (measureCtx) {\n      measureCtx.font = serif ? SERIF_FONT(100) : SANS_FONT(100);\n      return measureCtx.measureText(s).width / 100 + track * [...s].length;\n    }\n  }\n  let w = 0;\n  for (const ch of s) {\n    if (ch === \" \") w += 0.24;\n    else if (\"iljtfr,.;:!'|\".includes(ch)) w += 0.28;\n    else if (\"mwMW\".includes(ch)) w += 0.8;\n    else if (ch >= \"A\" && ch <= \"Z\") w += 0.64;\n    else w += serif ? 0.44 : 0.54;\n    w += track;\n  }\n  return w;\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={29}\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 lensMap(w: number, h: number, edge: number) {\n  const r = h / 2;\n  const iw = Math.max(1, w - edge * 2);\n  const ih = Math.max(1, h - edge * 2);\n  const svg = `<svg xmlns='http://www.w3.org/2000/svg' width='${w}' height='${h}'><defs><linearGradient id='x'><stop offset='0' stop-color='#000'/><stop offset='1' stop-color='#f00'/></linearGradient><linearGradient id='y' x2='0' y2='1'><stop offset='0' stop-color='#000'/><stop offset='1' stop-color='#0f0'/></linearGradient><filter id='b' x='-50%' y='-50%' width='200%' height='200%'><feGaussianBlur stdDeviation='${edge * 0.5}'/></filter></defs><rect width='${w}' height='${h}' rx='${r}' fill='url(#x)'/><rect width='${w}' height='${h}' rx='${r}' fill='url(#y)' style='mix-blend-mode:screen'/><rect x='${edge}' y='${edge}' width='${iw}' height='${ih}' rx='${Math.max(0, r - edge)}' fill='rgb(128,128,0)' filter='url(#b)'/></svg>`;\n  return `data:image/svg+xml;utf8,${encodeURIComponent(svg)}`;\n}\n\nfunction splitFocus(lines: string[], focus: string) {\n  const clean = (w: string) => w.replace(/[^\\p{L}\\p{N}]/gu, \"\").toLowerCase();\n  const target = clean(focus);\n  for (let li = lines.length - 1; li >= 0; li--) {\n    const words = lines[li].split(\" \");\n    const wi = words.findIndex((w) => clean(w) === target);\n    if (wi >= 0) {\n      const word = words[wi];\n      const before = [...words.slice(0, wi), \"\"].join(\" \");\n      const after =\n        wi < words.length - 1 ? [\"\", ...words.slice(wi + 1)].join(\" \") : \"\";\n      return { line: li, before, word, after };\n    }\n  }\n  const li = lines.length - 1;\n  const words = lines[li].split(\" \");\n  const last = words[words.length - 1];\n  return {\n    line: li,\n    before: [...words.slice(0, -1), \"\"].join(\" \"),\n    word: last,\n    after: \"\",\n  };\n}\n\ninterface Seg {\n  before: string;\n  word: string;\n  after: string;\n}\n\n/** The focus line laid out around its word: `left: 0` is the word's centre. */\nfunction AnchoredLine({\n  seg,\n  color,\n  wordStyle,\n  wordNode,\n  style,\n  room,\n  beforeW,\n  wordW,\n}: {\n  wordNode: React.ReactNode;\n  seg: Seg;\n  color: string;\n  wordStyle: React.CSSProperties;\n  style: React.CSSProperties;\n  room: number;\n  beforeW: number;\n  wordW: number;\n}) {\n  return (\n    <div\n      style={{\n        position: \"absolute\",\n        left: -(beforeW + wordW / 2),\n        top: 0,\n        display: \"flex\",\n        color,\n      }}\n    >\n      <span style={{ ...style, transform: `translateX(${-room}px)` }}>\n        {seg.before}\n      </span>\n      <span style={wordStyle}>{wordNode}</span>\n      <span style={{ ...style, transform: `translateX(${room}px)` }}>\n        {seg.after}\n      </span>\n    </div>\n  );\n}\n\nfunction Capsule({\n  t,\n  seg,\n  lw,\n  lh,\n  lineH,\n  offset,\n  mag,\n  disp,\n  wordStyle,\n  wordNode,\n  textStyle,\n  p,\n  u,\n  enter,\n  room,\n  beforeW,\n  wordW,\n}: {\n  wordNode: React.ReactNode;\n  room: number;\n  beforeW: number;\n  wordW: number;\n  t: number;\n  seg: Seg;\n  lw: number;\n  lh: number;\n  lineH: number;\n  offset: number;\n  mag: number;\n  disp: number;\n  wordStyle: React.CSSProperties;\n  textStyle: React.CSSProperties;\n  p: Palette;\n  u: number;\n  enter: number;\n}) {\n  const fid = `cap${useId().replace(/[^a-zA-Z0-9_-]/g, \"\")}`;\n  const radius = lh / 2;\n  const edge = lh * 0.26;\n  const M = Math.ceil(disp + 8 * u);\n  const map = lensMap(Math.round(lw), Math.round(lh), edge);\n  const rimX = 24 + interpolate(t, [10, 80], [0, 14], CLAMP);\n  return (\n    <div\n      style={{\n        position: \"absolute\",\n        left: \"50%\",\n        top: (lineH - lh) / 2,\n        width: lw,\n        height: lh,\n        transform: `translateX(${-lw / 2 + offset}px)`,\n        opacity: enter,\n      }}\n    >\n      <svg width={0} height={0} style={{ position: \"absolute\" }} aria-hidden>\n        <defs>\n          <filter\n            id={fid}\n            x={0}\n            y={0}\n            width={lw + M * 2}\n            height={lh + M * 2}\n            filterUnits=\"userSpaceOnUse\"\n            primitiveUnits=\"userSpaceOnUse\"\n            colorInterpolationFilters=\"sRGB\"\n          >\n            <feImage\n              href={map}\n              x={M}\n              y={M}\n              width={lw}\n              height={lh}\n              preserveAspectRatio=\"none\"\n              result=\"map\"\n            />\n            {[\n              [\"r\", 1, \"1 0 0 0 0  0 0 0 0 0  0 0 0 0 0  0 0 0 1 0\"],\n              [\"g\", 0.94, \"0 0 0 0 0  0 1 0 0 0  0 0 0 0 0  0 0 0 1 0\"],\n              [\"b\", 0.88, \"0 0 0 0 0  0 0 0 0 0  0 0 1 0 0  0 0 0 1 0\"],\n            ].map(([k, s, m]) => (\n              <Fragment key={k as string}>\n                <feDisplacementMap\n                  in=\"SourceGraphic\"\n                  in2=\"map\"\n                  scale={disp * (s as number)}\n                  xChannelSelector=\"R\"\n                  yChannelSelector=\"G\"\n                  result={`d${k}`}\n                />\n                <feColorMatrix\n                  in={`d${k}`}\n                  type=\"matrix\"\n                  values={m as string}\n                  result={`c${k}`}\n                />\n              </Fragment>\n            ))}\n            <feComposite\n              in=\"cr\"\n              in2=\"cg\"\n              operator=\"arithmetic\"\n              k2={1}\n              k3={1}\n              result=\"rg\"\n            />\n            <feComposite in=\"rg\" in2=\"cb\" operator=\"arithmetic\" k2={1} k3={1} />\n          </filter>\n        </defs>\n      </svg>\n      <div\n        style={{\n          position: \"absolute\",\n          inset: 0,\n          borderRadius: radius,\n          boxShadow: `0 0 0 1px ${p.edgeLine}, 0 ${20 * u}px ${40 * u}px -${12 * u}px ${p.shadow}, 0 ${4 * u}px ${10 * u}px -${3 * u}px ${p.shadow}`,\n        }}\n      />\n      <div\n        style={{\n          position: \"absolute\",\n          inset: 0,\n          borderRadius: radius,\n          overflow: \"hidden\",\n          isolation: \"isolate\",\n        }}\n      >\n        <div\n          style={{\n            position: \"absolute\",\n            left: -M,\n            top: -M,\n            width: lw + M * 2,\n            height: lh + M * 2,\n            filter: `url(#${fid})`,\n          }}\n        >\n          <div\n            style={{\n              position: \"absolute\",\n              inset: 0,\n              background: p.inner,\n            }}\n          />\n          <div\n            style={{\n              position: \"absolute\",\n              inset: 0,\n              transform: `scale(${mag})`,\n              transformOrigin: \"50% 50%\",\n            }}\n          >\n            <div\n              style={{\n                position: \"absolute\",\n                left: M + lw / 2 - offset,\n                top: M + (lh - lineH) / 2,\n              }}\n            >\n              <AnchoredLine\n                seg={seg}\n                color={p.fg}\n                wordStyle={wordStyle}\n                wordNode={wordNode}\n                style={textStyle}\n                room={room}\n                beforeW={beforeW}\n                wordW={wordW}\n              />\n            </div>\n          </div>\n        </div>\n        <div style={{ position: \"absolute\", inset: 0, background: p.tint }} />\n        <div\n          style={{\n            position: \"absolute\",\n            inset: 0,\n            borderRadius: radius,\n            boxShadow: `inset 0 ${-8 * u}px ${18 * u}px -${6 * u}px ${p.innerShade}, inset 0 0 ${10 * u}px ${p.innerShade}`,\n          }}\n        />\n        <div\n          style={{\n            position: \"absolute\",\n            left: radius * 0.4,\n            right: radius * 0.4,\n            top: 2 * u,\n            height: lh * 0.4,\n            borderRadius: `${radius}px ${radius}px ${radius * 0.6}px ${radius * 0.6}px`,\n            background: `linear-gradient(180deg, rgba(255,255,255,${p.sheen}), rgba(255,255,255,0.02) 60%, rgba(255,255,255,0) 100%)`,\n          }}\n        />\n        <div\n          style={{\n            position: \"absolute\",\n            inset: 0,\n            borderRadius: radius,\n            padding: 1.5 * u,\n            background: `radial-gradient(ellipse 40% 70% at ${rimX}% 0%, rgba(255,255,255,${p.rimHi}), transparent 100%), radial-gradient(ellipse 36% 60% at ${100 - rimX}% 100%, rgba(255,255,255,${p.rimLo}), transparent 100%), rgba(255,255,255,${p.rimLo * 0.3})`,\n            WebkitMask:\n              \"linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)\",\n            WebkitMaskComposite: \"xor\",\n            maskComposite: \"exclude\",\n          }}\n        />\n        <div\n          style={{\n            position: \"absolute\",\n            inset: 0,\n            borderRadius: radius,\n            boxShadow: `inset 0 0 0 1px rgba(255,255,255,${p.rimLo * 0.5})`,\n          }}\n        />\n      </div>\n    </div>\n  );\n}\n\nexport function GlassCapsule({\n  lines = [\"Launch films\", \"that move.\"],\n  focus = \"move\",\n  accent = \"#3B5BFD\",\n  speed = 1,\n  theme = \"dark\",\n}: GlassCapsuleProps) {\n  const t = useCurrentFrame() * speed;\n  const { width, height, durationInFrames } = useVideoConfig();\n  const p = THEMES[theme];\n  const u = height / 720;\n\n  const [ready, setReady] = useState(false);\n  const [handle] = useState(() => delayRender(\"glass-capsule: fonts\"));\n  useEffect(() => {\n    let alive = true;\n    const done = () => {\n      if (alive) setReady(true);\n    };\n    Promise.all([SANS.waitUntilDone(), SERIF.waitUntilDone()])\n      .then(() =>\n        typeof document !== \"undefined\" && document.fonts\n          ? Promise.all([\n              document.fonts.load(SANS_FONT(100)),\n              document.fonts.load(SERIF_FONT(100)),\n            ])\n          : undefined,\n      )\n      .then(done, done);\n    return () => {\n      alive = false;\n    };\n  }, []);\n  useEffect(() => {\n    if (ready) continueRender(handle);\n  }, [ready, handle]);\n\n  const seg = splitFocus(lines, focus);\n  const em = useMemo(() => {\n    const lineEm = lines.map((l, i) =>\n      i === seg.line\n        ? emWidth(seg.before, false, ready) +\n          emWidth(seg.word, true, ready) +\n          emWidth(seg.after, false, ready)\n        : emWidth(l, false, ready),\n    );\n    return {\n      lines: lineEm,\n      before: emWidth(seg.before, false, ready),\n      word: emWidth(seg.word, true, ready),\n      // Serif baseline offset vs the sans baseline, both on the same line-height.\n      drop: (emLift(false, ready) - emLift(true, ready)) / 2,\n    };\n  }, [lines, seg.line, seg.before, seg.word, seg.after, ready]);\n\n  const longest = Math.max(...em.lines, 0.1);\n  const size = Math.min(\n    176 * u,\n    (width * 0.8) / longest,\n    (height * 0.62) / (lines.length * 1.04),\n  );\n  const lineH = size * 1.06;\n  const blockH = lines.length * lineH;\n  const top = (height - blockH) / 2;\n\n  const wordW = em.word * size;\n  const beforeW = em.before * size;\n  const lineW = em.lines[seg.line] * size;\n  const wordCenter = (width - lineW) / 2 + beforeW + wordW / 2;\n  const restMag = 1.1;\n  const padX = (wordW * (restMag - 1)) / 2 + size * 0.24;\n  const lw = wordW + padX * 2;\n  const lh = size * 1.0;\n\n  const glide = EASE_GLIDE(interpolate(t, [10, 74], [0, 1], CLAMP));\n  const start = -(wordCenter + lw / 2 + 40 * u);\n  const settle =\n    t > 74\n      ? Math.sin(((t - 74) / 16) * Math.PI) *\n        Math.exp(-(t - 74) / 9) *\n        5 *\n        u *\n        (t < 106 ? 1 : 0)\n      : 0;\n  const offset = start * (1 - glide) + settle;\n  const travel = 1 - glide;\n  const mag =\n    1.05 + (restMag - 1.05) * EASE_IO(interpolate(t, [52, 80], [0, 1], CLAMP));\n  const disp = (18 + 16 * travel) * u;\n  const enter = interpolate(t, [8, 14], [0, 1], CLAMP);\n  const lit = interpolate(t, [72, 90], [0, 1], { ...CLAMP, easing: EASE_OUT });\n  const sheen = interpolate(\n    t,\n    [72, Math.max(73, durationInFrames)],\n    [0, 1],\n    CLAMP,\n  );\n\n  const room =\n    Math.max(0, padX - size * 0.15) *\n    EASE_IO(interpolate(t, [50, 80], [0, 1], CLAMP));\n  const shiftLine = seg.after ? 0 : room / 2;\n  const rise = (d: number) =>\n    interpolate(t, [d, d + 20], [0, 1], { ...CLAMP, easing: EASE_OUT });\n\n  const textStyle: React.CSSProperties = {\n    fontFamily: SANS.fontFamily,\n    fontWeight: 500,\n    fontSize: size,\n    letterSpacing: `${SANS_TRACK}em`,\n    lineHeight: `${lineH}px`,\n    height: lineH,\n    whiteSpace: \"pre\",\n  };\n  // The word lights from ink into the silk gradient; the accent marks its full stop.\n  const wordStyle: React.CSSProperties = {\n    ...textStyle,\n    fontFamily: SERIF.fontFamily,\n    fontStyle: \"italic\",\n    fontWeight: 400,\n    letterSpacing: `${SERIF_TRACK}em`,\n    paddingRight: size * 0.06,\n    marginRight: -size * 0.06,\n    display: \"inline-block\",\n    transform: `translateY(${(em.drop * size).toFixed(2)}px)`,\n    backgroundImage: silkFill(theme, sheen),\n    WebkitBackgroundClip: \"text\",\n    backgroundClip: \"text\",\n    color: \"transparent\",\n    WebkitTextFillColor: `color-mix(in oklab, ${p.fg} ${((1 - lit) * 100).toFixed(1)}%, transparent)`,\n  };\n  const stop = /[.!?…]$/.test(seg.word);\n  const wordNode = (\n    <>\n      {stop ? seg.word.slice(0, -1) : seg.word}\n      {stop ? (\n        <span\n          style={{\n            WebkitTextFillColor: `color-mix(in oklab, ${accent} ${(lit * 100).toFixed(1)}%, ${p.fg})`,\n          }}\n        >\n          {seg.word.slice(-1)}\n        </span>\n      ) : null}\n    </>\n  );\n\n  return (\n    <AbsoluteFill style={{ background: p.bg, overflow: \"hidden\" }}>\n      <div\n        style={{\n          position: \"absolute\",\n          left: 0,\n          top,\n          width,\n          height: blockH,\n        }}\n      >\n        {lines.map((line, i) => {\n          const r = rise(i * 4);\n          const isFocus = i === seg.line;\n          return (\n            <div\n              key={`${i}-${line}`}\n              style={{\n                position: \"absolute\",\n                left: 0,\n                top: i * lineH,\n                width,\n                height: lineH,\n                display: \"flex\",\n                justifyContent: \"center\",\n                color: p.fg,\n                opacity: r,\n                transform: `translateY(${(1 - r) * 26 * u}px)`,\n              }}\n            >\n              {isFocus ? (\n                <>\n                  <span\n                    style={{\n                      ...textStyle,\n                      transform: `translateX(${shiftLine - room}px)`,\n                    }}\n                  >\n                    {seg.before}\n                  </span>\n                  <span\n                    style={{\n                      position: \"relative\",\n                      zIndex: 1,\n                      height: lineH,\n                      transform: `translateX(${shiftLine}px)`,\n                    }}\n                  >\n                    <span style={wordStyle}>{wordNode}</span>\n                    <Capsule\n                      t={t}\n                      seg={seg}\n                      lw={lw}\n                      lh={lh}\n                      lineH={lineH}\n                      offset={offset}\n                      mag={mag}\n                      disp={disp}\n                      wordStyle={wordStyle}\n                      wordNode={wordNode}\n                      textStyle={textStyle}\n                      p={p}\n                      u={u}\n                      enter={enter}\n                      room={room}\n                      beforeW={beforeW}\n                      wordW={wordW}\n                    />\n                  </span>\n                  <span\n                    style={{\n                      ...textStyle,\n                      transform: `translateX(${shiftLine + room}px)`,\n                    }}\n                  >\n                    {seg.after}\n                  </span>\n                </>\n              ) : (\n                <span style={textStyle}>{line}</span>\n              )}\n            </div>\n          );\n        })}\n      </div>\n      <Grain opacity={p.grain} />\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/glass-capsule.tsx"
    }
  ],
  "type": "registry:component"
}
