{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"mote-field","type":"registry:ui","title":"Mote Field","description":"A drifting constellation that links nearby points and flees the cursor.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/mote-field.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface MoteFieldProps {\n  count?: number;\n  color?: string;\n  /** Px per frame. */\n  speed?: number;\n  /** Max px between linked particles; 0 disables links. */\n  linkDistance?: number;\n  /** Particles flee the cursor. */\n  interactive?: boolean;\n  className?: string;\n}\n\nexport function MoteField({\n  count = 80,\n  color = \"#ffffff\",\n  speed = 0.4,\n  linkDistance = 110,\n  interactive = true,\n  className,\n}: MoteFieldProps) {\n  const ref = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const canvas = ref.current!;\n    const ctx = canvas.getContext(\"2d\")!;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let w = 0;\n    let h = 0;\n    let raf = 0;\n    let visible = true;\n    const mouse = { x: -9999, y: -9999 };\n    const pts = Array.from({ length: count }, () => ({\n      x: Math.random(),\n      y: Math.random(),\n      vx: (Math.random() - 0.5) * speed,\n      vy: (Math.random() - 0.5) * speed,\n    }));\n    let placed = false;\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        for (const p of pts) {\n          p.x *= w;\n          p.y *= h;\n        }\n        placed = true;\n      }\n      if (reduced) frame();\n    };\n\n    const frame = () => {\n      ctx.clearRect(0, 0, w, h);\n      ctx.fillStyle = color;\n      ctx.strokeStyle = color;\n      for (const p of pts) {\n        if (!reduced) {\n          if (interactive) {\n            const dx = p.x - mouse.x;\n            const dy = p.y - mouse.y;\n            const d2 = dx * dx + dy * dy;\n            if (d2 < 10000) {\n              p.x += dx * 0.03;\n              p.y += dy * 0.03;\n            }\n          }\n          p.x = (p.x + p.vx + w) % w;\n          p.y = (p.y + p.vy + h) % h;\n        }\n        ctx.globalAlpha = 0.8;\n        ctx.beginPath();\n        ctx.arc(p.x, p.y, 1.4, 0, Math.PI * 2);\n        ctx.fill();\n      }\n      // ponytail: O(n²) link pass, fine to ~300 particles; use a spatial grid beyond that.\n      if (linkDistance > 0) {\n        const max2 = linkDistance * linkDistance;\n        for (let i = 0; i < pts.length; i++) {\n          for (let j = i + 1; j < pts.length; j++) {\n            const dx = pts[i].x - pts[j].x;\n            const dy = pts[i].y - pts[j].y;\n            const d2 = dx * dx + dy * dy;\n            if (d2 < max2) {\n              ctx.globalAlpha = (1 - d2 / max2) * 0.35;\n              ctx.beginPath();\n              ctx.moveTo(pts[i].x, pts[i].y);\n              ctx.lineTo(pts[j].x, pts[j].y);\n              ctx.stroke();\n            }\n          }\n        }\n      }\n    };\n\n    const loop = () => {\n      frame();\n      raf = visible && !reduced ? 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    };\n\n    const ro = new ResizeObserver(resize);\n    ro.observe(canvas);\n    // Stop drawing while offscreen.\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !raf && !reduced) raf = requestAnimationFrame(loop);\n    });\n    io.observe(canvas);\n    if (interactive) window.addEventListener(\"pointermove\", onMove, { passive: true });\n\n    return () => {\n      cancelAnimationFrame(raf);\n      ro.disconnect();\n      io.disconnect();\n      window.removeEventListener(\"pointermove\", onMove);\n    };\n  }, [count, color, speed, linkDistance, interactive]);\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"}]}