{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "flank-roll-swap",
  "title": "Flank Roll Swap",
  "description": "\"Out now ... on remocn.dev\" rolls its flanking words around a sunset image in the middle.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "registryDependencies": ["@remocn/gsh"],
  "files": [
    {
      "path": "registry/remocn/flank-roll-swap/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont } from \"@remotion/google-fonts/HankenGrotesk\";\nimport { useEffect, useMemo, useRef, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  Img,\n  interpolate,\n  spring,\n  staticFile,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\nimport type { GradientParams } from \"@/lib/remocn/gsh/gradient-engine\";\nimport { GshGround, gshParams } from \"@/lib/remocn/gsh/gsh-ground\";\nimport type { GshPresetId } from \"@/lib/remocn/gsh/presets\";\n\nconst hanken = loadFont(\"normal\", {\n  weights: [\"400\"],\n  subsets: [\"latin\"],\n});\n\nexport interface FlankRollSwapProps {\n  from?: [string, string];\n  to?: [string, string];\n  accentWord?: string;\n  images?: string[];\n  accent?: string;\n  ink?: string;\n  theme?: \"dark\" | \"light\";\n  ground?: GshPresetId;\n  groundParams?: Partial<GradientParams>;\n}\n\nconst LIGHT_GROUND: Partial<GradientParams> = {\n  kind: \"mist\",\n  background: \"#f1ece2\",\n  colors: [\"#fbf3dc\", \"#f4dfae\", \"#efd0c8\", \"#e6e2ea\"],\n  blur: 0.85,\n  warp: 0.3,\n  seed: 9,\n  grain: 0.16,\n};\n\nconst THEMES = {\n  dark: {\n    ink: \"#f6f1ea\",\n    accent: \"#ffb27a\",\n    ground: \"hearth\" as GshPresetId | undefined,\n    groundParams: { scale: 1 } as Partial<GradientParams> | undefined,\n    groundParamsTall: { scale: 1.7 } as Partial<GradientParams> | undefined,\n    groundOffset: 3,\n    scrim: \"10,6,4\",\n    scrimAlpha: 0.5,\n    border: \"rgba(255,255,255,0.14)\",\n    highlight: \"inset 0 1px 0 rgba(255,255,255,0.18)\",\n    shadow:\n      \"0 1px 2px rgba(6,3,1,0.45), 0 14px 30px rgba(6,3,1,0.45), 0 44px 90px rgba(6,3,1,0.55)\",\n  },\n  light: {\n    ink: \"#1b1814\",\n    accent: \"#3B5BFD\",\n    ground: undefined as GshPresetId | undefined,\n    groundParams: LIGHT_GROUND as Partial<GradientParams> | undefined,\n    groundParamsTall: LIGHT_GROUND as Partial<GradientParams> | undefined,\n    groundOffset: 2,\n    scrim: \"246,241,232\",\n    scrimAlpha: 0.45,\n    border: \"rgba(40,28,10,0.10)\",\n    highlight: \"inset 0 1px 0 rgba(255,255,255,0.4)\",\n    shadow:\n      \"0 1px 2px rgba(60,40,10,0.12), 0 12px 26px rgba(60,40,10,0.14), 0 36px 72px rgba(60,40,10,0.16)\",\n  },\n};\n\nconst DEFAULT_IMAGES = [\n  \"templates/launch-anything/desk.jpg\",\n  \"reel/reel-4.jpg\",\n  \"templates/product-showcase/sculpture.jpg\",\n  \"templates/launch-anything/horizon.jpg\",\n  \"templates/brand-guidelines/ceramics.jpg\",\n  \"reel/reel-2.jpg\",\n  \"templates/product-showcase/courtyard.jpg\",\n  \"templates/brand-guidelines/chair.jpg\",\n];\n\nconst BEAT = {\n  leftIn: 6,\n  rightIn: 9,\n  slide: 15,\n  leftRoll: 48,\n  rightGap: 6,\n  stagger: 1.2,\n  roll: 9,\n  accentLag: 3,\n  hold: 42,\n  fastFrom: 42,\n};\n\nconst FONT_PX = 200;\nconst TRACK = -0.02;\nconst LINE = 1.3;\nconst TYPE_IN = Easing.bezier(0.32, 0.72, 0, 1);\nconst ROLL = Easing.bezier(0.65, 0, 0.35, 1);\nconst PUSH = Easing.bezier(0.87, 0, 0.13, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\nconst CARD = { mass: 0.8, damping: 18, stiffness: 160 };\n\nfunction buildMetrics(_version: number) {\n  const ctx = document.createElement(\"canvas\").getContext(\"2d\");\n  const font = `400 ${FONT_PX}px \"${hanken.fontFamily}\"`;\n  const cache: Record<string, number> = {};\n  return (s: string) => {\n    if (cache[s] === undefined) {\n      if (!ctx) return s.length * 0.55;\n      ctx.font = font;\n      cache[s] =\n        ctx.measureText(s).width / FONT_PX + TRACK * Array.from(s).length;\n    }\n    return cache[s];\n  };\n}\n\nfunction useMetrics() {\n  const [version, setVersion] = useState(0);\n  const [handle] = useState(() => delayRender(\"flank-roll-swap: measuring\"));\n  const released = useRef(false);\n  useEffect(() => {\n    let live = true;\n    const bump = () => {\n      if (live) setVersion((v) => v + 1);\n    };\n    document.fonts.addEventListener(\"loadingdone\", bump);\n    Promise.all([\n      hanken.waitUntilDone(),\n      document.fonts.load(`400 ${FONT_PX}px \"${hanken.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        bump();\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n      document.fonts.removeEventListener(\"loadingdone\", bump);\n    };\n  }, [handle]);\n  return useMemo(() => buildMetrics(version), [version]);\n}\n\nconst rollEnd = (start: number, a: string, b: string) =>\n  start + Math.max(a.length, b.length) * BEAT.stagger + BEAT.roll;\n\nexport const flankRollSwapLength = (\n  from: [string, string] = [\"Remocn\", \"Studio\"],\n  to: [string, string] = [\"Out now\", \"on remocn.dev\"],\n) => {\n  const leftEnd = rollEnd(BEAT.leftRoll, from[0], to[0]);\n  const rightEnd = rollEnd(BEAT.leftRoll + BEAT.rightGap, from[1], to[1]);\n  return Math.ceil(Math.max(leftEnd, rightEnd) / 6) * 6 + BEAT.hold;\n};\n\nconst cutIndex = (frame: number, swapEnd: number) => {\n  let t = 0;\n  let k = 0;\n  while (t <= frame) {\n    const step = t < BEAT.fastFrom ? 6 : t < swapEnd ? 4 : 9;\n    t += step;\n    if (t <= frame) k++;\n  }\n  return k;\n};\n\nexport function FlankRollSwap({\n  from = [\"Remocn\", \"Studio\"],\n  to = [\"Out now\", \"on remocn.dev\"],\n  accentWord = \"remocn.dev\",\n  images,\n  accent,\n  ink,\n  theme = \"dark\",\n  ground,\n  groundParams,\n}: FlankRollSwapProps) {\n  const frame = useCurrentFrame();\n  const { width: W, height: H, fps } = useVideoConfig();\n  const c = THEMES[theme];\n  const inkC = ink ?? c.ink;\n  const hi = accent ?? c.accent;\n  const preset = ground ?? (groundParams ? undefined : c.ground);\n  const portrait = H > W * 1.1;\n  const gParams =\n    groundParams ??\n    (ground ? undefined : portrait ? c.groundParamsTall : c.groundParams);\n  const bg = gshParams(preset, gParams).background;\n  const u = Math.min(W, H) / 720;\n  const measure = useMetrics();\n  const pics = (images ?? DEFAULT_IMAGES.map((p) => staticFile(p))).filter(\n    Boolean,\n  );\n\n  const leftStart = BEAT.leftRoll;\n  const rightStart = BEAT.leftRoll + BEAT.rightGap;\n  const leftEnd = rollEnd(leftStart, from[0], to[0]);\n  const rightEnd = rollEnd(rightStart, from[1], to[1]);\n  const swapEnd = Math.max(leftEnd, rightEnd);\n\n  const geo = useMemo(() => {\n    if (portrait) {\n      const widest = Math.max(\n        measure(from[0]),\n        measure(from[1]),\n        measure(to[0]),\n        measure(to[1]),\n      );\n      const cardW = W * 0.74;\n      const cardH = cardW * 1.18;\n      const F = Math.min((W * 0.8) / widest, cardH * 0.24);\n      return { cardW, cardH, F, gap: F * 0.18, cx: W / 2 };\n    }\n    const leftMax = Math.max(measure(from[0]), measure(to[0]));\n    const rightMax = Math.max(measure(from[1]), measure(to[1]));\n    const gapEm = 0.42;\n    const sumEm = leftMax + rightMax + 2 * gapEm;\n    const cardW = W * Math.min(0.3, Math.max(0.2, 0.86 / (1 + 0.16 * sumEm)));\n    const cardH = cardW / 1.9;\n    const F = Math.min((W * 0.86 - cardW) / sumEm, cardH * 0.42);\n    const cx = W / 2 + ((measure(to[0]) - measure(to[1])) * F) / 2;\n    return { cardW, cardH, F, gap: F * gapEm, cx };\n  }, [measure, from, to, portrait, W]);\n\n  const { cardW, cardH, F, gap, cx } = geo;\n  const cy = H / 2;\n\n  const pop = spring({ frame, fps, config: CARD });\n  const punch = interpolate(\n    frame,\n    [leftStart, leftStart + 2, leftStart + 7],\n    [0, 1, 0],\n    CLAMP,\n  );\n  const push =\n    1 +\n    0.035 *\n      interpolate(frame, [0, swapEnd + BEAT.hold - 12], [0, 1], {\n        ...CLAMP,\n        easing: PUSH,\n      });\n  const cardScale = (0.55 + 0.45 * pop) * (1 + 0.035 * punch);\n  const tilt = (1 - pop) * 14;\n  const cut = cutIndex(frame, swapEnd);\n  const pic = pics[cut % Math.max(1, pics.length)];\n  let cutStart = 0;\n  {\n    let t = 0;\n    while (t <= frame) {\n      cutStart = t;\n      t += t < BEAT.fastFrom ? 6 : t < swapEnd ? 4 : 9;\n    }\n  }\n  const kb =\n    1.08 -\n    0.08 *\n      interpolate(frame - cutStart, [0, 9], [0, 1], {\n        ...CLAMP,\n        easing: TYPE_IN,\n      });\n\n  const accentAt = (word: string) => {\n    const i = accentWord ? word.indexOf(accentWord) : -1;\n    return i < 0 ? [-1, -1] : [i, i + accentWord.length];\n  };\n\n  const renderRow = (\n    text: string,\n    phase: \"old\" | \"new\",\n    side: \"a\" | \"b\",\n    start: number,\n    landed: number,\n  ) => {\n    const chars = Array.from(text);\n    const n = chars.length;\n    const [a0, a1] = phase === \"new\" ? accentAt(text) : [-1, -1];\n    const accentP =\n      a0 >= 0\n        ? interpolate(frame, [landed, landed + 9], [0, 1], {\n            ...CLAMP,\n            easing: TYPE_IN,\n          })\n        : 0;\n    return chars.map((ch, i) => {\n      const fromCard = portrait ? i : side === \"a\" ? n - 1 - i : i;\n      const t0 = start + fromCard * BEAT.stagger;\n      const p = interpolate(frame, [t0, t0 + BEAT.roll], [0, 1], {\n        ...CLAMP,\n        easing: ROLL,\n      });\n      const y = phase === \"old\" ? -p * 105 : (1 - p) * 105;\n      const op =\n        phase === \"old\"\n          ? interpolate(p, [0.4, 1], [1, 0], CLAMP)\n          : interpolate(p, [0, 0.6], [0, 1], CLAMP);\n      const isAccent = i >= a0 && i < a1;\n      const color =\n        isAccent && accentP > 0\n          ? accentP >= 1\n            ? hi\n            : `color-mix(in oklab, ${hi} ${accentP * 100}%, ${inkC})`\n          : inkC;\n      const velocity = Math.abs(\n        p -\n          interpolate(frame - 1, [t0, t0 + BEAT.roll], [0, 1], {\n            ...CLAMP,\n            easing: ROLL,\n          }),\n      );\n      const blur = Math.min(4, velocity * 14) * u;\n      return (\n        <span\n          key={`${phase}-${i}-${ch}`}\n          style={{\n            display: \"inline-block\",\n            whiteSpace: \"pre\",\n            transform: `translateY(${y}%)`,\n            opacity: op,\n            color,\n            filter: blur > 0.2 ? `blur(${blur * 0.4}px)` : undefined,\n          }}\n        >\n          {ch}\n        </span>\n      );\n    });\n  };\n\n  const renderSide = (side: \"a\" | \"b\") => {\n    const idx = side === \"a\" ? 0 : 1;\n    const inAt = side === \"a\" ? BEAT.leftIn : BEAT.rightIn;\n    const rollAt = side === \"a\" ? leftStart : rightStart;\n    const landed = side === \"a\" ? leftEnd : rightEnd;\n    const slide = interpolate(frame, [inAt, inAt + BEAT.slide], [0, 1], {\n      ...CLAMP,\n      easing: TYPE_IN,\n    });\n    const lineH = F * LINE;\n    const justify = portrait\n      ? \"center\"\n      : side === \"a\"\n        ? \"flex-end\"\n        : \"flex-start\";\n    const textStyle = {\n      position: \"absolute\" as const,\n      inset: 0,\n      display: \"flex\",\n      justifyContent: justify,\n      fontFamily: hanken.fontFamily,\n      fontWeight: 400,\n      fontSize: F,\n      letterSpacing: `${TRACK}em`,\n      lineHeight: `${lineH}px`,\n      whiteSpace: \"pre\" as const,\n    };\n    const rows = (\n      <>\n        <div style={textStyle}>\n          {renderRow(from[idx], \"old\", side, rollAt, landed)}\n        </div>\n        <div style={textStyle}>\n          {renderRow(to[idx], \"new\", side, rollAt + 1, landed)}\n        </div>\n      </>\n    );\n    if (portrait) {\n      const top = side === \"a\";\n      const edge = (cardH / 2) * Math.min(1, cardScale);\n      const travel = (1 - slide) * (lineH + gap + cardH / 2 - edge);\n      const clipTop = top ? 0 : cy + edge;\n      const rowTop = top\n        ? cy - cardH / 2 - gap - lineH\n        : cy + cardH / 2 + gap - clipTop;\n      return (\n        <div\n          style={{\n            position: \"absolute\",\n            left: 0,\n            top: clipTop,\n            width: W,\n            height: top ? cy - edge : H - clipTop,\n            overflow: \"hidden\",\n          }}\n        >\n          <div\n            style={{\n              position: \"absolute\",\n              left: 0,\n              width: W,\n              top: rowTop + (top ? travel : -travel),\n              height: lineH,\n              overflow: \"hidden\",\n            }}\n          >\n            {rows}\n          </div>\n        </div>\n      );\n    }\n    const left = side === \"a\";\n    const edge = (cardW / 2) * Math.min(1, cardScale);\n    const shift =\n      (1 - slide) * (measure(from[idx]) * F + gap + cardW / 2 - edge);\n    const inner = cardW / 2 + gap - shift - edge;\n    return (\n      <div\n        style={{\n          position: \"absolute\",\n          top: cy - lineH / 2,\n          height: lineH,\n          left: left ? 0 : cx + edge,\n          width: left ? cx - edge : W - cx - edge,\n          overflow: \"hidden\",\n        }}\n      >\n        <div\n          style={{\n            position: \"absolute\",\n            top: 0,\n            height: lineH,\n            width: W * 0.6,\n            ...(left ? { right: inner } : { left: inner }),\n          }}\n        >\n          {rows}\n        </div>\n      </div>\n    );\n  };\n\n  return (\n    <AbsoluteFill style={{ background: bg, overflow: \"hidden\" }}>\n      <GshGround\n        preset={preset}\n        params={gParams}\n        timeOffset={c.groundOffset}\n        timeScale={0.5}\n      />\n      <AbsoluteFill\n        style={{\n          background: `radial-gradient(${portrait ? \"95% 50%\" : \"70% 60%\"} at 50% 50%, rgba(${c.scrim},${c.scrimAlpha}) 0%, rgba(${c.scrim},0) 100%)`,\n        }}\n      />\n      <AbsoluteFill\n        style={{ transform: `scale(${push})`, transformOrigin: \"50% 50%\" }}\n      >\n        {renderSide(\"a\")}\n        {renderSide(\"b\")}\n        <div\n          style={{\n            position: \"absolute\",\n            left: cx - cardW / 2,\n            top: cy - cardH / 2,\n            width: cardW,\n            height: cardH,\n            perspective: 1200 * u,\n          }}\n        >\n          <div\n            style={{\n              position: \"absolute\",\n              inset: 0,\n              borderRadius: Math.max(6, cardW * (portrait ? 0.045 : 0.035)),\n              overflow: \"hidden\",\n              boxShadow: `${c.highlight}, ${c.shadow}`,\n              border: `1px solid ${c.border}`,\n              transform: `scale(${cardScale}) rotateX(${tilt}deg)`,\n              background: \"#111\",\n            }}\n          >\n            {pic ? (\n              <Img\n                src={pic}\n                style={{\n                  width: \"100%\",\n                  height: \"100%\",\n                  objectFit: \"cover\",\n                  transform: `scale(${kb})`,\n                }}\n              />\n            ) : null}\n            <div\n              style={{\n                position: \"absolute\",\n                inset: 0,\n                borderRadius: \"inherit\",\n                boxShadow: c.highlight,\n              }}\n            />\n          </div>\n        </div>\n      </AbsoluteFill>\n    </AbsoluteFill>\n  );\n}\n\nexport default FlankRollSwap;\n",
      "type": "registry:component",
      "target": "components/remocn/flank-roll-swap.tsx"
    }
  ],
  "type": "registry:component"
}
