{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-grid-scan",
  "type": "registry:component",
  "title": "Grid Scan",
  "description": "A flat grid crossed by a horizontal and a vertical scan line. The cell under the pointer lights up and leaves a trail of fading cells.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "grid",
    "scan",
    "trail",
    "canvas",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/bg-grid-scan",
  "files": [
    {
      "path": "registry/hyperblur/bg-grid-scan.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-grid-scan.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgGridScanProps extends React.HTMLAttributes<HTMLDivElement> {\n  cellSize?: number;\n  accent?: string;\n  color?: string;\n  background?: string;\n  trail?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-qp4 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-qp4-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-qp4-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nexport function BgGridScan({\n  cellSize = 28,\n  accent = \"#2f7bff\",\n  color = \"rgba(255,255,255,0.07)\",\n  background = \"#08090c\",\n  trail = 1.2,\n  speed = 1,\n  children,\n  className = \"\",\n  style,\n  ...rest\n}: BgGridScanProps) {\n  const wrapRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const wrap = wrapRef.current;\n    const host = wrap?.parentElement;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !ctx) return;\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const decay = Math.max(0.1, trail);\n\n    let w = 0;\n    let h = 0;\n    let cell = 28;\n    let cols = 0;\n    let rows = 0;\n    let ox = 0;\n    let oy = 0;\n    let heat = new Float32Array(0);\n    let t = 0;\n    let inside = false;\n    let tx = 0;\n    let ty = 0;\n    let px = 0;\n    let py = 0;\n    let lx = 0;\n    let ly = 0;\n    let placed = false;\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.38 * Math.sin(tt * 0.53 + 0.4) * Math.cos(tt * 0.21));\n    }\n    function idleY(tt: number) {\n      return h * (0.5 + 0.36 * Math.sin(tt * 0.81 + 1.1));\n    }\n\n    function layout() {\n      cell = Math.max(10, cellSize);\n      while ((w / cell) * (h / cell) > 9000) cell *= 1.2;\n      cols = Math.ceil(w / cell) + 1;\n      rows = Math.ceil(h / cell) + 1;\n      ox = ((w % cell) - cell) / 2;\n      oy = ((h % cell) - cell) / 2;\n      heat = new Float32Array(cols * rows);\n    }\n\n    function mark(x: number, y: number, v: number) {\n      const c = Math.floor((x - ox) / cell);\n      const r = Math.floor((y - oy) / cell);\n      if (c < 0 || r < 0 || c >= cols || r >= rows) return;\n      const i = r * cols + c;\n      if (heat[i] < v) heat[i] = v;\n    }\n\n    function track(dt: number) {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (!placed || dt === 0) {\n        px = gx;\n        py = gy;\n        lx = px;\n        ly = py;\n        placed = true;\n        return;\n      }\n      const k = 1 - Math.exp(-dt * (inside ? 18 : 4));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n      const dist = Math.hypot(px - lx, py - ly);\n      const steps = Math.max(1, Math.ceil(dist / (cell * 0.5)));\n      for (let s = 1; s <= steps; s++) {\n        mark(lx + ((px - lx) * s) / steps, ly + ((py - ly) * s) / steps, 1);\n      }\n      lx = px;\n      ly = py;\n    }\n\n    function scanPos() {\n      const sy = (((t * 0.16) % 1) + 1) % 1;\n      const sx = (((t * 0.11 + 0.37) % 1) + 1) % 1;\n      return [sx * (w + 80) - 40, sy * (h + 80) - 40];\n    }\n\n    function draw(dt: number) {\n      if (!w || !h) return;\n      const fall = Math.exp(-dt / decay);\n      for (let i = 0; i < heat.length; i++) heat[i] *= fall;\n      const [scx, scy] = scanPos();\n      if (dt > 0) mark(scx, scy, 0.55);\n\n      ctx!.globalCompositeOperation = \"source-over\";\n      ctx!.globalAlpha = 1;\n      ctx!.fillStyle = background;\n      ctx!.fillRect(0, 0, w, h);\n\n      ctx!.fillStyle = accent;\n      for (let r = 0; r < rows; r++) {\n        const y = oy + r * cell;\n        const rowLit = Math.max(0, 1 - Math.abs(y + cell / 2 - scy) / (cell * 1.2));\n        for (let c = 0; c < cols; c++) {\n          const x = ox + c * cell;\n          const colLit = Math.max(0, 1 - Math.abs(x + cell / 2 - scx) / (cell * 1.2));\n          const v = heat[r * cols + c];\n          const a = v * 0.55 + (rowLit + colLit) * 0.05;\n          if (a < 0.01) continue;\n          ctx!.globalAlpha = Math.min(1, a);\n          ctx!.fillRect(x + 1, y + 1, cell - 1, cell - 1);\n        }\n      }\n\n      ctx!.globalAlpha = 1;\n      ctx!.strokeStyle = color;\n      ctx!.lineWidth = 1;\n      ctx!.beginPath();\n      for (let c = 0; c <= cols; c++) {\n        const x = Math.round(ox + c * cell) + 0.5;\n        ctx!.moveTo(x, 0);\n        ctx!.lineTo(x, h);\n      }\n      for (let r = 0; r <= rows; r++) {\n        const y = Math.round(oy + r * cell) + 0.5;\n        ctx!.moveTo(0, y);\n        ctx!.lineTo(w, y);\n      }\n      ctx!.stroke();\n\n      ctx!.strokeStyle = accent;\n      for (let r = 0; r < rows; r++) {\n        for (let c = 0; c < cols; c++) {\n          const v = heat[r * cols + c];\n          if (v < 0.25) continue;\n          ctx!.globalAlpha = Math.min(1, (v - 0.25) * 1.3);\n          ctx!.strokeRect(Math.round(ox + c * cell) + 0.5, Math.round(oy + r * cell) + 0.5, cell, cell);\n        }\n      }\n\n      ctx!.globalCompositeOperation = \"lighter\";\n      const band = Math.max(26, cell * 1.4);\n      const gh = ctx!.createLinearGradient(0, scy - band, 0, scy + band);\n      gh.addColorStop(0, \"rgba(0,0,0,0)\");\n      gh.addColorStop(0.5, accent);\n      gh.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx!.globalAlpha = 0.16;\n      ctx!.fillStyle = gh;\n      ctx!.fillRect(0, scy - band, w, band * 2);\n      const gv = ctx!.createLinearGradient(scx - band, 0, scx + band, 0);\n      gv.addColorStop(0, \"rgba(0,0,0,0)\");\n      gv.addColorStop(0.5, accent);\n      gv.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx!.fillStyle = gv;\n      ctx!.fillRect(scx - band, 0, band * 2, h);\n\n      ctx!.globalAlpha = 0.9;\n      ctx!.fillStyle = accent;\n      ctx!.fillRect(0, scy - 0.75, w, 1.5);\n      ctx!.fillRect(scx - 0.75, 0, 1.5, h);\n      ctx!.globalAlpha = 0.7;\n      ctx!.fillStyle = \"#ffffff\";\n      ctx!.fillRect(0, scy - 0.25, w, 0.5);\n      ctx!.fillRect(scx - 0.25, 0, 0.5, h);\n\n      const cross = ctx!.createRadialGradient(scx, scy, 0, scx, scy, band * 1.6);\n      cross.addColorStop(0, \"#ffffff\");\n      cross.addColorStop(0.25, accent);\n      cross.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx!.globalAlpha = 0.55;\n      ctx!.fillStyle = cross;\n      ctx!.fillRect(scx - band * 1.6, scy - band * 1.6, band * 3.2, band * 3.2);\n\n      const pr = cell * 2.2;\n      const pg = ctx!.createRadialGradient(px, py, 0, px, py, pr);\n      pg.addColorStop(0, accent);\n      pg.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx!.globalAlpha = 0.35;\n      ctx!.fillStyle = pg;\n      ctx!.fillRect(px - pr, py - pr, pr * 2, pr * 2);\n\n      ctx!.globalAlpha = 1;\n      ctx!.globalCompositeOperation = \"source-over\";\n    }\n\n    function still() {\n      inside = false;\n      placed = false;\n      heat.fill(0);\n      for (let i = 0; i < 24; i++) {\n        t = 3 - i * 0.04;\n        const x = idleX(t);\n        const y = idleY(t);\n        mark(x, y, 1 - i / 24);\n      }\n      t = 3;\n      track(0);\n      draw(0);\n    }\n\n    function resize() {\n      if (!wrap || !canvas || !ctx) return;\n      const nw = wrap.clientWidth;\n      const nh = wrap.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      canvas.width = Math.max(1, Math.round(w * dpr));\n      canvas.height = Math.max(1, Math.round(h * dpr));\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      layout();\n      if (motion.matches) still();\n      else {\n        track(0);\n        draw(0);\n      }\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.033, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      track(dt);\n      draw(dt);\n      raf = requestAnimationFrame(frame);\n    }\n\n    function sync() {\n      const run = visible && !document.hidden && !motion.matches;\n      if (run && !raf) {\n        prev = performance.now();\n        raf = requestAnimationFrame(frame);\n      } else if (!run && raf) {\n        cancelAnimationFrame(raf);\n        raf = 0;\n      }\n      if (motion.matches) still();\n    }\n\n    function move(e: PointerEvent) {\n      if (!host || motion.matches) return;\n      const rect = host.getBoundingClientRect();\n      const sx = rect.width ? host.clientWidth / rect.width : 1;\n      const sy = rect.height ? host.clientHeight / rect.height : 1;\n      tx = (e.clientX - rect.left) * sx;\n      ty = (e.clientY - rect.top) * sy;\n      inside = true;\n    }\n\n    function leave(e: PointerEvent) {\n      if (e.type === \"pointerup\" && e.pointerType === \"mouse\") return;\n      inside = false;\n    }\n\n    const ro = new ResizeObserver(resize);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      sync();\n    });\n    ro.observe(wrap);\n    io.observe(wrap);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    host.addEventListener(\"pointermove\", move, { passive: true });\n    host.addEventListener(\"pointerdown\", move, { passive: true });\n    host.addEventListener(\"pointerleave\", leave);\n    host.addEventListener(\"pointerup\", leave);\n    host.addEventListener(\"pointercancel\", leave);\n    resize();\n    sync();\n\n    return () => {\n      if (raf) cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      document.removeEventListener(\"visibilitychange\", sync);\n      motion.removeEventListener(\"change\", sync);\n      host.removeEventListener(\"pointermove\", move);\n      host.removeEventListener(\"pointerdown\", move);\n      host.removeEventListener(\"pointerleave\", leave);\n      host.removeEventListener(\"pointerup\", leave);\n      host.removeEventListener(\"pointercancel\", leave);\n    };\n  }, [cellSize, accent, color, background, trail, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qp4 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-grid-scan\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qp4-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-qp4-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}