'use client'; import React, { useEffect, useRef } from 'react'; import { cn } from '@/lib/utils'; interface ParticlesProps { className?: string; quantity?: number; staticity?: number; ease?: number; size?: number; refresh?: boolean; color?: string; vx?: number; vy?: number; } export function Particles({ className = '', quantity = 40, staticity = 50, ease = 50, size = 0.5, refresh = false, color = '#3b82f6', vx = 0, vy = 0, }: ParticlesProps) { const canvasRef = useRef(null); const canvasContainerRef = useRef(null); const context = useRef(null); const circles = useRef([]); const mousePosition = useRef<{ x: number; y: number }>({ x: 0, y: 0 }); const mouse = useRef<{ x: number; y: number }>({ x: 0, y: 0 }); const canvasSize = useRef<{ w: number; h: number }>({ w: 0, h: 0 }); const dpr = typeof window !== 'undefined' ? Math.min(window.devicePixelRatio || 1, 2) : 1; useEffect(() => { if (canvasRef.current) { context.current = canvasRef.current.getContext('2d'); } initCanvas(); animate(); window.addEventListener('resize', initCanvas); return () => { window.removeEventListener('resize', initCanvas); }; }, [color]); const initCanvas = () => { resizeCanvas(); drawParticles(); }; const resizeCanvas = () => { if (canvasContainerRef.current && canvasRef.current && context.current) { circles.current.length = 0; canvasSize.current.w = canvasContainerRef.current.offsetWidth; canvasSize.current.h = canvasContainerRef.current.offsetHeight; canvasRef.current.width = canvasSize.current.w * dpr; canvasRef.current.height = canvasSize.current.h * dpr; canvasRef.current.style.width = `${canvasSize.current.w}px`; canvasRef.current.style.height = `${canvasSize.current.h}px`; context.current.scale(dpr, dpr); } }; const circleParams = (): any => { const x = Math.floor(Math.random() * canvasSize.current.w); const y = Math.floor(Math.random() * canvasSize.current.h); const translateX = 0; const translateY = 0; const pSize = Math.floor(Math.random() * 2) + size; const alpha = 0; const targetAlpha = parseFloat((Math.random() * 0.5 + 0.1).toFixed(1)); const dx = (Math.random() - 0.5) * 0.2; const dy = (Math.random() - 0.5) * 0.2; const magnetism = 0.1 + Math.random() * 4; return { x, y, translateX, translateY, size: pSize, alpha, targetAlpha, dx, dy, magnetism, }; }; const drawCircle = (circle: any, update = false) => { if (context.current) { const { x, y, translateX, translateY, size: pSize, alpha } = circle; context.current.translate(translateX, translateY); context.current.beginPath(); context.current.arc(x, y, pSize, 0, 2 * Math.PI); context.current.fillStyle = color; context.current.globalAlpha = alpha; context.current.fill(); context.current.setTransform(dpr, 0, 0, dpr, 0, 0); if (!update) { circles.current.push(circle); } } }; const drawParticles = () => { clearContext(); const particleCount = quantity; for (let i = 0; i < particleCount; i++) { const circle = circleParams(); drawCircle(circle); } }; const clearContext = () => { if (context.current) { context.current.clearRect( 0, 0, canvasSize.current.w, canvasSize.current.h ); } }; const animate = () => { clearContext(); circles.current.forEach((circle: any, i: number) => { // Handle edge collision const edge = [ circle.x + circle.translateX - circle.size < 0, circle.x + circle.translateX + circle.size > canvasSize.current.w, circle.y + circle.translateY - circle.size < 0, circle.y + circle.translateY + circle.size > canvasSize.current.h, ]; if (edge[0] || edge[1]) circle.dx = -circle.dx; if (edge[2] || edge[3]) circle.dy = -circle.dy; circle.x += circle.dx; circle.y += circle.dy; circle.translateX += ((mouse.current.x / (staticity / circle.magnetism)) - circle.translateX) / ease; circle.translateY += ((mouse.current.y / (staticity / circle.magnetism)) - circle.translateY) / ease; if (circle.alpha < circle.targetAlpha) { circle.alpha += 0.02; } drawCircle(circle, true); }); window.requestAnimationFrame(animate); }; return ( ); }