{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "fuchidori-count",
  "title": "Fuchidori Count",
  "description": "A sticker-style counter whose digits gain stacked red outline borders, landing on 4,096.",
  "dependencies": ["remotion", "@remotion/google-fonts"],
  "files": [
    {
      "path": "registry/remocn/fuchidori-count/index.tsx",
      "content": "\"use client\";\n\nimport { loadFont as loadZenKaku } from \"@remotion/google-fonts/ZenKakuGothicNew\";\nimport type React from \"react\";\nimport { useEffect, useMemo, useRef, useState } from \"react\";\nimport {\n  AbsoluteFill,\n  continueRender,\n  delayRender,\n  Easing,\n  interpolate,\n  spring,\n  useCurrentFrame,\n  useVideoConfig,\n} from \"remotion\";\n\nconst kaku = loadZenKaku(\"normal\", {\n  weights: [\"400\", \"500\"],\n  subsets: [\"latin\"],\n});\n\nexport interface FuchidoriCountProps {\n  value?: number;\n  from?: number;\n  prefix?: string;\n  suffix?: string;\n  label?: string;\n  maxBorders?: number;\n  colors?: string[];\n  accent?: string;\n  background?: string;\n  speed?: number;\n  theme?: \"dark\" | \"light\";\n}\n\nconst VERMILION = \"#E03C31\";\n\nconst THEMES = {\n  dark: { bg: \"#111111\", ink: \"#F5F1EA\" },\n  light: { bg: \"#F5F1EA\", ink: \"#111111\" },\n} as const;\n\nconst GAP = 0.024;\nconst RING = 0.04;\nconst STEP = GAP + RING;\nconst TRACK = 0.02;\n\nconst RISE_AT = 0;\nconst RISE_LEN = 14;\nconst LABEL_AT = 6;\nconst COUNT_AT = 8;\nconst COUNT_LEN = 68;\nconst RING_LEN = 12;\n\nconst EASE_OUT = Easing.bezier(0.22, 1, 0.36, 1);\nconst EASE_COUNT = Easing.bezier(0.3, 0.1, 0.3, 1);\nconst CLAMP = {\n  extrapolateLeft: \"clamp\",\n  extrapolateRight: \"clamp\",\n} as const;\n\nfunction group(n: number) {\n  const s = String(Math.max(0, Math.floor(n)));\n  let out = \"\";\n  for (let i = 0; i < s.length; i++) {\n    if (i > 0 && (s.length - i) % 3 === 0) out += \",\";\n    out += s[i];\n  }\n  return out;\n}\n\ninterface Metrics {\n  adv: Record<string, number>;\n  digit: number;\n  cap: number;\n  label: number;\n}\n\nfunction measure(chars: string[], label: string): Metrics {\n  const canvas = document.createElement(\"canvas\");\n  const ctx = canvas.getContext(\"2d\");\n  if (!ctx) throw new Error(\"fuchidori-count: no 2d context\");\n  ctx.font = `500 100px \"${kaku.fontFamily}\"`;\n  let digit = 0;\n  for (const d of \"0123456789\") {\n    digit = Math.max(digit, ctx.measureText(d).width / 100);\n  }\n  const adv: Record<string, number> = {};\n  for (const ch of chars) adv[ch] = ctx.measureText(ch).width / 100;\n  const cap = ctx.measureText(\"0\").actualBoundingBoxAscent / 100;\n  ctx.font = `400 100px \"${kaku.fontFamily}\"`;\n  return {\n    adv,\n    digit,\n    cap,\n    label: label ? ctx.measureText(label).width / 100 : 0,\n  };\n}\n\nfunction useMetrics(chars: string[], label: string) {\n  const [metrics, setMetrics] = useState<Metrics | null>(null);\n  const [handle] = useState(() => delayRender(\"fuchidori-count: measuring\"));\n  const released = useRef(false);\n  const key = chars.join(\"\");\n  useEffect(() => {\n    let live = true;\n    Promise.all([\n      kaku.waitUntilDone(),\n      document.fonts.load(`500 100px \"${kaku.fontFamily}\"`),\n      document.fonts.load(`400 100px \"${kaku.fontFamily}\"`),\n    ])\n      .catch(() => undefined)\n      .then(() => {\n        if (!live) return;\n        setMetrics(measure(Array.from(key), label));\n        if (!released.current) {\n          released.current = true;\n          continueRender(handle);\n        }\n      });\n    return () => {\n      live = false;\n    };\n  }, [key, label, handle]);\n  return metrics;\n}\n\nexport function FuchidoriCount({\n  value = 4096,\n  from = 1,\n  prefix = \"\",\n  suffix = \"\",\n  label = \"captions styled without opening an editor\",\n  maxBorders = 4,\n  colors,\n  accent,\n  background,\n  speed = 1,\n  theme = \"dark\",\n}: FuchidoriCountProps) {\n  const t = useCurrentFrame() * speed;\n  const { width: W, height: H, fps } = useVideoConfig();\n  const c = THEMES[theme];\n  const bg = background ?? c.bg;\n  const stage = bg === \"transparent\" ? c.bg : bg;\n  const red = accent ?? VERMILION;\n\n  const end = Math.max(1, Math.floor(value));\n  const start = Math.min(end, Math.max(1, Math.floor(from)));\n  const finalText = `${prefix}${group(end)}${suffix}`;\n  const chars = useMemo(\n    () => Array.from(new Set(Array.from(`${finalText}0123456789,`))),\n    [finalText],\n  );\n  const m = useMetrics(chars, label);\n\n  const lo = Math.log10(start);\n  const hi = Math.log10(end);\n  const powers: number[] = [];\n  for (let k = Math.floor(lo) + 1; k <= Math.floor(hi); k++) powers.push(k);\n  const shown = powers.slice(-Math.max(0, Math.floor(maxBorders)));\n  const N = shown.length;\n\n  if (!m) {\n    return <div style={{ position: \"absolute\", inset: 0, background: bg }} />;\n  }\n\n  const countP = interpolate(t, [COUNT_AT, COUNT_AT + COUNT_LEN], [0, 1], {\n    ...CLAMP,\n    easing: EASE_COUNT,\n  });\n  const logV = lo + (hi - lo) * countP;\n  const current = countP >= 1 ? end : Math.min(end, Math.floor(10 ** logV));\n  const text = `${prefix}${group(current)}${suffix}`;\n\n  const crossAt = (k: number) => {\n    const target = hi === lo ? 1 : (k - lo) / (hi - lo);\n    let a = 0;\n    let b = 1;\n    for (let i = 0; i < 24; i++) {\n      const mid = (a + b) / 2;\n      if (EASE_COUNT(mid) < target) a = mid;\n      else b = mid;\n    }\n    return COUNT_AT + b * COUNT_LEN;\n  };\n  const grows = shown.map((k) =>\n    spring({\n      frame: t - crossAt(k),\n      fps,\n      durationInFrames: RING_LEN,\n      config: { damping: 14, stiffness: 170, mass: 0.7 },\n    }),\n  );\n  const press = shown.reduce((acc, k) => {\n    const d = t - crossAt(k);\n    if (d < 0 || d > 10) return acc;\n    return acc + Math.sin((d / 10) * Math.PI) * 0.03 * (1 - d / 14);\n  }, 0);\n\n  const advOf = (ch: string) => (/[0-9]/.test(ch) ? m.digit : m.adv[ch]);\n  const widthOf = (s: string) => {\n    const a = Array.from(s);\n    return (\n      a.reduce((acc, ch) => acc + (advOf(ch) ?? m.digit), 0) +\n      TRACK * Math.max(0, a.length - 1)\n    );\n  };\n  const R = N * STEP;\n  const portrait = H > W * 1.1;\n  const marginX = W * (portrait ? 0.08 : 0.07);\n  const marginY = H * (portrait ? 0.07 : 0.1);\n  const labelSize = label\n    ? Math.min(Math.min(W, H) * 0.046, (H - marginY * 2) / (m.label * 1.06))\n    : 0;\n  const labelCol = label ? labelSize * 2.2 : 0;\n  const finalW = widthOf(finalText) + 2 * R;\n  const F = Math.min(\n    (W - marginX * 2 - labelCol) / finalW,\n    (H * (portrait ? 0.34 : 0.6)) / (m.cap + 2 * R),\n    H * 0.42,\n  );\n  const baseY = H - marginY - R * F;\n  const top = baseY - (R + m.cap) * F;\n\n  const glyphs = Array.from(text);\n  const curW = widthOf(text);\n  let pen = W - marginX - R * F - curW * F;\n  const placed = glyphs.map((ch, i) => {\n    const a = advOf(ch) ?? m.digit;\n    const cx = pen + (a * F) / 2;\n    pen += (a + TRACK) * F;\n    return { ch, cx, i };\n  });\n\n  const rise = interpolate(t, [RISE_AT, RISE_AT + RISE_LEN], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const riseY = (1 - rise) * F * 0.7;\n\n  const layer = (\n    key: string,\n    stroke: string | null,\n    sw: number,\n    fill: string,\n  ) => (\n    <g key={key}>\n      {placed.map((p) => (\n        <text\n          key={p.i}\n          x={p.cx}\n          y={baseY}\n          textAnchor=\"middle\"\n          strokeWidth={stroke ? sw : 0}\n          strokeLinejoin=\"round\"\n          strokeLinecap=\"round\"\n          style={{\n            fill,\n            stroke: stroke ?? \"none\",\n            fontFamily: kaku.fontFamily,\n            fontWeight: 500,\n            fontSize: F,\n          }}\n        >\n          {p.ch}\n        </text>\n      ))}\n    </g>\n  );\n\n  const rings = colors?.length ? colors : [red, c.ink];\n  const layers: React.ReactNode[] = [];\n  for (let i = N - 1; i >= 0; i--) {\n    const g = grows[i];\n    if (g <= 0.001) continue;\n    const inner = i * STEP + GAP;\n    const outer = inner + RING * g;\n    const col = rings[i % rings.length];\n    layers.push(layer(`r${i}`, col, 2 * outer * F, col));\n    layers.push(layer(`g${i}`, stage, 2 * inner * F, stage));\n  }\n  layers.push(layer(\"fill\", null, 0, c.ink));\n\n  const clipTop = top - F * 0.1;\n  const clipH = (m.cap + 2 * R) * F + F * 0.4;\n  const labelP = interpolate(t, [LABEL_AT, LABEL_AT + 18], [0, 1], {\n    ...CLAMP,\n    easing: EASE_OUT,\n  });\n  const pressX = W - marginX - R * F - (curW * F) / 2;\n\n  return (\n    <AbsoluteFill style={{ background: bg, overflow: \"hidden\" }}>\n      {label ? (\n        <div\n          style={{\n            position: \"absolute\",\n            left: marginX,\n            top: marginY,\n            width: labelSize * 1.4,\n            height: H - marginY * 2,\n            overflow: \"hidden\",\n          }}\n        >\n          <div\n            style={{\n              writingMode: \"vertical-rl\",\n              fontFamily: kaku.fontFamily,\n              fontWeight: 400,\n              fontSize: labelSize,\n              lineHeight: `${labelSize * 1.4}px`,\n              color: red,\n              whiteSpace: \"pre\",\n              transform: `translateY(${(-(1 - labelP) * (H - marginY * 2) * 0.25).toFixed(2)}px)`,\n              opacity: labelP,\n            }}\n          >\n            {label}\n          </div>\n        </div>\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        }}\n      >\n        <svg\n          width={W}\n          height={H}\n          viewBox={`0 0 ${W} ${H}`}\n          style={{\n            position: \"absolute\",\n            left: 0,\n            top: -clipTop,\n            transform: `translateY(${riseY.toFixed(2)}px) scale(${(1 + press).toFixed(4)})`,\n            transformOrigin: `${pressX.toFixed(1)}px ${baseY - (m.cap * F) / 2}px`,\n            overflow: \"visible\",\n          }}\n          aria-hidden\n        >\n          {layers}\n        </svg>\n      </div>\n    </AbsoluteFill>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/remocn/fuchidori-count.tsx"
    }
  ],
  "type": "registry:component"
}
