{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ascii-rig",
  "title": "ASCII Rig",
  "description": "Shared ASCII kit — luminance ramps, offscreen/reduced-motion aware tick loop, canvas character-cell metrics.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/default/lib/ascii-rig.ts",
      "content": "/**\n * ASCII rig — shared machinery for the ascii-* family.\n *\n * Characters are pixels. One place for:\n * - luminance ramps (dim → bright, ordered for direct indexing)\n * - one rAF tick loop that pauses offscreen and honors reduced motion\n * - canvas cell metrics so effects draw at exact character-cell width\n *\n * No dependencies. Monospace cells are 1:2 (w:h) unless a component opts out.\n */\n\nimport * as React from \"react\"\n\n/** Dim → bright ramps. Index by normalized value 0..len-1. */\nexport const ASCII_RAMPS = {\n  /** The classic donut.c ramp — thin for highlights, dense for body. */\n  classic: \".,-~:;=!*#$@\",\n  /** Even ladder, good for field effects. */\n  ladder: \" `./:;+*xOX#\",\n  /** Half blocks — flat bands, minimal texture. */\n  blocks: \"░▒▓█\",\n  /** Newskool scene look — chunky, high contrast. */\n  newskool: \"▓$#Xxo\",\n} as const\n\nexport type AsciiRampName = keyof typeof ASCII_RAMPS\n\n/** Resolve a ramp by literal string or named key. */\nexport function resolveAsciiRamp(ramp: string): string {\n  const named = ASCII_RAMPS[ramp as AsciiRampName]\n  return named ?? ramp\n}\n\n/** Character at a normalized 0..1 position of a ramp. */\nexport function rampAt(ramp: string, v01: number): string {\n  const i = Math.min(\n    ramp.length - 1,\n    Math.max(0, Math.floor(v01 * ramp.length))\n  )\n  return ramp[i] ?? ramp[0] ?? \" \"\n}\n\n/** Computed `color` of an element — canvas-safe on any accent or surface. */\nexport function readAsciiColor(el: Element | null): string {\n  if (!el) return \"#e5e7eb\"\n  return getComputedStyle(el).color\n}\n\n/**\n * One tick loop for an effect.\n * - `prefers-reduced-motion` → a single draw(0, 0) frame, then done\n * - offscreen (IntersectionObserver, 64px rootMargin) → paused, zero work\n * - throttled to `fps`; dt is clamped to 0.2s to survive tab switches\n */\nexport function useAsciiTick(\n  host: React.RefObject<Element | null>,\n  draw: (t: number, dt: number) => void,\n  opts?: { fps?: number }\n) {\n  const { fps = 16 } = opts ?? {}\n  const drawRef = React.useRef(draw)\n  React.useEffect(() => {\n    drawRef.current = draw\n  })\n\n  React.useEffect(() => {\n    if (typeof window === \"undefined\") return\n    const reduced = window.matchMedia?.(\n      \"(prefers-reduced-motion: reduce)\"\n    )?.matches\n    const el = host.current\n    if (reduced || !el) {\n      drawRef.current(0, 0)\n      return\n    }\n\n    let raf = 0\n    let last = 0\n    let t0 = 0\n    const step = (now: number) => {\n      raf = requestAnimationFrame(step)\n      const min = 1000 / fps\n      if (last && now - last < min - 2) return\n      const dt = last ? Math.min((now - last) / 1000, 0.2) : 0.016\n      last = now\n      t0 ||= now\n      drawRef.current((now - t0) / 1000, dt)\n    }\n    const run = () => {\n      if (raf) return\n      last = 0\n      t0 = performance.now()\n      raf = requestAnimationFrame(step)\n    }\n    const stop = () => {\n      if (!raf) return\n      cancelAnimationFrame(raf)\n      raf = 0\n    }\n\n    const io = new IntersectionObserver(\n      (entries) => (entries[0]?.isIntersecting ? run() : stop()),\n      { rootMargin: \"64px\" }\n    )\n    io.observe(el)\n    return () => {\n      stop()\n      io.disconnect()\n    }\n  }, [fps, host])\n}\n\n/**\n * Size a canvas to cols × rows character cells, DPR-crisp, and hand back a\n * context whose monospace advance width is exactly `cellW`px. Call each frame\n * (cheap: only re-allocates when the grid actually changes).\n */\nexport function setupAsciiCanvas(\n  canvas: HTMLCanvasElement,\n  cols: number,\n  rows: number,\n  cellW = 10\n) {\n  const cellH = cellW * 2\n  const dpr = Math.min(window.devicePixelRatio || 1, 2)\n  const w = Math.round(cols * cellW)\n  const h = Math.round(rows * cellH)\n  if (canvas.width !== w * dpr) canvas.width = w * dpr\n  if (canvas.height !== h * dpr) canvas.height = h * dpr\n  const style = canvas.style\n  if (style.width !== `${w}px`) style.width = `${w}px`\n  if (style.height !== `${h}px`) style.height = `${h}px`\n\n  const ctx = canvas.getContext(\"2d\")\n  if (!ctx) return null\n  ctx.setTransform(dpr, 0, 0, dpr, 0, 0)\n  // Lock the advance width to the cell so column math is pixel-exact.\n  ctx.font = \"100px ui-monospace, SFMono-Regular, Menlo, monospace\"\n  const probe = ctx.measureText(\"M\").width || 60\n  const px = Math.round((cellW / probe) * 100 * 100) / 100\n  ctx.font = `${px}px ui-monospace, SFMono-Regular, Menlo, monospace`\n  ctx.textBaseline = \"top\"\n  return { ctx, cellW, cellH, w, h }\n}\n",
      "type": "registry:lib",
      "target": "lib/ascii-rig.ts"
    }
  ],
  "type": "registry:lib"
}