{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "particles",
  "title": "Particles",
  "description": "Canvas particles drift gently, link up with fine lines and move out of the pointer's way; density scales with the area.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [],
  "files": [
    {
      "path": "registry/components/backgrounds/particles.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\n\n/** Resolves any CSS color (oklch, color-mix, var()) to RGBA by painting it once on a 1×1 canvas. */\nfunction resolveColor(host: HTMLElement, color: string): [number, number, number, number] {\n  const probe = document.createElement(\"span\")\n  probe.style.color = color\n  probe.style.display = \"none\"\n  host.appendChild(probe)\n  const computed = getComputedStyle(probe).color\n  probe.remove()\n  const c = document.createElement(\"canvas\")\n  c.width = c.height = 1\n  const ctx = c.getContext(\"2d\", { willReadFrequently: true })\n  if (!ctx) return [128, 128, 128, 1]\n  ctx.fillStyle = \"#808080\"\n  ctx.fillStyle = computed\n  ctx.fillRect(0, 0, 1, 1)\n  const [r = 128, g = 128, b = 128, a = 255] = ctx.getImageData(0, 0, 1, 1).data\n  return [r, g, b, a / 255]\n}\n\ntype Particle = { x: number; y: number; vx: number; vy: number; bx: number; by: number; r: number; a: number }\n\nconst SPRING_K = 90\nconst SPRING_D = 11\n/** Alpha steps for the field: dots are batched per step, so 2,000 of them cost a dozen fill calls. */\nconst STEPS = 8\n\nexport type ParticlesProps = React.HTMLAttributes<HTMLDivElement> & {\n  /** field: a dot grid that breathes, bends around the pointer and ripples on click. constellation: the drifting linked dots. */\n  variant?: \"field\" | \"constellation\"\n  /** Field only: px between dots. */\n  spacing?: number\n  /** Field only: color the dots take near the pointer and in a ripple. */\n  accent?: string\n  /** Field only: fade the dots out toward the edges so the content stays readable. */\n  fade?: boolean\n  /** Constellation only: particles per 10,000 px² of area. */\n  density?: number\n  /** Constellation only: hard cap on the particle count. */\n  maxParticles?: number\n  /** Dot and line color; any CSS color or variable. Re-read when the theme changes. */\n  color?: string\n  /** Radius range in px. */\n  size?: [number, number]\n  /** Constellation: drift speed in px per second. Field: 10 is the normal breathing pace. */\n  speed?: number\n  /** Constellation only: draw lines between nearby particles. Passing this or `density` without `variant` selects the constellation. */\n  connect?: boolean\n  /** Max distance for a connecting line, in px. */\n  linkDistance?: number\n  /** React to the pointer (listens on the parent element). */\n  repel?: boolean\n  /** Distance in px within which the pointer pushes dots away. Defaults to 150 for the field and 110 for the constellation. */\n  repelRadius?: number\n}\n\n/**\n * A pointer-reactive dot field by default, with the drifting constellation as a variant. Canvas 2D, DPR-aware, pauses\n * off-screen and in hidden tabs, and draws one still frame for reduced motion.\n */\nexport function Particles({\n  variant: variantProp,\n  connect,\n  spacing = 20,\n  accent = \"var(--chart-1)\",\n  fade = true,\n  density: densityProp,\n  maxParticles = 220,\n  color = \"var(--foreground)\",\n  size = [0.6, 1.8],\n  speed = 10,\n  linkDistance = 110,\n  repel = true,\n  repelRadius,\n  className,\n  ...props\n}: ParticlesProps) {\n  const reduce = useReducedMotion() ?? false\n  const rootRef = React.useRef<HTMLDivElement>(null)\n  const canvasRef = React.useRef<HTMLCanvasElement>(null)\n  const [minSize, maxSize] = size\n  const variant = variantProp ?? (connect === undefined && densityProp === undefined ? \"field\" : \"constellation\")\n  const density = densityProp ?? 1.4\n  const radius = repelRadius ?? (variant === \"field\" ? 150 : 110)\n  const linked = connect ?? true\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    let w = 0, h = 0, dpr = 1\n    let raf = 0\n    let last = 0\n    let inView = false\n    let rgb = \"rgb(128 128 128)\"\n    let accentRgb = rgb\n    let alpha = 1\n    const pointer = { x: 0, y: 0, tx: 0, ty: 0, on: 0, active: false }\n    const ripples: { x: number; y: number; t: number }[] = []\n\n    const readColor = () => {\n      const [r, g, b, a] = resolveColor(root, color)\n      rgb = `rgb(${r} ${g} ${b})`\n      alpha = a\n      const [ar, ag, ab] = resolveColor(root, accent)\n      accentRgb = `rgb(${ar} ${ag} ${ab})`\n    }\n\n    /* ---- Constellation ---- */\n    let ps: Particle[] = []\n    const link2 = linkDistance * linkDistance\n    const spawn = (): Particle => {\n      const angle = Math.random() * Math.PI * 2\n      const v = speed * (0.4 + Math.random() * 0.6)\n      const bx = Math.cos(angle) * v\n      const by = Math.sin(angle) * v\n      return { x: Math.random() * w, y: Math.random() * h, vx: bx, vy: by, bx, by, r: minSize + Math.random() * (maxSize - minSize), a: 0.35 + Math.random() * 0.55 }\n    }\n    const drawLinked = () => {\n      ctx.fillStyle = rgb\n      ctx.strokeStyle = rgb\n      ctx.lineWidth = 0.6\n      if (linked) {\n        for (let i = 0; i < ps.length; i++) {\n          const p = ps[i]!\n          for (let j = i + 1; j < ps.length; j++) {\n            const q = ps[j]!\n            const dx = p.x - q.x\n            const dy = p.y - q.y\n            const d2 = dx * dx + dy * dy\n            if (d2 > link2) continue\n            ctx.globalAlpha = (1 - Math.sqrt(d2) / linkDistance) * 0.35 * alpha\n            ctx.beginPath()\n            ctx.moveTo(p.x, p.y)\n            ctx.lineTo(q.x, q.y)\n            ctx.stroke()\n          }\n        }\n      }\n      for (const p of ps) {\n        ctx.globalAlpha = p.a * alpha\n        ctx.beginPath()\n        ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2)\n        ctx.fill()\n      }\n    }\n    const stepLinked = (dt: number) => {\n      const m = 20\n      for (const p of ps) {\n        if (repel && pointer.active) {\n          const dx = p.x - pointer.tx\n          const dy = p.y - pointer.ty\n          const d = Math.hypot(dx, dy)\n          if (d < radius && d > 0.01) {\n            const f = (1 - d / radius) * 900 * dt\n            p.vx += (dx / d) * f\n            p.vy += (dy / d) * f\n          }\n        }\n        // Ease back toward the particle's own gentle drift.\n        const k = Math.min(1, dt * 1.6)\n        p.vx += (p.bx - p.vx) * k\n        p.vy += (p.by - p.vy) * k\n        p.x += p.vx * dt\n        p.y += p.vy * dt\n        if (p.x < -m) p.x = w + m\n        else if (p.x > w + m) p.x = -m\n        if (p.y < -m) p.y = h + m\n        else if (p.y > h + m) p.y = -m\n      }\n    }\n\n    /* ---- Field: offsets and velocities live in typed arrays, one spring per dot. ---- */\n    let cols = 0, rows = 0, ox = 0, oy = 0\n    let off = new Float32Array(0)\n    let vel = new Float32Array(0)\n    let time = 0\n    const layout = () => {\n      cols = Math.ceil(w / spacing) + 1\n      rows = Math.ceil(h / spacing) + 1\n      ox = (w - (cols - 1) * spacing) / 2\n      oy = (h - (rows - 1) * spacing) / 2\n      off = new Float32Array(cols * rows * 2)\n      vel = new Float32Array(cols * rows * 2)\n    }\n    const stepField = (dt: number) => {\n      time += dt * (speed / 10)\n      // The pointer itself eases, so a fast flick reads as a wake and not a teleport.\n      const chase = 1 - Math.exp(-dt * 14)\n      // With no pointer a ghost wanders the field, so it is never a still image (touch screens, galleries).\n      const tx = pointer.active ? pointer.tx : w * (0.5 + 0.36 * Math.sin(time * 0.43))\n      const ty = pointer.active ? pointer.ty : h * (0.5 + 0.34 * Math.sin(time * 0.71 + 1))\n      pointer.x += (tx - pointer.x) * chase\n      pointer.y += (ty - pointer.y) * chase\n      pointer.on += ((repel ? (pointer.active ? 1 : 0.55) : 0) - pointer.on) * (1 - Math.exp(-dt * 3))\n      for (let i = ripples.length - 1; i >= 0; i--) {\n        ripples[i]!.t += dt\n        if (ripples[i]!.t > 1.6) ripples.splice(i, 1)\n      }\n      const r2 = radius * radius\n      const damp = Math.exp(-SPRING_D * dt)\n      for (let row = 0, n = 0; row < rows; row++) {\n        for (let col = 0; col < cols; col++, n += 2) {\n          const bx = ox + col * spacing\n          const by = oy + row * spacing\n          let fx = -off[n]! * SPRING_K\n          let fy = -off[n + 1]! * SPRING_K\n          if (pointer.on > 0.01) {\n            const dx = bx - pointer.x\n            const dy = by - pointer.y\n            const d2 = dx * dx + dy * dy\n            if (d2 < r2 && d2 > 0.01) {\n              const d = Math.sqrt(d2)\n              const f = (1 - d / radius) ** 2 * 2300 * pointer.on\n              fx += (dx / d) * f\n              fy += (dy / d) * f\n            }\n          }\n          for (const r of ripples) {\n            const dx = bx - r.x\n            const dy = by - r.y\n            const d = Math.hypot(dx, dy) || 1\n            const band = 1 - Math.abs(d - r.t * 520) / 70\n            if (band > 0) {\n              const f = band * 5200 * (1 - r.t / 1.6)\n              fx += (dx / d) * f\n              fy += (dy / d) * f\n            }\n          }\n          vel[n] = (vel[n]! + fx * dt) * damp\n          vel[n + 1] = (vel[n + 1]! + fy * dt) * damp\n          off[n] = off[n]! + vel[n]! * dt\n          off[n + 1] = off[n + 1]! + vel[n + 1]! * dt\n        }\n      }\n    }\n    const drawField = () => {\n      const base: Path2D[] = Array.from({ length: STEPS }, () => new Path2D())\n      const lit: Path2D[] = Array.from({ length: STEPS }, () => new Path2D())\n      const r2 = radius * radius\n      for (let row = 0, n = 0; row < rows; row++) {\n        for (let col = 0; col < cols; col++, n += 2) {\n          const bx = ox + col * spacing\n          const by = oy + row * spacing\n          const x = bx + off[n]!\n          const y = by + off[n + 1]!\n          // Two slow waves crossing: the field breathes in long diagonal swells.\n          const swell = 0.5 + 0.5 * Math.sin(bx * 0.011 + by * 0.006 + time * 0.9) * Math.cos(by * 0.013 - bx * 0.004 - time * 0.6)\n          let a = 0.14 + swell * swell * 0.4\n          let dot = minSize + (maxSize - minSize) * swell * 0.55\n          let glow = 0\n          if (pointer.on > 0.01) {\n            const dx = bx - pointer.x\n            const dy = by - pointer.y\n            const d2 = dx * dx + dy * dy\n            if (d2 < r2) glow = (1 - Math.sqrt(d2) / radius) * pointer.on\n          }\n          for (const r of ripples) {\n            const band = 1 - Math.abs(Math.hypot(bx - r.x, by - r.y) - r.t * 520) / 90\n            if (band > 0) glow = Math.max(glow, band * (1 - r.t / 1.6))\n          }\n          if (glow > 0) {\n            a += glow * 0.75\n            dot += glow * maxSize * 0.9\n          }\n          if (fade) {\n            const ex = Math.min(1, Math.min(bx, w - bx) / (w * 0.14))\n            const ey = Math.min(1, Math.min(by, h - by) / (h * 0.2))\n            a *= Math.max(0, ex) * Math.max(0, ey)\n          }\n          if (a < 0.02) continue\n          const step = Math.min(STEPS - 1, Math.floor(Math.min(1, a) * STEPS))\n          const path = (glow > 0.18 ? lit : base)[step]!\n          path.moveTo(x + dot, y)\n          path.arc(x, y, dot, 0, Math.PI * 2)\n        }\n      }\n      for (let i = 0; i < STEPS; i++) {\n        ctx.globalAlpha = ((i + 0.6) / STEPS) * alpha\n        ctx.fillStyle = rgb\n        ctx.fill(base[i]!)\n        ctx.fillStyle = accentRgb\n        ctx.fill(lit[i]!)\n      }\n    }\n\n    const draw = () => {\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0)\n      ctx.clearRect(0, 0, w, h)\n      if (variant === \"field\") drawField()\n      else drawLinked()\n      ctx.globalAlpha = 1\n    }\n\n    const frame = (t: number) => {\n      raf = requestAnimationFrame(frame)\n      const dt = Math.min((t - last) / 1000, 0.033)\n      last = t\n      if (variant === \"field\") stepField(dt)\n      else stepLinked(dt)\n      draw()\n    }\n\n    const resize = () => {\n      const r = root.getBoundingClientRect()\n      const pw = w, ph = h\n      w = r.width\n      h = r.height\n      dpr = Math.min(window.devicePixelRatio || 1, 2)\n      canvas.width = Math.max(1, Math.round(w * dpr))\n      canvas.height = Math.max(1, Math.round(h * dpr))\n      if (variant === \"field\") layout()\n      else {\n        const target = Math.min(maxParticles, Math.round(((w * h) / 10000) * density))\n        if (pw && ph) for (const p of ps) { p.x = (p.x / pw) * w; p.y = (p.y / ph) * h }\n        while (ps.length < target) ps.push(spawn())\n        if (ps.length > target) ps = ps.slice(0, target)\n      }\n      draw()\n    }\n\n    const update = () => {\n      const run = inView && !document.hidden && !reduce\n      if (run && !raf) {\n        last = performance.now()\n        raf = requestAnimationFrame(frame)\n      } else if (!run && raf) {\n        cancelAnimationFrame(raf)\n        raf = 0\n      }\n    }\n\n    readColor()\n    resize()\n\n    const host = root.parentElement ?? root\n    const move = (e: PointerEvent) => {\n      const r = root.getBoundingClientRect()\n      pointer.tx = e.clientX - r.left\n      pointer.ty = e.clientY - r.top\n      pointer.active = true\n    }\n    const leave = () => {\n      pointer.active = false\n    }\n    const press = (e: PointerEvent) => {\n      if (!repel || variant !== \"field\" || ripples.length > 3) return\n      const r = root.getBoundingClientRect()\n      ripples.push({ x: e.clientX - r.left, y: e.clientY - r.top, t: 0 })\n    }\n    host.addEventListener(\"pointermove\", move)\n    host.addEventListener(\"pointerleave\", leave)\n    host.addEventListener(\"pointerdown\", press)\n\n    const ro = new ResizeObserver(resize)\n    ro.observe(root)\n    const io = new IntersectionObserver(([e]) => {\n      if (!e) return\n      inView = e.isIntersecting\n      update()\n    })\n    io.observe(root)\n    const onTheme = () => {\n      readColor()\n      draw()\n    }\n    const mo = new MutationObserver(onTheme)\n    mo.observe(document.documentElement, { attributes: true, attributeFilter: [\"class\", \"style\", \"data-theme\"] })\n    const mq = window.matchMedia(\"(prefers-color-scheme: dark)\")\n    mq.addEventListener(\"change\", onTheme)\n    document.addEventListener(\"visibilitychange\", update)\n\n    return () => {\n      cancelAnimationFrame(raf)\n      host.removeEventListener(\"pointermove\", move)\n      host.removeEventListener(\"pointerleave\", leave)\n      host.removeEventListener(\"pointerdown\", press)\n      ro.disconnect()\n      io.disconnect()\n      mo.disconnect()\n      mq.removeEventListener(\"change\", onTheme)\n      document.removeEventListener(\"visibilitychange\", update)\n    }\n  }, [variant, spacing, accent, fade, density, maxParticles, color, minSize, maxSize, speed, linked, linkDistance, repel, radius, reduce])\n\n  return (\n    <div ref={rootRef} aria-hidden=\"true\" className={cn(\"pointer-events-none absolute inset-0 overflow-hidden\", className)} {...props}>\n      <canvas ref={canvasRef} className=\"block size-full\" />\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/particles.tsx"
    }
  ],
  "categories": [
    "components",
    "backgrounds"
  ],
  "type": "registry:component"
}