{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cursor-swarm",
  "type": "registry:component",
  "title": "Cursor Swarm",
  "description": "A flock of small arrow points that steer toward the pointer, keep their distance from each other and circle it like starlings.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "hover",
    "cursor",
    "swarm",
    "boids",
    "flock",
    "particles",
    "canvas"
  ],
  "docs": "https://hyperblur.dev/effects/cursor-swarm",
  "files": [
    {
      "path": "registry/hyperblur/cursor-swarm.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/cursor-swarm.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface CursorSwarmProps extends React.HTMLAttributes<HTMLDivElement> {\n  count?: number;\n  color?: string;\n  size?: number;\n  speed?: number;\n  trail?: number;\n  idle?: boolean;\n}\n\nconst css = `\n.hb-qi3 { position: relative; width: 100%; height: 100%; overflow: hidden; box-sizing: border-box; isolation: isolate; font-family: inherit; }\n.hb-qi3 *, .hb-qi3 *::before, .hb-qi3 *::after { box-sizing: border-box; }\n.hb-qi3-canvas { position: absolute; inset: 0; z-index: 0; display: block; width: 100%; height: 100%; pointer-events: none; }\n.hb-qi3-content { position: relative; z-index: 1; width: 100%; height: 100%; }\n`;\n\nfunction hash(n: number) {\n  let x = (Math.imul(n | 0, 374761393) + 668265263) | 0;\n  x = Math.imul(x ^ (x >>> 13), 1274126177);\n  return ((x ^ (x >>> 16)) >>> 0) / 4294967296;\n}\n\nexport function CursorSwarm({\n  count = 40,\n  color = \"#7fb3ff\",\n  size = 2.4,\n  speed = 1,\n  trail = 0.7,\n  idle = true,\n  className = \"\",\n  style,\n  children,\n  ...rest\n}: CursorSwarmProps) {\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    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const n = Math.max(1, Math.min(200, Math.floor(count)));\n    const pos = new Float32Array(n * 2);\n    const vel = new Float32Array(n * 2);\n    const max = 170 * speed;\n    const fadeAlpha = Math.max(0.05, Math.min(1, 1 - trail * 0.9));\n    let w = 0;\n    let h = 0;\n    let px = 0;\n    let py = 0;\n    let active = false;\n    let mix = 0;\n    let raf = 0;\n    let last = 0;\n    let t = 0;\n    let visible = true;\n    let seeded = false;\n\n    const seed = () => {\n      for (let i = 0; i < n; i++) {\n        const a = hash(i * 3 + 1) * Math.PI * 2;\n        const d = 20 + hash(i * 3 + 2) * Math.min(w, h) * 0.35;\n        pos[i * 2] = w / 2 + Math.cos(a) * d;\n        pos[i * 2 + 1] = h / 2 + Math.sin(a) * d;\n        vel[i * 2] = Math.cos(a + 1.6) * max * 0.5;\n        vel[i * 2 + 1] = Math.sin(a + 1.6) * max * 0.5;\n      }\n    };\n\n    const step = (dt: number) => {\n      t += dt;\n      mix += ((active ? 1 : 0) - mix) * (1 - Math.exp(-dt * 3));\n      const ix = idle ? w * (0.5 + 0.3 * Math.sin(t * 0.42) + 0.08 * Math.sin(t * 1.3)) : px;\n      const iy = idle ? h * (0.5 + 0.25 * Math.sin(t * 0.67 + 0.4)) : py;\n      const tx = ix + (px - ix) * mix;\n      const ty = iy + (py - iy) * mix;\n      for (let i = 0; i < n; i++) {\n        const x = pos[i * 2];\n        const y = pos[i * 2 + 1];\n        const vx = vel[i * 2];\n        const vy = vel[i * 2 + 1];\n        let sx = 0;\n        let sy = 0;\n        let cxs = 0;\n        let cys = 0;\n        let avx = 0;\n        let avy = 0;\n        let near = 0;\n        for (let j = 0; j < n; j++) {\n          if (j === i) continue;\n          const dx = x - pos[j * 2];\n          const dy = y - pos[j * 2 + 1];\n          const d2 = dx * dx + dy * dy;\n          if (d2 < 2500) {\n            near++;\n            cxs += pos[j * 2];\n            cys += pos[j * 2 + 1];\n            avx += vel[j * 2];\n            avy += vel[j * 2 + 1];\n            if (d2 < 256 && d2 > 0.01) {\n              sx += dx / d2;\n              sy += dy / d2;\n            }\n          }\n        }\n        const gx = tx - x;\n        const gy = ty - y;\n        const gd = Math.hypot(gx, gy) || 1;\n        const orbit = gd < 70 ? 0.9 : 0.25;\n        const side = i % 2 ? 1 : -1;\n        let dvx = (gx / gd) * max + (-gy / gd) * max * orbit * side;\n        let dvy = (gy / gd) * max + (gx / gd) * max * orbit * side;\n        const wobble = Math.sin(t * 1.7 + i * 1.3) * 40;\n        dvx += (-vy / (Math.hypot(vx, vy) || 1)) * wobble;\n        dvy += (vx / (Math.hypot(vx, vy) || 1)) * wobble;\n        let ax = (dvx - vx) * 1.6 + sx * 2600;\n        let ay = (dvy - vy) * 1.6 + sy * 2600;\n        if (near) {\n          ax += (cxs / near - x) * 0.8 + (avx / near - vx) * 0.6;\n          ay += (cys / near - y) * 0.8 + (avy / near - vy) * 0.6;\n        }\n        let nvx = vx + ax * dt;\n        let nvy = vy + ay * dt;\n        const sp = Math.hypot(nvx, nvy);\n        const lim = max * 1.25;\n        if (sp > lim) {\n          nvx = (nvx / sp) * lim;\n          nvy = (nvy / sp) * lim;\n        } else if (sp < 30 && sp > 0) {\n          nvx = (nvx / sp) * 30;\n          nvy = (nvy / sp) * 30;\n        }\n        vel[i * 2] = nvx;\n        vel[i * 2 + 1] = nvy;\n        pos[i * 2] = x + nvx * dt;\n        pos[i * 2 + 1] = y + nvy * dt;\n      }\n    };\n\n    const draw = (clear: boolean) => {\n      if (clear) {\n        ctx.clearRect(0, 0, w, h);\n      } else {\n        ctx.globalCompositeOperation = \"destination-out\";\n        ctx.fillStyle = `rgba(0,0,0,${fadeAlpha.toFixed(3)})`;\n        ctx.fillRect(0, 0, w, h);\n        ctx.globalCompositeOperation = \"source-over\";\n      }\n      ctx.fillStyle = color;\n      for (let i = 0; i < n; i++) {\n        const x = pos[i * 2];\n        const y = pos[i * 2 + 1];\n        const vx = vel[i * 2];\n        const vy = vel[i * 2 + 1];\n        const sp = Math.hypot(vx, vy) || 1;\n        const ux = vx / sp;\n        const uy = vy / sp;\n        const len = size * 2.6;\n        ctx.globalAlpha = 0.55 + 0.45 * Math.min(1, sp / max);\n        ctx.beginPath();\n        ctx.moveTo(x + ux * len, y + uy * len);\n        ctx.lineTo(x - ux * len * 0.6 - uy * size, y - uy * len * 0.6 + ux * size);\n        ctx.lineTo(x - ux * len * 0.6 + uy * size, y - uy * len * 0.6 - ux * size);\n        ctx.closePath();\n        ctx.fill();\n      }\n      ctx.globalAlpha = 1;\n    };\n\n    const frame = (now: number) => {\n      const dt = Math.min(0.033, Math.max(0.001, (now - last) / 1000));\n      last = now;\n      step(dt);\n      draw(false);\n      raf = requestAnimationFrame(frame);\n    };\n\n    const start = () => {\n      if (raf || reduced || !visible || !w) return;\n      last = performance.now();\n      raf = requestAnimationFrame(frame);\n    };\n    const stop = () => {\n      if (raf) 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      w = nw;\n      h = nh;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\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 (!seeded) {\n        seeded = true;\n        px = w / 2;\n        py = h / 2;\n        seed();\n      }\n      if (reduced) {\n        for (let i = 0; i < 90; i++) step(1 / 60);\n        draw(true);\n      } else {\n        start();\n      }\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const r = root.getBoundingClientRect();\n      const s = r.width ? w / r.width : 1;\n      px = (e.clientX - r.left) * s;\n      py = (e.clientY - r.top) * s;\n      active = true;\n    };\n    const onLeave = () => {\n      active = false;\n    };\n    const onUp = (e: PointerEvent) => {\n      if (e.pointerType === \"touch\") onLeave();\n    };\n\n    root.addEventListener(\"pointermove\", onMove);\n    root.addEventListener(\"pointerdown\", onMove);\n    root.addEventListener(\"pointerleave\", onLeave);\n    root.addEventListener(\"pointercancel\", onLeave);\n    root.addEventListener(\"pointerup\", onUp);\n    const ro = new ResizeObserver(resize);\n    ro.observe(root);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible) start();\n      else stop();\n    });\n    io.observe(root);\n    resize();\n\n    return () => {\n      stop();\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", onMove);\n      root.removeEventListener(\"pointerdown\", onMove);\n      root.removeEventListener(\"pointerleave\", onLeave);\n      root.removeEventListener(\"pointercancel\", onLeave);\n      root.removeEventListener(\"pointerup\", onUp);\n    };\n  }, [count, color, size, speed, trail, idle]);\n\n  return (\n    <div ref={rootRef} className={`hb-qi3 ${className}`} style={style} {...rest}>\n      <style href=\"hb-cursor-swarm\" precedence=\"default\">{css}</style>\n      <canvas ref={canvasRef} className=\"hb-qi3-canvas\" aria-hidden=\"true\" />\n      <div className=\"hb-qi3-content\">{children}</div>\n    </div>\n  );\n}\n"
    }
  ]
}