{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"swarm-word","type":"registry:ui","title":"Swarm Word","description":"Thousands of tiny particles swarm together to spell a word, burst away from the cursor and spring back into place.","dependencies":[],"registryDependencies":[],"files":[{"path":"registry/ui/swarm-word.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef } from \"react\";\n\nexport interface SwarmWordProps {\n  text?: string;\n  /** Particle square size, in px. */\n  particleSize?: number;\n  /** Sampling step, in px. Smaller means more particles. */\n  gap?: number;\n  color?: string;\n  /** Second gradient stop (right side). */\n  secondaryColor?: string;\n  /** Color of particles while they are flying. */\n  sparkColor?: string;\n  /** Pointer push radius, in px. */\n  repelRadius?: number;\n  /** How hard the pointer pushes. */\n  repelStrength?: number;\n  /** Spring pull back home, 0-1. */\n  returnSpeed?: number;\n  className?: string;\n}\n\ntype P = { x: number; y: number; vx: number; vy: number; hx: number; hy: number };\n\nexport function SwarmWord({\n  text = \"Glint\",\n  particleSize = 2.2,\n  gap = 4,\n  color = \"#bef264\",\n  secondaryColor = \"#22d3ee\",\n  sparkColor = \"#ffffff\",\n  repelRadius = 90,\n  repelStrength = 5,\n  returnSpeed = 0.06,\n  className,\n}: SwarmWordProps) {\n  const wrap = useRef<HTMLDivElement>(null);\n  const ref = useRef<HTMLCanvasElement>(null);\n\n  useEffect(() => {\n    const root = wrap.current!;\n    const canvas = ref.current!;\n    const ctx = canvas.getContext(\"2d\")!;\n    const reduced = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const family = getComputedStyle(root).fontFamily || \"sans-serif\";\n    const step = Math.max(2, gap);\n    const mouse = { x: -1e4, y: -1e4 };\n    let pts: P[] = [];\n    let w = 0;\n    let h = 0;\n    let fill: CanvasGradient | string = color;\n    let raf = 0;\n    let visible = true;\n\n    const layout = () => {\n      w = canvas.offsetWidth;\n      h = canvas.offsetHeight;\n      if (!w || !h) return;\n      const dpr = Math.min(window.devicePixelRatio || 1, 2);\n      canvas.width = w * dpr;\n      canvas.height = h * dpr;\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n\n      const off = document.createElement(\"canvas\");\n      off.width = w;\n      off.height = h;\n      const o = off.getContext(\"2d\", { willReadFrequently: true })!;\n      o.font = `900 100px ${family}`;\n      const size = Math.min((100 * w * 0.84) / Math.max(o.measureText(text).width, 1), h * 0.6);\n      o.font = `900 ${size}px ${family}`;\n      o.textAlign = \"center\";\n      o.textBaseline = \"middle\";\n      o.fillText(text, w / 2, h / 2);\n      const data = o.getImageData(0, 0, w, h).data;\n\n      const homes: [number, number][] = [];\n      for (let y = 0; y < h; y += step) for (let x = 0; x < w; x += step) if (data[(y * w + x) * 4 + 3] > 128) homes.push([x, y]);\n      // Reuse existing particles so a resize morphs instead of restarting.\n      pts = homes.map(([hx, hy], i) => {\n        const p = pts[i] ?? { x: Math.random() * w, y: Math.random() * h, vx: 0, vy: 0, hx, hy };\n        p.hx = hx;\n        p.hy = hy;\n        if (reduced) {\n          p.x = hx;\n          p.y = hy;\n        }\n        return p;\n      });\n      const g = ctx.createLinearGradient(w * 0.15, 0, w * 0.85, 0);\n      g.addColorStop(0, color);\n      g.addColorStop(1, secondaryColor);\n      fill = g;\n      if (reduced || !raf) frame();\n    };\n\n    const frame = () => {\n      ctx.clearRect(0, 0, w, h);\n      const R2 = repelRadius * repelRadius;\n      const s = particleSize;\n      const calm = new Path2D();\n      const fast = new Path2D();\n      for (const p of pts) {\n        if (!reduced) {\n          const dx = p.x - mouse.x;\n          const dy = p.y - mouse.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 / repelRadius) * repelStrength;\n            p.vx += (dx / d) * f;\n            p.vy += (dy / d) * f;\n          }\n          p.vx = (p.vx + (p.hx - p.x) * returnSpeed) * 0.84;\n          p.vy = (p.vy + (p.hy - p.y) * returnSpeed) * 0.84;\n          p.x += p.vx;\n          p.y += p.vy;\n        }\n        (p.vx * p.vx + p.vy * p.vy > 1.5 ? fast : calm).rect(p.x, p.y, s, s);\n      }\n      ctx.fillStyle = fill;\n      ctx.fill(calm);\n      ctx.fillStyle = sparkColor;\n      ctx.fill(fast);\n    };\n\n    const loop = () => {\n      frame();\n      raf = visible && !reduced ? requestAnimationFrame(loop) : 0;\n    };\n    const move = (e: PointerEvent) => {\n      const r = canvas.getBoundingClientRect();\n      mouse.x = e.clientX - r.left;\n      mouse.y = e.clientY - r.top;\n    };\n    const leave = () => {\n      mouse.x = -1e4;\n      mouse.y = -1e4;\n    };\n\n    const ro = new ResizeObserver(layout);\n    ro.observe(canvas);\n    document.fonts?.ready.then(layout);\n    const io = new IntersectionObserver(([entry]) => {\n      visible = entry.isIntersecting;\n      if (visible && !raf && !reduced) raf = requestAnimationFrame(loop);\n    });\n    io.observe(canvas);\n    root.addEventListener(\"pointermove\", move, { passive: true });\n    root.addEventListener(\"pointerleave\", leave);\n    return () => {\n      cancelAnimationFrame(raf);\n      raf = -1;\n      ro.disconnect();\n      io.disconnect();\n      root.removeEventListener(\"pointermove\", move);\n      root.removeEventListener(\"pointerleave\", leave);\n    };\n  }, [text, particleSize, gap, color, secondaryColor, sparkColor, repelRadius, repelStrength, returnSpeed]);\n\n  return (\n    <div ref={wrap} className={className} style={{ position: \"relative\", width: \"100%\", height: \"100%\", touchAction: \"pan-y\" }}>\n      <span className=\"sr-only\">{text}</span>\n      <canvas ref={ref} aria-hidden style={{ position: \"absolute\", inset: 0, width: \"100%\", height: \"100%\" }} />\n    </div>\n  );\n}\n"}]}