diff --git a/src/components/magicui/particles.tsx b/src/components/magicui/particles.tsx index 928ffdc..b33c9ad 100644 --- a/src/components/magicui/particles.tsx +++ b/src/components/magicui/particles.tsx @@ -9,42 +9,83 @@ interface ParticlesProps { staticity?: number; ease?: number; size?: number; - refresh?: boolean; color?: string; - vx?: number; - vy?: number; } export function Particles({ className = '', - quantity = 40, + quantity = 35, staticity = 50, ease = 50, - size = 0.5, - refresh = false, + size = 0.6, 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 isVisibleRef = useRef(true); const dpr = typeof window !== 'undefined' ? Math.min(window.devicePixelRatio || 1, 2) : 1; useEffect(() => { - if (canvasRef.current) { - context.current = canvasRef.current.getContext('2d'); - } + const container = canvasContainerRef.current; + if (!container || !canvasRef.current) return; + + context.current = canvasRef.current.getContext('2d'); initCanvas(); - animate(); - window.addEventListener('resize', initCanvas); + + // IntersectionObserver to pause loop when scrolled out of view! + let animId = 0; + const observer = new IntersectionObserver( + (entries) => { + entries.forEach((entry) => { + isVisibleRef.current = entry.isIntersecting; + }); + }, + { threshold: 0.05 } + ); + observer.observe(container); + + const animate = () => { + if (isVisibleRef.current && context.current) { + clearContext(); + circles.current.forEach((circle: any) => { + const edge = [ + circle.x + circle.size < 0, + circle.x - circle.size > canvasSize.current.w, + circle.y + circle.size < 0, + circle.y - 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; + + if (circle.alpha < circle.targetAlpha) { + circle.alpha += 0.015; + } + + drawCircle(circle, true); + }); + } + animId = window.requestAnimationFrame(animate); + }; + + animId = window.requestAnimationFrame(animate); + + const handleResize = () => { + resizeCanvas(); + drawParticles(); + }; + + window.addEventListener('resize', handleResize, { passive: true }); return () => { - window.removeEventListener('resize', initCanvas); + cancelAnimationFrame(animId); + observer.disconnect(); + window.removeEventListener('resize', handleResize); }; }, [color]); @@ -69,38 +110,29 @@ export function Particles({ 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; + const targetAlpha = parseFloat((Math.random() * 0.4 + 0.1).toFixed(2)); + const dx = (Math.random() - 0.5) * 0.18; + const dy = (Math.random() - 0.5) * 0.18; return { x, y, - translateX, - translateY, size: pSize, - alpha, + alpha: 0, 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); + const { x, y, size: pSize, alpha } = circle; 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); @@ -110,51 +142,17 @@ export function Particles({ const drawParticles = () => { clearContext(); - const particleCount = quantity; - for (let i = 0; i < particleCount; i++) { - const circle = circleParams(); - drawCircle(circle); + for (let i = 0; i < quantity; i++) { + drawCircle(circleParams()); } }; const clearContext = () => { if (context.current) { - context.current.clearRect( - 0, - 0, - canvasSize.current.w, - canvasSize.current.h - ); + 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 (