fix(performance): eliminate device thermal throttling and remove expand icon from trendy bento
This commit is contained in:
@@ -20,6 +20,8 @@ export function Particles({
|
||||
size = 0.6,
|
||||
color = '#3b82f6',
|
||||
}: ParticlesProps) {
|
||||
const [mounted, setMounted] = React.useState(false);
|
||||
const [isMobile, setIsMobile] = React.useState(true);
|
||||
const canvasRef = useRef<HTMLCanvasElement>(null);
|
||||
const canvasContainerRef = useRef<HTMLDivElement>(null);
|
||||
const context = useRef<CanvasRenderingContext2D | null>(null);
|
||||
@@ -29,6 +31,19 @@ export function Particles({
|
||||
const dpr = typeof window !== 'undefined' ? Math.min(window.devicePixelRatio || 1, 2) : 1;
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window === 'undefined') return;
|
||||
const mobileDevice =
|
||||
window.innerWidth < 1024 ||
|
||||
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ||
|
||||
('ontouchstart' in window && window.innerWidth < 1024) ||
|
||||
(window.matchMedia && window.matchMedia('(pointer: coarse)').matches && !window.matchMedia('(pointer: fine)').matches);
|
||||
|
||||
setIsMobile(mobileDevice);
|
||||
setMounted(true);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!mounted || isMobile) return;
|
||||
const container = canvasContainerRef.current;
|
||||
if (!container || !canvasRef.current) return;
|
||||
|
||||
@@ -40,11 +55,12 @@ export function Particles({
|
||||
let isRunning = false;
|
||||
|
||||
const startAnimation = () => {
|
||||
if (isRunning) return;
|
||||
if (isRunning || !isVisibleRef.current || document.hidden) return;
|
||||
isRunning = true;
|
||||
const animate = () => {
|
||||
if (!isVisibleRef.current) {
|
||||
if (!isVisibleRef.current || document.hidden) {
|
||||
isRunning = false;
|
||||
animId = 0;
|
||||
return;
|
||||
}
|
||||
if (context.current) {
|
||||
@@ -99,6 +115,15 @@ export function Particles({
|
||||
|
||||
startAnimation();
|
||||
|
||||
const handleVisibility = () => {
|
||||
if (document.hidden) {
|
||||
stopAnimation();
|
||||
} else if (isVisibleRef.current) {
|
||||
startAnimation();
|
||||
}
|
||||
};
|
||||
document.addEventListener('visibilitychange', handleVisibility);
|
||||
|
||||
const handleResize = () => {
|
||||
resizeCanvas();
|
||||
drawParticles();
|
||||
@@ -107,11 +132,17 @@ export function Particles({
|
||||
window.addEventListener('resize', handleResize, { passive: true });
|
||||
|
||||
return () => {
|
||||
cancelAnimationFrame(animId);
|
||||
stopAnimation();
|
||||
observer.disconnect();
|
||||
document.removeEventListener('visibilitychange', handleVisibility);
|
||||
window.removeEventListener('resize', handleResize);
|
||||
};
|
||||
}, [color]);
|
||||
}, [mounted, isMobile, color]);
|
||||
|
||||
// Completely omitted on phones and mobile touchscreens
|
||||
if (!mounted || isMobile) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const initCanvas = () => {
|
||||
resizeCanvas();
|
||||
|
||||
@@ -62,6 +62,13 @@ export function SmoothCursor() {
|
||||
let isVisible = false;
|
||||
let animId = 0;
|
||||
|
||||
// Start RAF loop only when needed
|
||||
const startLoop = () => {
|
||||
if (!animId && isVisible && !document.hidden) {
|
||||
animId = requestAnimationFrame(loop);
|
||||
}
|
||||
};
|
||||
|
||||
// Desktop Mouse & Pointer Movement
|
||||
const onMouseMove = (e: MouseEvent) => {
|
||||
mouseX = e.clientX;
|
||||
@@ -73,6 +80,7 @@ export function SmoothCursor() {
|
||||
dot.style.opacity = '1';
|
||||
if (caustic) caustic.style.opacity = '1';
|
||||
}
|
||||
startLoop();
|
||||
};
|
||||
|
||||
const onMouseLeave = () => {
|
||||
@@ -80,6 +88,10 @@ export function SmoothCursor() {
|
||||
lens.style.opacity = '0';
|
||||
dot.style.opacity = '0';
|
||||
if (caustic) caustic.style.opacity = '0';
|
||||
if (animId) {
|
||||
cancelAnimationFrame(animId);
|
||||
animId = 0;
|
||||
}
|
||||
};
|
||||
|
||||
const onMouseEnter = () => {
|
||||
@@ -87,14 +99,17 @@ export function SmoothCursor() {
|
||||
lens.style.opacity = '1';
|
||||
dot.style.opacity = '1';
|
||||
if (caustic) caustic.style.opacity = '1';
|
||||
startLoop();
|
||||
};
|
||||
|
||||
const onMouseDown = () => {
|
||||
isPressed = true;
|
||||
startLoop();
|
||||
};
|
||||
|
||||
const onMouseUp = () => {
|
||||
isPressed = false;
|
||||
startLoop();
|
||||
};
|
||||
|
||||
// Throttled interactive element detection for magnetic lens scaling
|
||||
@@ -110,11 +125,20 @@ export function SmoothCursor() {
|
||||
if (!element || typeof element.closest !== 'function') return;
|
||||
|
||||
const interactive = element.closest('a, button, input, textarea, select, [role="button"], .cursor-pointer, [data-interactive]');
|
||||
const wasHovered = isHovered;
|
||||
isHovered = !!interactive;
|
||||
if (wasHovered !== isHovered) {
|
||||
startLoop();
|
||||
}
|
||||
};
|
||||
|
||||
// High performance RAF loop (locked 60-120fps, 100% GPU composited)
|
||||
// High performance RAF loop (sleeps when cursor is idle, wakes on movement)
|
||||
const loop = () => {
|
||||
if (document.hidden || !isVisible) {
|
||||
animId = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
const vx = mouseX - prevMouseX;
|
||||
const vy = mouseY - prevMouseY;
|
||||
prevMouseX = mouseX;
|
||||
@@ -153,26 +177,49 @@ export function SmoothCursor() {
|
||||
const dotScale = isHovered ? 0 : (isPressed ? 0.7 : 1);
|
||||
dot.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0) translate3d(-50%, -50%, 0) scale(${dotScale})`;
|
||||
|
||||
// Idle sleep check: if mouse stopped moving and lens reached position, pause RAF loop!
|
||||
const dist = Math.hypot(mouseX - lensX, mouseY - lensY);
|
||||
if (dist < 0.1 && currentSpeed < 0.02) {
|
||||
lensX = mouseX;
|
||||
lensY = mouseY;
|
||||
lens.style.transform = `translate3d(${lensX}px, ${lensY}px, 0) translate3d(-50%, -50%, 0) scale(${baseScale})`;
|
||||
animId = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
animId = requestAnimationFrame(loop);
|
||||
};
|
||||
|
||||
const onVisibilityChange = () => {
|
||||
if (document.hidden) {
|
||||
if (animId) {
|
||||
cancelAnimationFrame(animId);
|
||||
animId = 0;
|
||||
}
|
||||
} else if (isVisible) {
|
||||
startLoop();
|
||||
}
|
||||
};
|
||||
|
||||
window.addEventListener('mousemove', onMouseMove, { passive: true });
|
||||
window.addEventListener('mouseleave', onMouseLeave);
|
||||
window.addEventListener('mouseenter', onMouseEnter);
|
||||
window.addEventListener('mousedown', onMouseDown);
|
||||
window.addEventListener('mouseup', onMouseUp);
|
||||
document.addEventListener('mouseover', onMouseOver, { passive: true });
|
||||
document.addEventListener('visibilitychange', onVisibilityChange);
|
||||
|
||||
animId = requestAnimationFrame(loop);
|
||||
startLoop();
|
||||
|
||||
return () => {
|
||||
cancelAnimationFrame(animId);
|
||||
if (animId) cancelAnimationFrame(animId);
|
||||
window.removeEventListener('mousemove', onMouseMove);
|
||||
window.removeEventListener('mouseleave', onMouseLeave);
|
||||
window.removeEventListener('mouseenter', onMouseEnter);
|
||||
window.removeEventListener('mousedown', onMouseDown);
|
||||
window.removeEventListener('mouseup', onMouseUp);
|
||||
document.removeEventListener('mouseover', onMouseOver);
|
||||
document.removeEventListener('visibilitychange', onVisibilityChange);
|
||||
};
|
||||
}, [isEnabled]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user