{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cursor-magnetic-filings",
  "type": "registry:component",
  "title": "Cursor Magnetic Filings",
  "description": "A canvas field of iron-filing needles that swing into the field lines of a dipole magnet. A hidden magnet drifts on its own until the pointer takes over.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "hover",
    "cursor",
    "magnet",
    "canvas",
    "particles",
    "field",
    "interactive"
  ],
  "docs": "https://hyperblur.dev/effects/cursor-magnetic-filings",
  "files": [
    {
      "path": "registry/hyperblur/cursor-magnetic-filings.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/cursor-magnetic-filings.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface CursorMagneticFilingsProps extends React.HTMLAttributes<HTMLDivElement> {\n  color?: string;\n  baseColor?: string;\n  spacing?: number;\n  length?: number;\n  thickness?: number;\n  strength?: number;\n  idle?: boolean;\n}\n\nconst css = `\n.hb-zb3 {\n  position: relative;\n  width: 100%;\n  height: 100%;\n  min-height: 120px;\n  overflow: hidden;\n  box-sizing: border-box;\n  font-family: inherit;\n  touch-action: pan-y;\n}\n.hb-zb3-c { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }\n.hb-zb3-in { position: relative; z-index: 1; width: 100%; height: 100%; pointer-events: none; }\n.hb-zb3-in > * { pointer-events: auto; }\n`;\n\nconst BUCKETS = 8;\nconst MAX_NEEDLES = 4200;\n\nfunction hash(n: number) {\n  let h = Math.imul(n ^ 0x9e3779b9, 0x85ebca6b);\n  h ^= h >>> 13;\n  h = Math.imul(h, 0xc2b2ae35);\n  h ^= h >>> 16;\n  return (h >>> 0) / 4294967296;\n}\n\nfunction parseColor(input: string, fallback: [number, number, number]): [number, number, number] {\n  const m = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(input.trim());\n  if (!m) return fallback;\n  let hex = m[1];\n  if (hex.length === 3) hex = hex.split(\"\").map((c) => c + c).join(\"\");\n  const v = parseInt(hex, 16);\n  return [(v >> 16) & 255, (v >> 8) & 255, v & 255];\n}\n\nexport function CursorMagneticFilings({\n  color = \"#7fb3ff\",\n  baseColor = \"#3b4150\",\n  spacing = 13,\n  length = 9,\n  thickness = 1.3,\n  strength = 1,\n  idle = true,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: CursorMagneticFilingsProps) {\n  const rootRef = React.useRef<HTMLDivElement>(null);\n  const canvasRef = React.useRef<HTMLCanvasElement>(null);\n\n  React.useEffect(() => {\n    const root = rootRef.current;\n    const canvas = canvasRef.current;\n    const ctx = canvas?.getContext(\"2d\");\n    if (!root || !canvas || !ctx) return;\n\n    const motion = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n    const ca = parseColor(baseColor, [59, 65, 80]);\n    const cb = parseColor(color, [127, 179, 255]);\n    const styles: string[] = [];\n    for (let b = 0; b < BUCKETS; b++) {\n      const u = b / (BUCKETS - 1);\n      const r = Math.round(ca[0] + (cb[0] - ca[0]) * u);\n      const g = Math.round(ca[1] + (cb[1] - ca[1]) * u);\n      const bl = Math.round(ca[2] + (cb[2] - ca[2]) * u);\n      styles.push(`rgba(${r},${g},${bl},${(0.5 + 0.5 * u).toFixed(3)})`);\n    }\n\n    let w = 0;\n    let h = 0;\n    let n = 0;\n    let xs = new Float32Array(0);\n    let ys = new Float32Array(0);\n    let rest0 = new Float32Array(0);\n    let ang = new Float32Array(0);\n    let segs: Float32Array[] = [];\n    const counts = new Array<number>(BUCKETS).fill(0);\n    let t = 0.8;\n    let raf = 0;\n    let prev = 0;\n    let visible = true;\n    let pointerOn = false;\n    let tx = 0;\n    let ty = 0;\n    let px = 0;\n    let py = 0;\n    let lastTx = 0;\n    let lastTy = 0;\n    let vx = 0;\n    let vy = 0;\n    let pa = 0;\n    let mix = 0;\n\n    const build = () => {\n      const sp = Math.max(6, spacing, Math.sqrt((w * h) / MAX_NEEDLES));\n      const cols = Math.ceil(w / sp) + 1;\n      const rows = Math.ceil(h / sp) + 1;\n      n = cols * rows;\n      xs = new Float32Array(n);\n      ys = new Float32Array(n);\n      rest0 = new Float32Array(n);\n      ang = new Float32Array(n);\n      segs = Array.from({ length: BUCKETS }, () => new Float32Array(n * 4));\n      for (let r = 0; r < rows; r++) {\n        for (let c = 0; c < cols; c++) {\n          const i = r * cols + c;\n          xs[i] = (c + 0.5 + (hash(i * 3 + 1) - 0.5) * 0.45) * sp - sp * 0.25;\n          ys[i] = (r + 0.5 + (hash(i * 3 + 2) - 0.5) * 0.45) * sp - sp * 0.25;\n          rest0[i] = hash(i * 3 + 3) * Math.PI;\n          ang[i] = rest0[i];\n        }\n      }\n    };\n\n    const step = (dt: number) => {\n      t += dt;\n      const ox = w * 0.5 + w * 0.3 * Math.cos(t * 0.5);\n      const oy = h * 0.5 + h * 0.27 * Math.sin(t * 0.83 + 0.7);\n      const oa = t * 0.7;\n      const target = pointerOn ? 1 : 0;\n      mix += (target - mix) * (1 - Math.exp(-dt * 5));\n      if (pointerOn) {\n        const k = 1 - Math.exp(-dt * 14);\n        px += (tx - px) * k;\n        py += (ty - py) * k;\n        const ddx = tx - lastTx;\n        const ddy = ty - lastTy;\n        lastTx = tx;\n        lastTy = ty;\n        const q = 1 - Math.exp(-dt * 9);\n        vx += (ddx / Math.max(dt, 0.001) - vx) * q;\n        vy += (ddy / Math.max(dt, 0.001) - vy) * q;\n        if (Math.hypot(vx, vy) > 30) pa = Math.atan2(vy, vx);\n      }\n      const active = idle || mix > 0.001;\n      const live = idle ? 1 - mix : 0;\n      const mx = idle ? ox * (1 - mix) + px * mix : px;\n      const my = idle ? oy * (1 - mix) + py * mix : py;\n      const ux = Math.cos(oa) * live + Math.cos(pa) * (1 - live);\n      const uy = Math.sin(oa) * live + Math.sin(pa) * (1 - live);\n      const th = Math.atan2(uy, ux);\n      const cx = Math.cos(th);\n      const sx = Math.sin(th);\n      const R0 = Math.min(Math.max(w, 160), Math.max(h, 160)) * 0.55 * strength;\n      const soft = Math.max(spacing * 1.1, 10);\n      const rate = 1 - Math.exp(-dt * 14);\n      counts.fill(0);\n      const half0 = length * 0.5;\n      for (let i = 0; i < n; i++) {\n        let wgt = 0;\n        let a = rest0[i];\n        if (active) {\n          const dx = xs[i] - mx;\n          const dy = ys[i] - my;\n          const r = Math.max(Math.sqrt(dx * dx + dy * dy), soft);\n          const rx = dx / r;\n          const ry = dy / r;\n          const md = cx * rx + sx * ry;\n          const bx = 3 * md * rx - cx;\n          const by = 3 * md * ry - sx;\n          const q = R0 / r;\n          const g = q * q * q * Math.sqrt(3 * md * md + 1) * 0.5;\n          wgt = 1 - Math.exp(-g * 0.35);\n          const ta = Math.atan2(by, bx);\n          const vxx = (1 - wgt) * Math.cos(2 * rest0[i]) + wgt * Math.cos(2 * ta);\n          const vyy = (1 - wgt) * Math.sin(2 * rest0[i]) + wgt * Math.sin(2 * ta);\n          a = Math.atan2(vyy, vxx) * 0.5;\n        }\n        let d = a - ang[i];\n        d -= Math.PI * Math.round(d / Math.PI);\n        ang[i] += d * rate;\n        const b = Math.min(BUCKETS - 1, Math.floor(wgt * BUCKETS));\n        const half = half0 * (0.82 + 0.5 * wgt);\n        const ca2 = Math.cos(ang[i]) * half;\n        const sa2 = Math.sin(ang[i]) * half;\n        const arr = segs[b];\n        const o = counts[b] * 4;\n        arr[o] = xs[i] - ca2;\n        arr[o + 1] = ys[i] - sa2;\n        arr[o + 2] = xs[i] + ca2;\n        arr[o + 3] = ys[i] + sa2;\n        counts[b]++;\n      }\n      return { mx, my, show: mix };\n    };\n\n    const draw = (info: { mx: number; my: number; show: number }) => {\n      ctx.clearRect(0, 0, w, h);\n      ctx.lineCap = \"round\";\n      for (let b = 0; b < BUCKETS; b++) {\n        const c = counts[b];\n        if (!c) continue;\n        const arr = segs[b];\n        ctx.strokeStyle = styles[b];\n        ctx.lineWidth = thickness * (0.8 + 0.5 * (b / (BUCKETS - 1)));\n        ctx.beginPath();\n        for (let k = 0; k < c; k++) {\n          const o = k * 4;\n          ctx.moveTo(arr[o], arr[o + 1]);\n          ctx.lineTo(arr[o + 2], arr[o + 3]);\n        }\n        ctx.stroke();\n      }\n      if (info.show > 0.01) {\n        const g = ctx.createRadialGradient(info.mx, info.my, 0, info.mx, info.my, 46);\n        g.addColorStop(0, `rgba(${cb[0]},${cb[1]},${cb[2]},${(0.22 * info.show).toFixed(3)})`);\n        g.addColorStop(1, `rgba(${cb[0]},${cb[1]},${cb[2]},0)`);\n        ctx.fillStyle = g;\n        ctx.fillRect(info.mx - 46, info.my - 46, 92, 92);\n      }\n    };\n\n    const still = () => {\n      draw(step(10));\n    };\n\n    const frame = (now: number) => {\n      const dt = Math.min(0.05, Math.max(0, (now - prev) / 1000));\n      prev = now;\n      draw(step(dt));\n      raf = requestAnimationFrame(frame);\n    };\n\n    const start = () => {\n      if (raf || motion.matches || !visible || (!idle && !pointerOn && mix < 0.002)) return;\n      prev = performance.now();\n      raf = requestAnimationFrame(frame);\n    };\n\n    const stop = () => {\n      if (!raf) return;\n      cancelAnimationFrame(raf);\n      raf = 0;\n    };\n\n    const resize = () => {\n      const nw = root.clientWidth;\n      const nh = root.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      build();\n      if (!px && !py) {\n        px = w * 0.5;\n        py = h * 0.5;\n      }\n      if (motion.matches || !idle) still();\n    };\n\n    const move = (ev: Event) => {\n      const e = ev as PointerEvent;\n      if (motion.matches) return;\n      const r = root.getBoundingClientRect();\n      tx = e.clientX - r.left;\n      ty = e.clientY - r.top;\n      if (!pointerOn) {\n        pointerOn = true;\n        px = tx;\n        py = ty;\n        lastTx = tx;\n        lastTy = ty;\n        vx = 0;\n        vy = 0;\n        start();\n      }\n    };\n\n    const leave = () => {\n      pointerOn = false;\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(root);\n    const io = new IntersectionObserver((entries) => {\n      visible = entries[entries.length - 1].isIntersecting;\n      if (visible) start();\n      else stop();\n    });\n    io.observe(root);\n    root.addEventListener(\"pointermove\", move);\n    root.addEventListener(\"pointerdown\", move);\n    root.addEventListener(\"pointerleave\", leave);\n    root.addEventListener(\"pointercancel\", leave);\n    const onMotion = () => {\n      if (motion.matches) {\n        stop();\n        still();\n      } else {\n        start();\n      }\n    };\n    motion.addEventListener(\"change\", onMotion);\n    resize();\n    if (!motion.matches) start();\n\n    const watch = window.setInterval(() => {\n      if (!idle && !pointerOn && mix < 0.002) stop();\n    }, 500);\n\n    return () => {\n      window.clearInterval(watch);\n      stop();\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", move);\n      root.removeEventListener(\"pointerdown\", move);\n      root.removeEventListener(\"pointerleave\", leave);\n      root.removeEventListener(\"pointercancel\", leave);\n      motion.removeEventListener(\"change\", onMotion);\n    };\n  }, [color, baseColor, spacing, length, thickness, strength, idle]);\n\n  return (\n    <div ref={rootRef} className={`hb-zb3 ${className}`.trim()} style={style} {...rest}>\n      <style href=\"hb-cursor-magnetic-filings\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-zb3-c\" aria-hidden=\"true\" />\n      {children ? <div className=\"hb-zb3-in\">{children}</div> : null}\n    </div>\n  );\n}\n"
    }
  ]
}