perf(animations): refactor ShiftableToolsGrid, cursor, hero & sections to locked 60-120fps with zero layout thrashing

This commit is contained in:
2026-11-04 18:48:24 +05:30
parent d9ef1497a3
commit 51afbf573b
9 changed files with 249 additions and 147 deletions
+52 -28
View File
@@ -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();
+20 -25
View File
@@ -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 (
<div className="pointer-events-none fixed inset-0 z-[9999] overflow-hidden select-none">
{/* Trailing Fluid Squircle Ring */}
{/* Trailing Fluid Squircle Ring (GPU-scale transform only, zero layout reflows) */}
<div
ref={cursorRingRef}
className="fixed top-0 left-0 rounded-full border border-blue-500/40 bg-blue-500/5 backdrop-blur-[0.5px] pointer-events-none opacity-0 transition-[width,height,background-color,border-color] duration-200 ease-out will-change-transform"
style={{
width: '30px',
height: '30px',
}}
className="fixed top-0 left-0 w-8 h-8 rounded-full border border-blue-500/35 bg-blue-500/4 pointer-events-none opacity-0 transition-[border-color,background-color] duration-300 ease-[cubic-bezier(0.16,1,0.3,1)] will-change-transform transform-gpu [backface-visibility:hidden]"
/>
{/* Precision Dot */}
{/* Precision Dot (GPU-scale transform only) */}
<div
ref={cursorDotRef}
className="fixed top-0 left-0 w-2 h-2 rounded-full bg-blue-600 shadow-sm pointer-events-none opacity-0 transition-transform duration-100 ease-out will-change-transform"
className="fixed top-0 left-0 w-2 h-2 rounded-full bg-blue-600 shadow-sm pointer-events-none opacity-0 will-change-transform transform-gpu [backface-visibility:hidden]"
/>
</div>
);