{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "retro-grid",
  "type": "registry:component",
  "title": "Retro Grid",
  "description": "A perspective grid that scrolls endlessly toward a horizon with a muted sun; the cursor shifts the horizon and holding down speeds it up.",
  "author": "Marcelo Treviño Velázquez <https://furin.dev>",
  "dependencies": [],
  "categories": [
    "Backgrounds"
  ],
  "docs": "Installed in components/furin/retro-grid/. Docs, live controls and props table: https://furin.dev/components/retro-grid/",
  "files": [
    {
      "path": "registry/retro-grid/RetroGrid.tsx",
      "content": "\"use client\";\nimport { useEffect, useRef } from \"react\";\nimport type { CSSProperties, ReactNode } from \"react\";\nimport \"./RetroGrid.css\";\n\nexport interface RetroGridProps {\n  /** Contenido que va encima de la rejilla. */\n  children?: ReactNode;\n  /** Color de las líneas de la rejilla. */\n  color?: string;\n  /** Color del sol y del brillo del horizonte. */\n  accent?: string;\n  /** Color del fondo. */\n  background?: string;\n  /** Velocidad con la que avanza la rejilla. */\n  speed?: number;\n  /** Cuántas líneas verticales caben en el ancho. */\n  columns?: number;\n  /** Muestra el sol sobre el horizonte. */\n  sun?: boolean;\n  /** Qué tanto mueve el cursor el horizonte (paralaje). */\n  parallax?: number;\n  /** Clase extra para el contenedor. */\n  className?: string;\n  /** Estilos extra para el contenedor. */\n  style?: CSSProperties;\n}\n\nlet colorCtx: CanvasRenderingContext2D | null = null;\nconst RGB_CACHE = new Map<string, [number, number, number]>();\nfunction rgb(css: string, def: [number, number, number]): [number, number, number] {\n  const hit = RGB_CACHE.get(css);\n  if (hit) return hit;\n  try {\n    colorCtx ??= document.createElement(\"canvas\").getContext(\"2d\", { willReadFrequently: true })!;\n    colorCtx.clearRect(0, 0, 1, 1);\n    colorCtx.fillStyle = \"#010203\";\n    colorCtx.fillStyle = css;\n    colorCtx.fillRect(0, 0, 1, 1);\n    const d = colorCtx.getImageData(0, 0, 1, 1).data;\n    const out: [number, number, number] = [d[0], d[1], d[2]];\n    if (RGB_CACHE.size > 64) RGB_CACHE.clear();\n    RGB_CACHE.set(css, out);\n    return out;\n  } catch {\n    return def;\n  }\n}\n\n/** Fondo de rejilla infinita: un suelo en perspectiva que avanza hacia un horizonte con un sol sobrio y estrellas lejanas. */\nexport function RetroGrid({\n  children,\n  color = \"#8f9cd8\",\n  accent = \"#f0b08a\",\n  background = \"#0a0a12\",\n  speed = 1,\n  columns = 16,\n  sun = true,\n  parallax = 1,\n  className = \"\",\n  style,\n}: RetroGridProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const props = useRef({ color, accent, background, speed, columns, sun, parallax });\n  props.current = { color, accent, background, speed, columns, sun, parallax };\n  const wakeRef = useRef<(() => void) | null>(null);\n\n  useEffect(() => {\n    const root = rootRef.current;\n    if (!root) return;\n    const canvas = document.createElement(\"canvas\");\n    canvas.className = \"furin-retro-grid__lienzo\";\n    canvas.setAttribute(\"aria-hidden\", \"true\");\n    const ctx = canvas.getContext(\"2d\");\n    if (!ctx) return;\n    root.insertBefore(canvas, root.firstChild);\n\n    const reducido = matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let w = 0, h = 0, dpr = 1, raf = 0, visible = true, muerto = false, ult = 0;\n    let avance = reducido ? 0.35 : 0, t = 0;\n    const ptr = { x: 0, y: 0, tx: 0, ty: 0 };\n    let impulso = 0, impulsoObj = 0;\n    // Estrellas fijas en el cielo, con su propio parpadeo.\n    const estrellas = Array.from({ length: 70 }, () => ({ x: Math.random(), y: Math.random() ** 1.4, r: 0.4 + Math.random() * 0.9, f: Math.random() * 6.28, a: 0.25 + Math.random() * 0.6 }));\n\n    const pintar = () => {\n      const p = props.current;\n      const [cr, cg, cb] = rgb(p.color, [143, 156, 216]);\n      const [ar, ag, ab] = rgb(p.accent, [240, 176, 138]);\n      const [br, bg, bb] = rgb(p.background, [10, 10, 18]);\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.clearRect(0, 0, canvas.width, canvas.height);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n\n      // Horizonte y punto de fuga con paralaje del cursor.\n      const yh = h * 0.5 + ptr.y * h * 0.05 * p.parallax;\n      const vx = w / 2 - ptr.x * w * 0.12 * p.parallax;\n      const suelo = h - yh;\n\n      // Cielo: del fondo hacia un resplandor cálido pegado al horizonte.\n      const cielo = ctx.createLinearGradient(0, 0, 0, yh);\n      cielo.addColorStop(0, `rgb(${br},${bg},${bb})`);\n      cielo.addColorStop(0.62, `rgb(${br},${bg},${bb})`);\n      cielo.addColorStop(1, `rgb(${Math.round(br + (ar - br) * 0.2)},${Math.round(bg + (ag - bg) * 0.2)},${Math.round(bb + (ab - bb) * 0.2)})`);\n      ctx.fillStyle = cielo;\n      ctx.fillRect(0, 0, w, yh);\n      ctx.fillStyle = `rgb(${br},${bg},${bb})`;\n      ctx.fillRect(0, yh, w, suelo);\n\n      for (const e of estrellas) {\n        const y = e.y * yh * 0.9;\n        const a = e.a * (0.55 + 0.45 * Math.sin(t * 1.2 + e.f)) * (1 - y / yh);\n        ctx.fillStyle = `rgba(255,255,255,${a.toFixed(3)})`;\n        ctx.fillRect(e.x * w, y, e.r, e.r);\n      }\n\n      // Sol: disco con degradé, cortado por rendijas horizontales que crecen hacia abajo y suben despacio.\n      const R = Math.min(w, h) * 0.24;\n      if (p.sun) {\n        const cx = vx, cy = yh - R * 0.5;\n        ctx.save();\n        ctx.beginPath(); ctx.rect(0, 0, w, yh); ctx.clip();\n        ctx.globalCompositeOperation = \"lighter\";\n        const halo = ctx.createRadialGradient(cx, cy, R * 0.6, cx, cy, R * 3);\n        halo.addColorStop(0, `rgba(${ar},${ag},${ab},0.2)`);\n        halo.addColorStop(1, `rgba(${ar},${ag},${ab},0)`);\n        ctx.fillStyle = halo; ctx.fillRect(0, 0, w, yh);\n        ctx.globalCompositeOperation = \"source-over\";\n        const g = ctx.createLinearGradient(0, cy - R, 0, cy + R);\n        g.addColorStop(0, `rgba(${Math.min(255, ar + 12)},${Math.min(255, ag + 40)},${Math.min(255, ab + 50)},0.9)`);\n        g.addColorStop(1, `rgba(${ar},${Math.round(ag * 0.62)},${Math.round(ab * 0.85)},0.78)`);\n        ctx.fillStyle = g;\n        ctx.beginPath(); ctx.arc(cx, cy, R, 0, 6.2832); ctx.fill();\n        // Rendijas: se repinta el cielo dentro del disco, así parecen cortes que dejan ver lo de atrás.\n        ctx.save();\n        ctx.beginPath(); ctx.arc(cx, cy, R + 0.5, 0, 6.2832); ctx.clip();\n        ctx.fillStyle = cielo;\n        const fase = (t * 0.05) % 1;\n        const top = cy - R * 0.1;\n        for (let k = 0; k < 6; k++) {\n          const f = (k + fase) / 6;\n          ctx.fillRect(cx - R - 2, top + (yh - top) * f, R * 2 + 4, 0.8 + f * 5.5);\n        }\n        ctx.restore();\n        ctx.restore();\n      }\n\n      // Rejilla: horizontales que vienen hacia ti (z decreciente) y verticales que nacen del punto de fuga.\n      ctx.globalCompositeOperation = \"lighter\";\n      ctx.lineCap = \"butt\";\n      const dz = 0.95;\n      const zMax = 38;\n      for (let k = 0; k < 42; k++) {\n        const z = 1 + (k - avance) * dz;\n        if (z < 1 || z > zMax) continue;\n        const y = yh + suelo / z;\n        const lejania = 1 - z / zMax;\n        const a = Math.pow(lejania, 1.7);\n        ctx.strokeStyle = `rgba(${cr},${cg},${cb},${(0.55 * a).toFixed(3)})`;\n        ctx.lineWidth = 0.6 + 0.9 * Math.min(1, 2 / z);\n        ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(w, y); ctx.stroke();\n        ctx.strokeStyle = `rgba(${cr},${cg},${cb},${(0.07 * a).toFixed(3)})`;\n        ctx.lineWidth = 3.4;\n        ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(w, y); ctx.stroke();\n      }\n      const sep = (w * 1.0) / Math.max(4, p.columns);\n      const J = Math.ceil(p.columns * 1.5);\n      ctx.beginPath();\n      for (let j = -J; j <= J; j++) {\n        const xb = vx + j * sep * 1.0;\n        if (xb < -w * 1.2 || xb > w * 2.2) continue;\n        ctx.moveTo(vx + (xb - vx) * 0.02, yh + suelo * 0.0);\n        ctx.lineTo(xb + (xb - vx) * 0.0, h);\n      }\n      ctx.strokeStyle = `rgba(${cr},${cg},${cb},0.42)`;\n      ctx.lineWidth = 1;\n      ctx.stroke();\n      ctx.globalCompositeOperation = \"source-over\";\n\n      // Niebla del horizonte: tapa la rejilla lejana con el color del fondo y deja un hilo de luz en el horizonte.\n      const niebla = ctx.createLinearGradient(0, yh, 0, yh + suelo * 0.55);\n      niebla.addColorStop(0, `rgba(${br},${bg},${bb},1)`);\n      niebla.addColorStop(0.35, `rgba(${br},${bg},${bb},0.7)`);\n      niebla.addColorStop(1, `rgba(${br},${bg},${bb},0)`);\n      ctx.fillStyle = niebla;\n      ctx.fillRect(0, yh, w, suelo * 0.55);\n      ctx.globalCompositeOperation = \"lighter\";\n      const hl = ctx.createLinearGradient(0, 0, w, 0);\n      hl.addColorStop(0, `rgba(${ar},${ag},${ab},0)`);\n      hl.addColorStop(0.5, `rgba(${ar},${ag},${ab},0.55)`);\n      hl.addColorStop(1, `rgba(${ar},${ag},${ab},0)`);\n      ctx.fillStyle = hl;\n      ctx.fillRect(0, yh - 0.5, w, 1.2);\n      const hg = ctx.createLinearGradient(0, yh - 24, 0, yh + 6);\n      hg.addColorStop(0, `rgba(${ar},${ag},${ab},0)`);\n      hg.addColorStop(1, `rgba(${ar},${ag},${ab},0.16)`);\n      ctx.fillStyle = hg;\n      ctx.fillRect(0, yh - 24, w, 30);\n      ctx.globalCompositeOperation = \"source-over\";\n    };\n\n    const paso = (ts: number) => {\n      raf = 0;\n      if (muerto || !visible) return;\n      const dt = Math.min(0.05, ult ? (ts - ult) / 1000 : 0.016);\n      ult = ts;\n      t += dt;\n      impulso += (impulsoObj - impulso) * (1 - Math.exp(-dt * 3));\n      avance = (avance + dt * 0.55 * props.current.speed * (1 + impulso * 3)) % 1000;\n      const k = 1 - Math.exp(-dt * 4);\n      ptr.x += (ptr.tx - ptr.x) * k; ptr.y += (ptr.ty - ptr.y) * k;\n      pintar();\n      raf = requestAnimationFrame(paso);\n    };\n    const wake = () => { if (reducido) return; if (!raf && visible && !muerto) { ult = 0; raf = requestAnimationFrame(paso); } };\n    wakeRef.current = () => { if (reducido) pintar(); else wake(); };\n\n    const medir = () => {\n      dpr = Math.min(window.devicePixelRatio || 1, 2);\n      const r = root.getBoundingClientRect();\n      w = Math.max(1, r.width); h = Math.max(1, r.height);\n      canvas.width = Math.round(w * dpr); canvas.height = Math.round(h * dpr);\n      pintar();\n      wake();\n    };\n    const ro = new ResizeObserver(medir);\n    ro.observe(root);\n    const io = new IntersectionObserver(([e]) => { visible = e.isIntersecting; if (visible) wake(); }, { rootMargin: \"80px\" });\n    io.observe(root);\n\n    const onMove = (e: PointerEvent) => {\n      const r = root.getBoundingClientRect();\n      ptr.tx = ((e.clientX - r.left) / r.width) * 2 - 1;\n      ptr.ty = ((e.clientY - r.top) / r.height) * 2 - 1;\n    };\n    const onLeave = () => { ptr.tx = 0; ptr.ty = 0; impulsoObj = 0; };\n    const onDown = (e: PointerEvent) => { onMove(e); impulsoObj = 1; };\n    const onUp = () => { impulsoObj = 0; };\n    if (!reducido) {\n      root.addEventListener(\"pointermove\", onMove, { passive: true });\n      root.addEventListener(\"pointerdown\", onDown, { passive: true });\n      root.addEventListener(\"pointerup\", onUp);\n      root.addEventListener(\"pointercancel\", onUp);\n      root.addEventListener(\"pointerleave\", onLeave);\n    }\n    medir();\n\n    return () => {\n      muerto = true;\n      wakeRef.current = null;\n      cancelAnimationFrame(raf);\n      ro.disconnect(); io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerdown\", onDown);\n      root.removeEventListener(\"pointerup\", onUp);\n      root.removeEventListener(\"pointercancel\", onUp);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      canvas.remove();\n    };\n  }, []);\n\n  useEffect(() => { wakeRef.current?.(); }, [color, accent, background, speed, columns, sun, parallax]);\n\n  const vars = { \"--furin-retro-grid-bg\": background } as CSSProperties;\n  return (\n    <div ref={rootRef} className={`furin-retro-grid ${className}`} style={{ ...vars, ...style }}>\n      <span className=\"furin-retro-grid__vineta\" aria-hidden=\"true\" />\n      {children != null ? <div className=\"furin-retro-grid__contenido\">{children}</div> : null}\n    </div>\n  );\n}\n\nexport default RetroGrid;\n",
      "type": "registry:component",
      "target": "components/furin/retro-grid/RetroGrid.tsx"
    },
    {
      "path": "registry/retro-grid/RetroGrid.css",
      "content": ".furin-retro-grid {\n  position: absolute;\n  inset: 0;\n  display: grid;\n  place-items: center;\n  overflow: hidden;\n  isolation: isolate;\n  background: var(--furin-retro-grid-bg, #0a0a12);\n  touch-action: pan-y;\n}\n.furin-retro-grid__lienzo { position: absolute; inset: 0; width: 100%; height: 100%; display: block; z-index: 0; }\n.furin-retro-grid__vineta {\n  position: absolute;\n  inset: 0;\n  z-index: 1;\n  pointer-events: none;\n  background: radial-gradient(ellipse 95% 90% at 50% 50%, transparent 50%, rgba(0, 0, 0, 0.5) 120%);\n}\n.furin-retro-grid__contenido { position: relative; z-index: 2; max-width: 100%; pointer-events: none; }\n.furin-retro-grid__contenido > * { pointer-events: auto; }\n",
      "type": "registry:file",
      "target": "components/furin/retro-grid/RetroGrid.css"
    }
  ]
}
