{
  "$schema": "https://www.cooki-ui.design/schema/registry-item.json",
  "name": "text-particle-animation",
  "type": "registry:ui",
  "title": "Text Particle Animation",
  "description": "Interactive HTML5 canvas particle typography with Hooke's law spring physics, mouse hover repulsion, and click shockwave dispersal.",
  "category": "text",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "tags": [
    "text",
    "particles",
    "canvas",
    "physics",
    "interactive",
    "typography",
    "hover"
  ],
  "files": [
    {
      "path": "registry/text/text-particle-animation/text-particle-animation.tsx",
      "type": "registry:ui",
      "target": "components/ui/text-particle-animation.tsx",
      "content": "\"use client\";\n\nimport React, { useEffect, useRef } from \"react\";\n\nexport interface TextParticleAnimationProps {\n  text: string;\n  fontSize?: number;\n  fontFamily?: string;\n  fontWeight?: string | number;\n  resolution?: number;\n  pixelSize?: number;\n  hoverRadius?: number;\n  repelForce?: number;\n  clickRadius?: number;\n  clickForce?: number;\n  springForce?: number;\n  friction?: number;\n  theme?: \"light\" | \"dark\";\n  padding?: number;\n  height?: number;\n}\n\nexport function TextParticleAnimation({\n  text,\n  fontSize = 120,\n  fontFamily = \"sans-serif\",\n  fontWeight = 900,\n  resolution = 4,\n  pixelSize = 3,\n  hoverRadius = 60,\n  repelForce = 15,\n  clickRadius = 300,\n  clickForce = 80,\n  springForce = 0.08,\n  friction = 0.85,\n  theme,\n  padding = 150,\n  height,\n}: TextParticleAnimationProps) {\n  const canvasRef = useRef<HTMLCanvasElement | null>(null);\n\n  useEffect(() => {\n    const canvas = canvasRef.current;\n    if (!canvas) return;\n    const canvas2 = canvas;\n\n    const ctx = canvas.getContext(\"2d\", { willReadFrequently: true });\n    if (!ctx) return;\n    const ctx2 = ctx;\n\n    let particles: Particle[] = [];\n    let animationFrameId = 0;\n\n    const mouse = { x: -1000, y: -1000 };\n\n    const fontStr = `${fontWeight} ${fontSize}px ${fontFamily}`;\n    ctx.font = fontStr;\n    const metrics = ctx.measureText(text);\n\n    const textWidth = metrics.width;\n    const textHeight =\n      (metrics.actualBoundingBoxAscent || fontSize) +\n      (metrics.actualBoundingBoxDescent || 0);\n\n    // Respect the height prop if specified, otherwise use calculated height\n    canvas.width = textWidth + padding * 2;\n    canvas.height = typeof height === \"number\" ? height : textHeight + padding * 2;\n\n    ctx.font = fontStr;\n    ctx.textAlign = \"center\";\n    ctx.textBaseline = \"middle\";\n    const isDark = theme ? theme === \"dark\" : (typeof document !== \"undefined\" && document.documentElement.classList.contains(\"dark\"));\n    ctx.fillStyle = isDark ? \"#ffffff\" : \"#09090b\";\n    ctx.fillText(text, canvas.width / 2, canvas.height / 2);\n\n    const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);\n    ctx.clearRect(0, 0, canvas.width, canvas.height);\n\n    class Particle {\n      x: number;\n      y: number;\n      originX: number;\n      originY: number;\n      vx: number;\n      vy: number;\n      color: string;\n\n      constructor(x: number, y: number, color: string) {\n        this.x = x;\n        this.y = y;\n        this.originX = x;\n        this.originY = y;\n        this.vx = 0;\n        this.vy = 0;\n        this.color = color;\n      }\n\n      update() {\n        const dx = mouse.x - this.x;\n        const dy = mouse.y - this.y;\n        const distance = Math.sqrt(dx * dx + dy * dy);\n\n        if (distance < hoverRadius) {\n          const angle = Math.atan2(dy, dx);\n          const force = (hoverRadius - distance) / hoverRadius;\n          this.vx -= Math.cos(angle) * force * repelForce;\n          this.vy -= Math.sin(angle) * force * repelForce;\n        }\n\n        this.vx += (this.originX - this.x) * springForce;\n        this.vy += (this.originY - this.y) * springForce;\n\n        this.vx *= friction;\n        this.vy *= friction;\n\n        this.x += this.vx;\n        this.y += this.vy;\n      }\n\n      draw() {\n        ctx2.fillStyle = this.color;\n        ctx2.fillRect(this.x, this.y, pixelSize, pixelSize);\n      }\n    }\n\n    function initParticles(data: ImageData) {\n      particles = [];\n      const d = data.data;\n\n      for (let y = 0; y < canvas2.height; y += resolution) {\n        for (let x = 0; x < canvas2.width; x += resolution) {\n          const index = (y * canvas2.width + x) * 4;\n          const alpha = d[index + 3];\n          if (alpha > 10) {\n            const r = d[index];\n            const g = d[index + 1];\n            const b = d[index + 2];\n            particles.push(new Particle(x, y, `rgb(${r}, ${g}, ${b})`));\n          }\n        }\n      }\n    }\n\n    function animate() {\n      ctx2.clearRect(0, 0, canvas2.width, canvas2.height);\n      for (let i = 0; i < particles.length; i++) {\n        particles[i].update();\n        particles[i].draw();\n      }\n      animationFrameId = window.requestAnimationFrame(animate);\n    }\n\n    const getMousePos = (e: MouseEvent) => {\n      const rect = canvas2.getBoundingClientRect();\n      const scaleX = canvas2.width / rect.width;\n      const scaleY = canvas2.height / rect.height;\n      return {\n        x: (e.clientX - rect.left) * scaleX,\n        y: (e.clientY - rect.top) * scaleY,\n      };\n    };\n\n    const handleMouseMove = (e: MouseEvent) => {\n      const pos = getMousePos(e);\n      mouse.x = pos.x;\n      mouse.y = pos.y;\n    };\n\n    const handleMouseLeave = () => {\n      mouse.x = -1000;\n      mouse.y = -1000;\n    };\n\n    const handleMouseDown = (e: MouseEvent) => {\n      const pos = getMousePos(e);\n      for (const p of particles) {\n        const dx = pos.x - p.x;\n        const dy = pos.y - p.y;\n        const distance = Math.sqrt(dx * dx + dy * dy);\n\n        if (distance < clickRadius) {\n          const angle = Math.atan2(dy, dx);\n          const force = (clickRadius - distance) / clickRadius;\n          p.vx -= Math.cos(angle) * force * clickForce;\n          p.vy -= Math.sin(angle) * force * clickForce;\n        }\n      }\n    };\n\n    initParticles(imageData);\n    animate();\n\n    canvas2.addEventListener(\"mousemove\", handleMouseMove);\n    canvas2.addEventListener(\"mouseleave\", handleMouseLeave);\n    canvas2.addEventListener(\"mousedown\", handleMouseDown);\n\n    return () => {\n      window.cancelAnimationFrame(animationFrameId);\n      canvas2.removeEventListener(\"mousemove\", handleMouseMove);\n      canvas2.removeEventListener(\"mouseleave\", handleMouseLeave);\n      canvas2.removeEventListener(\"mousedown\", handleMouseDown);\n    };\n  }, [\n    text,\n    fontSize,\n    fontFamily,\n    fontWeight,\n    resolution,\n    pixelSize,\n    hoverRadius,\n    repelForce,\n    clickRadius,\n    clickForce,\n    springForce,\n    friction,\n    theme,\n    padding,\n    height,\n  ]);\n\n  return (\n    <div className=\"flex w-full items-center justify-center overflow-visible\">\n      <canvas\n        ref={canvasRef}\n        className=\"block max-w-full select-none\"\n        style={{ height: typeof height === \"number\" ? `${height}px` : \"auto\" }}\n      />\n    </div>\n  );\n}\n\nexport default TextParticleAnimation;\n"
    }
  ]
}