{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-scanner-sweep",
  "type": "registry:component",
  "title": "Scanner Sweep",
  "description": "A beam sweeps back and forth over a grid of dots, lighting cells as it passes and marking a few as detected. The pointer takes hold of the beam.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "scanner",
    "radar",
    "grid",
    "pointer",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/bg-scanner-sweep",
  "files": [
    {
      "path": "registry/hyperblur/bg-scanner-sweep.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-scanner-sweep.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgScannerSweepProps extends React.HTMLAttributes<HTMLDivElement> {\n  cellSize?: number;\n  color?: string;\n  accent?: string;\n  hits?: number;\n  decay?: number;\n  trail?: number;\n  seed?: number;\n  speed?: number;\n}\n\nconst css = `\n.hb-qo6 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-qo6-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-qo6-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction hashU(n: number, s: number) {\n  let x = (Math.imul(n, 2654435761) ^ Math.imul(s + 1, 1597334677)) | 0;\n  x = Math.imul(x ^ (x >>> 15), 2246822519);\n  x = Math.imul(x ^ (x >>> 13), 3266489917);\n  return ((x ^ (x >>> 16)) >>> 0) / 4294967295;\n}\n\nfunction toRgb(value: string): [number, number, number] {\n  const c = document.createElement(\"canvas\");\n  c.width = 1;\n  c.height = 1;\n  const x = c.getContext(\"2d\");\n  if (!x) return [47, 123, 255];\n  x.fillStyle = \"#2f7bff\";\n  x.fillStyle = value;\n  x.fillRect(0, 0, 1, 1);\n  const d = x.getImageData(0, 0, 1, 1).data;\n  return [d[0], d[1], d[2]];\n}\n\nexport function BgScannerSweep({\n  cellSize = 14,\n  color = \"rgba(255,255,255,0.16)\",\n  accent = \"#2f7bff\",\n  hits = 0.1,\n  decay = 1.1,\n  trail = 70,\n  seed = 9,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgScannerSweepProps) {\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 context = canvas?.getContext(\"2d\");\n    if (!wrap || !host || !canvas || !context) return;\n    const g = context;\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n\n    const [ar, ag, ab] = toRgb(accent);\n    const rgba = (a: number) => `rgba(${ar},${ag},${ab},${Math.max(0, Math.min(1, a)).toFixed(3)})`;\n    const s0 = Math.round(seed) | 0;\n    let cell = Math.max(6, cellSize);\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let cols = 0;\n    let rows = 0;\n    let ox = 0;\n    let oy = 0;\n    let heat = new Float32Array(0);\n    let hit = new Uint8Array(0);\n    let inside = false;\n    let tx = 0;\n    let ty = 0;\n    let bx = 0;\n    let by = 0;\n    let tilt = 0;\n    let lastBx = 0;\n    let lastTilt = 0;\n    let dir = 1;\n    let placed = false;\n\n    const idleX = (tt: number) => w * (0.5 + 0.56 * Math.sin(tt * 0.62));\n\n    const layout = () => {\n      cell = Math.max(6, cellSize);\n      while ((w / cell) * (h / cell) > 12000) cell *= 1.15;\n      cols = Math.ceil(w / cell);\n      rows = Math.ceil(h / cell);\n      ox = (w - cols * cell) / 2;\n      oy = (h - rows * cell) / 2;\n      heat = new Float32Array(cols * rows);\n      hit = new Uint8Array(cols * rows);\n      for (let n = 0; n < cols * rows; n++) hit[n] = hashU(n, s0) < hits ? 1 : 0;\n    };\n\n    const beamAt = (x: number, tl: number, y: number) => x + tl * (1 - (2 * y) / Math.max(1, h));\n\n    const advance = (dt: number) => {\n      const gx = inside ? tx : idleX(t);\n      if (!placed) {\n        bx = gx;\n        by = h / 2;\n        lastBx = bx;\n        placed = true;\n      } else {\n        const e = 1 - Math.exp(-dt * (inside ? 14 : 6));\n        bx += (gx - bx) * e;\n        by += ((inside ? ty : h / 2) - by) * Math.min(1, dt * 6);\n      }\n      const vel = dt > 0 ? (bx - lastBx) / dt : 0;\n      tilt += (Math.max(-h * 0.18, Math.min(h * 0.18, vel * 0.06)) - tilt) * Math.min(1, dt * 8);\n      if (Math.abs(bx - lastBx) > 0.05) dir = bx > lastBx ? 1 : -1;\n      const keepHit = Math.exp(-dt / Math.max(0.05, decay * 2.6));\n      const keep = Math.exp(-dt / Math.max(0.05, decay));\n      for (let j = 0; j < rows; j++) {\n        const cy = oy + (j + 0.5) * cell;\n        const a = beamAt(lastBx, lastTilt, cy);\n        const b = beamAt(bx, tilt, cy);\n        const lo = Math.min(a, b) - cell * 0.5;\n        const hi = Math.max(a, b) + cell * 0.5;\n        for (let i = 0; i < cols; i++) {\n          const n = j * cols + i;\n          const cx = ox + (i + 0.5) * cell;\n          if (cx >= lo && cx <= hi) heat[n] = 1;\n          else heat[n] *= hit[n] ? keepHit : keep;\n        }\n      }\n      lastBx = bx;\n      lastTilt = tilt;\n    };\n\n    const draw = () => {\n      if (!w || !h) return;\n      g.clearRect(0, 0, w, h);\n      const dot = Math.max(0.8, cell * 0.08);\n      g.fillStyle = color;\n      g.beginPath();\n      for (let j = 0; j < rows; j++) {\n        for (let i = 0; i < cols; i++) {\n          const n = j * cols + i;\n          if (heat[n] > 0.25) continue;\n          const cx = ox + (i + 0.5) * cell;\n          const cy = oy + (j + 0.5) * cell;\n          g.moveTo(cx + dot, cy);\n          g.arc(cx, cy, dot, 0, Math.PI * 2);\n        }\n      }\n      g.fill();\n      for (let j = 0; j < rows; j++) {\n        for (let i = 0; i < cols; i++) {\n          const n = j * cols + i;\n          const k = heat[n];\n          if (k < 0.02) continue;\n          const cx = ox + (i + 0.5) * cell;\n          const cy = oy + (j + 0.5) * cell;\n          if (hit[n]) {\n            const s = cell * 0.62;\n            g.fillStyle = rgba(0.25 + 0.6 * k);\n            g.fillRect(cx - s / 2, cy - s / 2, s, s);\n            if (k > 0.15) {\n              const m = cell * 0.5;\n              const l = cell * 0.2;\n              g.strokeStyle = rgba(k);\n              g.lineWidth = 1;\n              g.beginPath();\n              g.moveTo(cx - m, cy - m + l);\n              g.lineTo(cx - m, cy - m);\n              g.lineTo(cx - m + l, cy - m);\n              g.moveTo(cx + m - l, cy - m);\n              g.lineTo(cx + m, cy - m);\n              g.lineTo(cx + m, cy - m + l);\n              g.moveTo(cx + m, cy + m - l);\n              g.lineTo(cx + m, cy + m);\n              g.lineTo(cx + m - l, cy + m);\n              g.moveTo(cx - m + l, cy + m);\n              g.lineTo(cx - m, cy + m);\n              g.lineTo(cx - m, cy + m - l);\n              g.stroke();\n            }\n          } else {\n            const s = cell * (0.18 + 0.32 * k);\n            g.fillStyle = rgba(k * 0.75);\n            g.fillRect(cx - s / 2, cy - s / 2, s, s);\n          }\n        }\n      }\n      const topX = beamAt(bx, tilt, 0);\n      const botX = beamAt(bx, tilt, h);\n      const tr = Math.max(4, trail);\n      const band = g.createLinearGradient(bx, 0, bx - dir * tr, 0);\n      band.addColorStop(0, rgba(0.32));\n      band.addColorStop(1, rgba(0));\n      g.fillStyle = band;\n      g.beginPath();\n      g.moveTo(topX, 0);\n      g.lineTo(topX - dir * tr, 0);\n      g.lineTo(botX - dir * tr, h);\n      g.lineTo(botX, h);\n      g.closePath();\n      g.fill();\n      g.globalCompositeOperation = \"lighter\";\n      g.beginPath();\n      g.moveTo(topX, 0);\n      g.lineTo(botX, h);\n      g.strokeStyle = rgba(0.9);\n      g.lineWidth = 3;\n      g.stroke();\n      g.strokeStyle = \"rgba(255,255,255,0.85)\";\n      g.lineWidth = 1;\n      g.stroke();\n      const hx = beamAt(bx, tilt, by);\n      const glow = g.createRadialGradient(hx, by, 0, hx, by, cell * 4);\n      glow.addColorStop(0, rgba(0.55));\n      glow.addColorStop(1, rgba(0));\n      g.fillStyle = glow;\n      g.fillRect(hx - cell * 4, by - cell * 4, cell * 8, cell * 8);\n      g.globalCompositeOperation = \"source-over\";\n    };\n\n    const resize = () => {\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      g.setTransform(dpr, 0, 0, dpr, 0, 0);\n      layout();\n      placed = false;\n      if (motion.matches) still();\n      else {\n        advance(0);\n        draw();\n      }\n    };\n\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    const still = () => {\n      inside = false;\n      placed = false;\n      t = 0;\n      advance(0);\n      for (let s = 1; s <= 40; s++) {\n        t = s * 0.03;\n        advance(0.03);\n      }\n      draw();\n    };\n\n    const frame = (now: number) => {\n      const dt = Math.min(0.033, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      advance(dt);\n      draw();\n      raf = requestAnimationFrame(frame);\n    };\n\n    const 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    const move = (e: PointerEvent) => {\n      if (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    const leave = (e: PointerEvent) => {\n      if (e.type === \"pointerup\" && e.pointerType === \"mouse\") return;\n      if (inside) {\n        const target = Math.max(-0.999, Math.min(0.999, (bx / Math.max(1, w) - 0.5) / 0.56));\n        const phase = Math.asin(target) / 0.62;\n        const period = (Math.PI * 2) / 0.62;\n        const goingRight = dir > 0;\n        const base = goingRight ? phase : Math.PI / 0.62 - phase;\n        t = base + Math.ceil((t - base) / period) * period;\n      }\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, color, accent, hits, decay, trail, seed, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qo6 ${className}`} style={style} {...rest}>\n      <style href=\"hb-bg-scanner-sweep\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qo6-c\" aria-hidden=\"true\" />\n      {children != null && <div className=\"hb-qo6-in\">{children}</div>}\n    </div>\n  );\n}\n"
    }
  ]
}