{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-grid-distortion",
  "type": "registry:component",
  "title": "Grid Distortion",
  "description": "A fine line grid that bulges around the pointer like a magnifying lens, with a soft accent glow where it bends. Drifts on its own until the pointer arrives.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "grid",
    "lens",
    "pointer",
    "canvas",
    "distortion"
  ],
  "docs": "https://hyperblur.dev/effects/bg-grid-distortion",
  "files": [
    {
      "path": "registry/hyperblur/bg-grid-distortion.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-grid-distortion.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgGridDistortionProps extends React.HTMLAttributes<HTMLDivElement> {\n  cellSize?: number;\n  radius?: number;\n  strength?: number;\n  color?: string;\n  accent?: string;\n  lineWidth?: number;\n  speed?: number;\n}\n\nconst css = `\n.hb-qj1 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }\n.hb-qj1-c { display: block; width: 100%; height: 100%; }\n`;\n\nexport function BgGridDistortion({\n  cellSize = 32,\n  radius = 140,\n  strength = 0.55,\n  color = \"rgba(255,255,255,0.13)\",\n  accent = \"#2f7bff\",\n  lineWidth = 1,\n  speed = 1,\n  className = \"\",\n  style,\n  ...rest\n}: BgGridDistortionProps) {\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 cell = Math.max(8, cellSize);\n    const R = Math.max(20, radius);\n    const step = Math.max(4, Math.min(12, cell / 3));\n    let w = 0;\n    let h = 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 placed = false;\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.32 * Math.sin(tt * 0.41));\n    }\n    function idleY(tt: number) {\n      return h * (0.5 + 0.3 * Math.sin(tt * 0.67 + 0.8));\n    }\n\n    function track(dt: number) {\n      const gx = inside ? tx : idleX(t);\n      const gy = inside ? ty : idleY(t);\n      if (!placed) {\n        px = gx;\n        py = gy;\n        placed = true;\n        return;\n      }\n      const k = 1 - Math.exp(-dt * (inside ? 10 : 2.4));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n    }\n\n    function draw() {\n      if (!ctx || !w || !h) return;\n      ctx.clearRect(0, 0, w, h);\n      const amt = strength * (0.85 + 0.15 * Math.sin(t * 1.3));\n      const inv = 1 / (R * R);\n      const ox = (w % cell) / 2;\n      const oy = (h % cell) / 2;\n      const grad = ctx.createRadialGradient(px, py, 0, px, py, R * 1.7);\n      grad.addColorStop(0, accent);\n      grad.addColorStop(1, color);\n      ctx.strokeStyle = grad;\n      ctx.lineWidth = lineWidth;\n      ctx.beginPath();\n      for (let y = oy - cell; y <= h + cell; y += cell) {\n        for (let x = -cell; x <= w + cell; x += step) {\n          const dx = x - px;\n          const dy = y - py;\n          const f = amt * Math.exp(-(dx * dx + dy * dy) * inv);\n          const X = x + dx * f;\n          const Y = y + dy * f;\n          if (x === -cell) ctx.moveTo(X, Y);\n          else ctx.lineTo(X, Y);\n        }\n      }\n      for (let x = ox - cell; x <= w + cell; x += cell) {\n        for (let y = -cell; y <= h + cell; y += step) {\n          const dx = x - px;\n          const dy = y - py;\n          const f = amt * Math.exp(-(dx * dx + dy * dy) * inv);\n          const X = x + dx * f;\n          const Y = y + dy * f;\n          if (y === -cell) ctx.moveTo(X, Y);\n          else ctx.lineTo(X, Y);\n        }\n      }\n      ctx.stroke();\n      ctx.fillStyle = accent;\n      const reach = R * 1.4;\n      for (let y = oy; y <= h; y += cell) {\n        for (let x = ox; x <= w; x += cell) {\n          const dx = x - px;\n          const dy = y - py;\n          const d2 = dx * dx + dy * dy;\n          if (d2 > reach * reach) continue;\n          const f = amt * Math.exp(-d2 * inv);\n          const k = 1 - Math.sqrt(d2) / reach;\n          ctx.globalAlpha = k * 0.9;\n          ctx.fillRect(x + dx * f - 1, y + dy * f - 1, 2, 2);\n        }\n      }\n      ctx.globalAlpha = 1;\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      if (motion.matches) still();\n      else {\n        track(0);\n        draw();\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 = 2.2;\n      inside = false;\n      placed = false;\n      track(0);\n      draw();\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      track(dt);\n      draw();\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, radius, strength, color, accent, lineWidth, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qj1 ${className}`} style={style} aria-hidden=\"true\" {...rest}>\n      <style href=\"hb-bg-grid-distortion\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qj1-c\" />\n    </div>\n  );\n}\n"
    }
  ]
}