{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"flock-trail","type":"registry:ui","title":"Flock Trail","description":"A flock of boids that separates, aligns and swirls around the cursor, leaving luminous trails.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/flock-trail.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface FlockTrailProps {\n  /** Number of boids. */\n  count?: number;\n  color?: string;\n  /** Color of the fastest boids. */\n  accent?: string;\n  /** Top speed in px per frame. */\n  maxSpeed?: number;\n  /** Boid length in px. */\n  size?: number;\n  /** Leave fading motion trails. */\n  trails?: boolean;\n  className?: string;\n}\n\nfunction hexToRgb(hex: string) {\n  const n = parseInt(hex.replace(\"#\", \"\").padEnd(6, \"0\").slice(0, 6), 16);\n  return [(n >> 16) & 255, (n >> 8) & 255, n & 255];\n}\n\nexport function FlockTrail({\n  count = 160,\n  color = \"#c6ff3d\",\n  accent = \"#22d3ee\",\n  maxSpeed = 5,\n  size = 7,\n  trails = true,\n  className,\n}: FlockTrailProps) {\n  const ref = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const canvas = ref.current!;\n    const host = canvas.parentElement ?? canvas;\n    const ctx = canvas.getContext(\"2d\")!;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const [r1, g1, b1] = hexToRgb(color);\n    const [r2, g2, b2] = hexToRgb(accent);\n    let w = 0;\n    let h = 0;\n    let raf = 0;\n    let visible = true;\n    const mouse = { x: 0, y: 0, at: -1e9 };\n    const px = new Float32Array(count);\n    const py = new Float32Array(count);\n    const vx = new Float32Array(count);\n    const vy = new Float32Array(count);\n    let placed = false;\n    // Ring buffer of recent positions for the trails.\n    const T = 10;\n    const hist = new Float32Array(count * T * 2);\n    let ring = 0;\n    let filled = 0;\n\n    const resize = () => {\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      w = canvas.offsetWidth;\n      h = canvas.offsetHeight;\n      canvas.width = w * dpr;\n      canvas.height = h * dpr;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      if (!placed) {\n        placed = true;\n        for (let i = 0; i < count; i++) {\n          px[i] = Math.random() * w;\n          py[i] = Math.random() * h;\n          const a = Math.random() * Math.PI * 2;\n          vx[i] = Math.cos(a) * maxSpeed * 0.5;\n          vy[i] = Math.sin(a) * maxSpeed * 0.5;\n        }\n      }\n      if (reduced) draw();\n    };\n\n    const step = (now: number) => {\n      const t = now / 1000;\n      let tx = mouse.x;\n      let ty = mouse.y;\n      if (now - mouse.at > 1500) {\n        tx = w / 2 + Math.sin(t * 0.7) * w * 0.3;\n        ty = h / 2 + Math.sin(t * 1.1) * h * 0.28;\n      }\n      // ponytail: O(n²) neighbour scan, fine to ~400 boids; add a spatial hash beyond that.\n      for (let i = 0; i < count; i++) {\n        let sx = 0, sy = 0, ax = 0, ay = 0, cx = 0, cy = 0, n = 0;\n        for (let j = 0; j < count; j++) {\n          if (i === j) continue;\n          const dx = px[j] - px[i];\n          const dy = py[j] - py[i];\n          const d2 = dx * dx + dy * dy;\n          if (d2 > 2500) continue;\n          n++;\n          ax += vx[j];\n          ay += vy[j];\n          cx += dx;\n          cy += dy;\n          if (d2 < 400) {\n            sx -= dx / (d2 + 1);\n            sy -= dy / (d2 + 1);\n          }\n        }\n        let fx = 0;\n        let fy = 0;\n        if (n) {\n          fx += (ax / n - vx[i]) * 0.05 + (cx / n) * 0.002 + sx * 1.6;\n          fy += (ay / n - vy[i]) * 0.05 + (cy / n) * 0.002 + sy * 1.6;\n        }\n        // Seek the target, with a sideways nudge so the swarm orbits instead of collapsing.\n        const dx = tx - px[i];\n        const dy = ty - py[i];\n        const d = Math.hypot(dx, dy) + 1;\n        fx += (dx / d) * 0.22 + (-dy / d) * 0.06;\n        fy += (dy / d) * 0.22 + (dx / d) * 0.06;\n        vx[i] += fx;\n        vy[i] += fy;\n        const s = Math.hypot(vx[i], vy[i]);\n        if (s > maxSpeed) {\n          vx[i] *= maxSpeed / s;\n          vy[i] *= maxSpeed / s;\n        }\n        px[i] += vx[i];\n        py[i] += vy[i];\n        hist[(i * T + ring) * 2] = px[i];\n        hist[(i * T + ring) * 2 + 1] = py[i];\n      }\n      ring = (ring + 1) % T;\n      filled = Math.min(T, filled + 1);\n    };\n\n    const draw = () => {\n      ctx.clearRect(0, 0, w, h);\n      ctx.lineCap = \"round\";\n      ctx.lineJoin = \"round\";\n      if (trails && filled > 2) {\n        ctx.lineWidth = 1;\n        ctx.strokeStyle = `rgba(${r2},${g2},${b2},0.22)`;\n        ctx.beginPath();\n        for (let i = 0; i < count; i++) {\n          for (let k = 0; k < filled; k++) {\n            const j = (i * T + ((ring - filled + k + T) % T)) * 2;\n            if (k) ctx.lineTo(hist[j], hist[j + 1]);\n            else ctx.moveTo(hist[j], hist[j + 1]);\n          }\n        }\n        ctx.stroke();\n      }\n      ctx.lineWidth = Math.max(1, size * 0.32);\n      for (let i = 0; i < count; i++) {\n        const s = Math.hypot(vx[i], vy[i]) || 1;\n        const k = Math.min(1, s / maxSpeed);\n        ctx.strokeStyle = `rgb(${r1 + (r2 - r1) * k},${g1 + (g2 - g1) * k},${b1 + (b2 - b1) * k})`;\n        ctx.beginPath();\n        ctx.moveTo(px[i], py[i]);\n        ctx.lineTo(px[i] - (vx[i] / s) * size, py[i] - (vy[i] / s) * size);\n        ctx.stroke();\n      }\n    };\n\n    const loop = (now: number) => {\n      step(now);\n      draw();\n      raf = visible ? requestAnimationFrame(loop) : 0;\n    };\n\n    const onMove = (e: PointerEvent) => {\n      const r = canvas.getBoundingClientRect();\n      mouse.x = e.clientX - r.left;\n      mouse.y = e.clientY - r.top;\n      mouse.at = performance.now();\n    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(canvas);\n    if (reduced) return () => ro.disconnect();\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !raf) raf = requestAnimationFrame(loop);\n    });\n    io.observe(canvas);\n    host.addEventListener(\"pointermove\", onMove, { passive: true });\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      host.removeEventListener(\"pointermove\", onMove);\n    };\n  }, [count, color, accent, maxSpeed, size, trails]);\n\n  return (\n    <canvas\n      ref={ref}\n      aria-hidden\n      className={className}\n      style={{ position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\" }}\n    />\n  );\n}\n"}]}