{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bg-magnet-lines",
  "type": "registry:component",
  "title": "Magnet Lines",
  "description": "A field of short strokes that turn to face the pointer like iron filings around a magnet. Without a pointer they follow a slow swirl.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "overlay",
    "background",
    "magnet",
    "lines",
    "pointer",
    "canvas",
    "field"
  ],
  "docs": "https://hyperblur.dev/effects/bg-magnet-lines",
  "files": [
    {
      "path": "registry/hyperblur/bg-magnet-lines.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bg-magnet-lines.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BgMagnetLinesProps extends React.HTMLAttributes<HTMLDivElement> {\n  spacing?: number;\n  length?: number;\n  thickness?: number;\n  color?: string;\n  accent?: string;\n  radius?: number;\n  swirl?: number;\n  speed?: number;\n}\n\nconst css = `\n.hb-qj2 { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }\n.hb-qj2-c { display: block; width: 100%; height: 100%; }\n`;\n\nexport function BgMagnetLines({\n  spacing = 26,\n  length = 12,\n  thickness = 2,\n  color = \"rgba(255,255,255,0.2)\",\n  accent = \"#2f7bff\",\n  radius = 200,\n  swirl = 1,\n  speed = 1,\n  className = \"\",\n  style,\n  ...rest\n}: BgMagnetLinesProps) {\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 gap = Math.max(10, spacing);\n    const R = Math.max(30, radius);\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 presence = 0;\n    let placed = false;\n\n    function idleX(tt: number) {\n      return w * (0.5 + 0.3 * Math.sin(tt * 0.37 + 0.4));\n    }\n    function idleY(tt: number) {\n      return h * (0.5 + 0.32 * Math.sin(tt * 0.59 + 1.6));\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 ? 9 : 2.2));\n      px += (gx - px) * k;\n      py += (gy - py) * k;\n      presence += ((inside ? 1 : 0) - presence) * (1 - Math.exp(-dt * 3));\n    }\n\n    function draw() {\n      if (!ctx || !w || !h) return;\n      ctx.clearRect(0, 0, w, h);\n      const cols = Math.ceil(w / gap) + 1;\n      const rows = Math.ceil(h / gap) + 1;\n      const ox = (w - (cols - 1) * gap) / 2;\n      const oy = (h - (rows - 1) * gap) / 2;\n      const idle = (1 - presence) * swirl;\n      ctx.lineCap = \"round\";\n      ctx.lineWidth = thickness;\n      ctx.strokeStyle = color;\n      ctx.beginPath();\n      const near: number[] = [];\n      for (let r = 0; r < rows; r++) {\n        for (let c = 0; c < cols; c++) {\n          const x = ox + c * gap;\n          const y = oy + r * gap;\n          const dx = px - x;\n          const dy = py - y;\n          const d = Math.sqrt(dx * dx + dy * dy);\n          const a = Math.atan2(dy, dx) + idle * 0.85 * Math.sin(t * 0.55 + x * 0.006 - y * 0.0045);\n          const k = Math.max(0, 1 - d / R);\n          const half = (length * (0.7 + 0.65 * k)) / 2;\n          const cx = Math.cos(a) * half;\n          const cy = Math.sin(a) * half;\n          ctx.moveTo(x - cx, y - cy);\n          ctx.lineTo(x + cx, y + cy);\n          if (k > 0) near.push(x - cx, y - cy, x + cx, y + cy, k);\n        }\n      }\n      ctx.stroke();\n      ctx.strokeStyle = accent;\n      for (let i = 0; i < near.length; i += 5) {\n        ctx.globalAlpha = near[i + 4] * near[i + 4];\n        ctx.beginPath();\n        ctx.moveTo(near[i], near[i + 1]);\n        ctx.lineTo(near[i + 2], near[i + 3]);\n        ctx.stroke();\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 = 3.1;\n      inside = false;\n      placed = false;\n      presence = 0;\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  }, [spacing, length, thickness, color, accent, radius, swirl, speed]);\n\n  return (\n    <div ref={wrapRef} className={`hb-qj2 ${className}`} style={style} aria-hidden=\"true\" {...rest}>\n      <style href=\"hb-bg-magnet-lines\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qj2-c\" />\n    </div>\n  );\n}\n"
    }
  ]
}