From 51afbf573bff8b18ecf375a26267d1377d7dc384 Mon Sep 17 00:00:00 2001 From: vshnosngh Date: Wed, 4 Nov 2026 18:48:24 +0530 Subject: [PATCH] perf(animations): refactor ShiftableToolsGrid, cursor, hero & sections to locked 60-120fps with zero layout thrashing --- src/components/magicui/particles.tsx | 80 ++++--- src/components/magicui/smooth-cursor.tsx | 45 ++-- src/components/sections/FaqSection.tsx | 6 +- src/components/sections/HeroSection.tsx | 18 +- .../sections/RecommendedAppsSection.tsx | 11 +- .../sections/ReviewsMarqueeSection.tsx | 2 +- .../sections/ShiftableToolsGridSection.tsx | 206 ++++++++++++------ .../sections/TrendyBentoSection.tsx | 18 +- src/components/ui/Navbar.tsx | 10 +- 9 files changed, 249 insertions(+), 147 deletions(-) diff --git a/src/components/magicui/particles.tsx b/src/components/magicui/particles.tsx index b33c9ad..f0774d7 100644 --- a/src/components/magicui/particles.tsx +++ b/src/components/magicui/particles.tsx @@ -35,45 +35,69 @@ export function Particles({ context.current = canvasRef.current.getContext('2d'); initCanvas(); - // IntersectionObserver to pause loop when scrolled out of view! + // IntersectionObserver to pause loop completely when scrolled out of view! let animId = 0; + let isRunning = false; + + const startAnimation = () => { + if (isRunning) return; + isRunning = true; + const animate = () => { + if (!isVisibleRef.current) { + isRunning = false; + return; + } + if (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 stopAnimation = () => { + isRunning = false; + if (animId) { + cancelAnimationFrame(animId); + animId = 0; + } + }; + const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { isVisibleRef.current = entry.isIntersecting; + if (entry.isIntersecting) { + startAnimation(); + } else { + stopAnimation(); + } }); }, { 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); + startAnimation(); const handleResize = () => { resizeCanvas(); diff --git a/src/components/magicui/smooth-cursor.tsx b/src/components/magicui/smooth-cursor.tsx index daf8485..add6808 100644 --- a/src/components/magicui/smooth-cursor.tsx +++ b/src/components/magicui/smooth-cursor.tsx @@ -56,32 +56,31 @@ export function SmoothCursor() { const target = e.target as HTMLElement | null; if (!target) return; const interactive = target.closest('a, button, input, textarea, select, [role="button"], .cursor-pointer'); - isHovered = !!interactive; + const nextHovered = !!interactive; - if (isHovered) { - ring.style.width = '52px'; - ring.style.height = '52px'; - ring.style.borderColor = 'rgba(37, 99, 235, 0.7)'; - ring.style.backgroundColor = 'rgba(37, 99, 235, 0.12)'; - dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0) scale(0)`; - } else { - ring.style.width = '30px'; - ring.style.height = '30px'; - ring.style.borderColor = 'rgba(37, 99, 235, 0.4)'; - ring.style.backgroundColor = 'rgba(37, 99, 235, 0.05)'; + if (nextHovered !== isHovered) { + isHovered = nextHovered; + if (isHovered) { + ring.style.borderColor = 'rgba(37, 99, 235, 0.7)'; + ring.style.backgroundColor = 'rgba(37, 99, 235, 0.12)'; + } else { + ring.style.borderColor = 'rgba(37, 99, 235, 0.35)'; + ring.style.backgroundColor = 'rgba(37, 99, 235, 0.04)'; + } } }; - // Ultra-smooth 120fps/60fps RAF lerp loop (Zero React re-renders!) + // Locked 60-120fps RAF lerp loop (Zero React re-renders, 100% GPU composited transforms) const loop = () => { // Smooth linear interpolation for trailing ring ringX += (mouseX - ringX) * 0.22; ringY += (mouseY - ringY) * 0.22; - ring.style.transform = `translate3d(${ringX}px, ${ringY}px, 0) translate(-50%, -50%)`; - if (!isHovered) { - dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0) translate(-50%, -50%) scale(1)`; - } + const ringScale = isHovered ? 1.45 : 1; + ring.style.transform = `translate3d(${ringX}px, ${ringY}px, 0) translate3d(-50%, -50%, 0) scale(${ringScale})`; + + const dotScale = isHovered ? 0 : 1; + dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0) translate3d(-50%, -50%, 0) scale(${dotScale})`; animId = requestAnimationFrame(loop); }; @@ -104,20 +103,16 @@ export function SmoothCursor() { return (
- {/* Trailing Fluid Squircle Ring */} + {/* Trailing Fluid Squircle Ring (GPU-scale transform only, zero layout reflows) */}
- {/* Precision Dot */} + {/* Precision Dot (GPU-scale transform only) */}
); diff --git a/src/components/sections/FaqSection.tsx b/src/components/sections/FaqSection.tsx index 6c52aff..d4036b6 100644 --- a/src/components/sections/FaqSection.tsx +++ b/src/components/sections/FaqSection.tsx @@ -78,16 +78,16 @@ export function FaqSection({ return (