{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-voronoi",
  "type": "registry:component",
  "title": "Voronoi Cells",
  "description": "A tiling of drifting Voronoi cells separated by thin gaps, each shaded like a glass tile. Cells light up one by one, a soft band sweeps across them, and the cell under the pointer glows.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "voronoi",
    "cells",
    "tiles",
    "canvas",
    "pointer"
  ],
  "docs": "https://hyperblur.dev/effects/bg-voronoi",
  "files": [
    {
      "path": "registry/hyperblur/bg-voronoi.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-voronoi.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgVoronoiProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  accent?: string;\n  background?: string;\n  cells?: number;\n  gap?: number;\n  speed?: number;\n  children?: React.ReactNode;\n}\n\nconst css = `\n.hb-ve3 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; box-sizing: border-box; }\n.hb-ve3-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-ve3-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: auto; font-family: inherit; }\n`;\n\nfunction rgbOf(value: string, fallback: [number, number, number]): [number, number, number] {\n  const raw = value.trim().replace(\"#\", \"\");\n  const hex = raw.length === 3 ? raw.split(\"\").map((c) => c + c).join(\"\") : raw;\n  const n = parseInt(hex.slice(0, 6), 16);\n  if (hex.length < 6 || Number.isNaN(n)) return fallback;\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nfunction rnd(i: number) {\n  let x = Math.imul(i ^ 0x9e3779b9, 0x85ebca6b);\n  x ^= x >>> 13;\n  x = Math.imul(x, 0xc2b2ae35);\n  x ^= x >>> 16;\n  return (x >>> 0) / 4294967296;\n}\n\ntype Pt = [number, number];\n\nfunction clip(poly: Pt[], mx: number, my: number, nx: number, ny: number): Pt[] {\n  const out: Pt[] = [];\n  const len = poly.length;\n  for (let i = 0; i < len; i++) {\n    const a = poly[i];\n    const b = poly[(i + 1) % len];\n    const da = (a[0] - mx) * nx + (a[1] - my) * ny;\n    const db = (b[0] - mx) * nx + (b[1] - my) * ny;\n    if (da <= 0) out.push(a);\n    if ((da <= 0) !== (db <= 0)) {\n      const k = da / (da - db);\n      out.push([a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k]);\n    }\n  }\n  return out;\n}\n\nexport function BgVoronoi({\n  color = \"#121722\",\n  accent = \"#2f7bff\",\n  background = \"#05060a\",\n  cells = 24,\n  gap = 3,\n  speed = 1,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: BgVoronoiProps) {\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 canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    const host = wrap?.parentElement;\n    if (!wrap || !canvas || !ctx || !host) return;\n    const g: CanvasRenderingContext2D = ctx;\n    const el: HTMLDivElement = wrap;\n    const cv: HTMLCanvasElement = canvas;\n    const hostEl: HTMLElement = host;\n\n    const base = rgbOf(color, [18, 23, 34]);\n    const ac = rgbOf(accent, [47, 123, 255]);\n    const n = Math.max(6, Math.min(80, Math.round(cells)));\n    const seeds = Array.from({ length: n }, (_, i) => ({\n      x: rnd(i * 5 + 11),\n      y: rnd(i * 5 + 12),\n      fx: 0.08 + rnd(i * 5 + 13) * 0.18,\n      fy: 0.07 + rnd(i * 5 + 14) * 0.16,\n      ph: rnd(i * 5 + 15) * 6.283,\n      lum: rnd(i * 5 + 16),\n    }));\n    const sx = new Float32Array(n);\n    const sy = new Float32Array(n);\n    const lit = new Float32Array(n);\n\n    let w = 0;\n    let h = 0;\n    let t = 0;\n    let mx = -9999;\n    let my = -9999;\n    let inside = false;\n\n    function render(dt: number) {\n      if (!w || !h) return;\n      const amp = Math.sqrt((w * h) / n) * 0.28;\n      for (let i = 0; i < n; i++) {\n        const s = seeds[i];\n        sx[i] = s.x * w + Math.sin(t * s.fx + s.ph) * amp;\n        sy[i] = s.y * h + Math.cos(t * s.fy + s.ph * 1.3) * amp;\n      }\n      let near = -1;\n      if (inside) {\n        let best = Infinity;\n        for (let i = 0; i < n; i++) {\n          const d = (sx[i] - mx) ** 2 + (sy[i] - my) ** 2;\n          if (d < best) {\n            best = d;\n            near = i;\n          }\n        }\n      }\n      const sweep = ((t * 0.09) % 1.6) - 0.3;\n      g.fillStyle = background;\n      g.fillRect(0, 0, w, h);\n      g.lineJoin = \"round\";\n      for (let i = 0; i < n; i++) {\n        let poly: Pt[] = [\n          [-40, -40],\n          [w + 40, -40],\n          [w + 40, h + 40],\n          [-40, h + 40],\n        ];\n        for (let j = 0; j < n && poly.length; j++) {\n          if (j === i) continue;\n          poly = clip(poly, (sx[i] + sx[j]) / 2, (sy[i] + sy[j]) / 2, sx[j] - sx[i], sy[j] - sy[i]);\n        }\n        if (poly.length < 3) continue;\n        let cx = 0;\n        let cy = 0;\n        for (const p of poly) {\n          cx += p[0];\n          cy += p[1];\n        }\n        cx /= poly.length;\n        cy /= poly.length;\n        const s = seeds[i];\n        const blink = Math.max(0, Math.sin(t * 0.55 + s.ph * 3)) ** 14;\n        const band = Math.max(0, 1 - Math.abs(cx / w - sweep) / 0.14);\n        const target = Math.max(blink, band * 0.55, i === near ? 1 : 0);\n        lit[i] += (target - lit[i]) * Math.min(1, dt > 0 ? dt * 5 : 1);\n        const a = lit[i];\n        g.beginPath();\n        for (let k = 0; k < poly.length; k++) {\n          const p = poly[k];\n          const dx = cx - p[0];\n          const dy = cy - p[1];\n          const d = Math.sqrt(dx * dx + dy * dy) || 1;\n          const m = Math.min(gap, d * 0.4);\n          const x = p[0] + (dx / d) * m;\n          const y = p[1] + (dy / d) * m;\n          if (k) g.lineTo(x, y);\n          else g.moveTo(x, y);\n        }\n        g.closePath();\n        const lift = 0.75 + s.lum * 0.5;\n        const r = Math.min(255, base[0] * lift + ac[0] * a * 0.35);\n        const gg = Math.min(255, base[1] * lift + ac[1] * a * 0.35);\n        const bb = Math.min(255, base[2] * lift + ac[2] * a * 0.35);\n        const grad = g.createRadialGradient(sx[i], sy[i], 0, sx[i], sy[i], amp * 2.6);\n        grad.addColorStop(0, `rgb(${(r + 10 + a * 30) | 0},${(gg + 12 + a * 30) | 0},${(bb + 16 + a * 30) | 0})`);\n        grad.addColorStop(1, `rgb(${(r * 0.62) | 0},${(gg * 0.62) | 0},${(bb * 0.62) | 0})`);\n        g.fillStyle = grad;\n        g.fill();\n        g.lineWidth = 1;\n        g.strokeStyle = `rgba(${ac[0]},${ac[1]},${ac[2]},${(0.08 + a * 0.75).toFixed(3)})`;\n        g.stroke();\n        g.fillStyle = `rgba(${ac[0]},${ac[1]},${ac[2]},${(0.18 + a * 0.8).toFixed(3)})`;\n        g.beginPath();\n        g.arc(sx[i], sy[i], 1.2 + a * 1.4, 0, 6.2832);\n        g.fill();\n      }\n    }\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    let visible = true;\n    let raf = 0;\n    let prev = 0;\n\n    function still() {\n      t = 6;\n      inside = false;\n      render(0);\n    }\n\n    function resize() {\n      const nw = el.clientWidth;\n      const nh = el.clientHeight;\n      if (!nw || !nh) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      w = nw;\n      h = nh;\n      cv.width = Math.max(1, Math.round(w * dpr));\n      cv.height = Math.max(1, Math.round(h * dpr));\n      g.setTransform(dpr, 0, 0, dpr, 0, 0);\n      if (motion.matches) still();\n      else render(0);\n    }\n\n    function frame(now: number) {\n      const dt = Math.min(0.05, (now - prev) / 1000);\n      prev = now;\n      t += dt * speed;\n      render(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      const rect = el.getBoundingClientRect();\n      mx = e.clientX - rect.left;\n      my = e.clientY - rect.top;\n      inside = true;\n    }\n\n    function leave() {\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(el);\n    io.observe(el);\n    document.addEventListener(\"visibilitychange\", sync);\n    motion.addEventListener(\"change\", sync);\n    hostEl.addEventListener(\"pointermove\", move, { passive: true });\n    hostEl.addEventListener(\"pointerleave\", 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      hostEl.removeEventListener(\"pointermove\", move);\n      hostEl.removeEventListener(\"pointerleave\", leave);\n    };\n  }, [color, accent, background, cells, gap, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-ve3 ${className}`} style={{ background, ...style }} {...rest}>\n      <style href=\"hb-bg-voronoi\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-ve3-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-ve3-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}