perf(thermal): eradicate thermal heating, CPU throttling, and excessive power draw across mobile and low-end devices

This commit is contained in:
2026-11-05 15:44:10 +05:30
parent 8715e23be0
commit fcd716463b
13 changed files with 396 additions and 321 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ export function Marquee({
pauseOnHover = true,
children,
vertical = false,
repeat = 4,
repeat = 2,
...props
}: MarqueeProps) {
return (
+76 -67
View File
@@ -14,79 +14,94 @@ interface ParticlesProps {
export function Particles({
className = '',
quantity = 35,
staticity = 50,
ease = 50,
quantity = 15,
size = 0.6,
color = '#3b82f6',
}: ParticlesProps) {
const [mounted, setMounted] = React.useState(false);
const [isMobile, setIsMobile] = React.useState(true);
const [isTouchOrMobile, setIsTouchOrMobile] = React.useState(true);
const canvasRef = useRef<HTMLCanvasElement>(null);
const canvasContainerRef = useRef<HTMLDivElement>(null);
const context = useRef<CanvasRenderingContext2D | null>(null);
const circles = useRef<any[]>([]);
const canvasSize = useRef<{ w: number; h: number }>({ w: 0, h: 0 });
const isVisibleRef = useRef<boolean>(true);
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);
// Completely disable on ANY mobile, tablet, or touchscreen device
const touchOrMobileDevice =
navigator.maxTouchPoints > 0 ||
'ontouchstart' in window ||
window.innerWidth < 1024 ||
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Tablet/i.test(navigator.userAgent) ||
(window.matchMedia && window.matchMedia('(pointer: coarse)').matches);
setIsTouchOrMobile(touchOrMobileDevice);
setMounted(true);
}, []);
useEffect(() => {
if (!mounted || isMobile) return;
// Zero CPU / GPU allocation on touch/mobile devices
if (!mounted || isTouchOrMobile) return;
const container = canvasContainerRef.current;
if (!container || !canvasRef.current) return;
context.current = canvasRef.current.getContext('2d');
if (!context.current) return;
initCanvas();
// IntersectionObserver to pause loop completely when scrolled out of view!
let animId = 0;
let isRunning = false;
let lastFrameTime = 0;
const targetInterval = 1000 / 24; // Strictly capped at 24fps for ultra-low power consumption
const animate = (timestamp: number) => {
if (!isVisibleRef.current || document.hidden) {
isRunning = false;
animId = 0;
return;
}
// Throttle to 24fps max (0% idle battery drain)
if (timestamp - lastFrameTime < targetInterval) {
animId = window.requestAnimationFrame(animate);
return;
}
lastFrameTime = timestamp;
if (context.current && canvasSize.current.w > 0) {
clearContext();
for (let i = 0; i < circles.current.length; i++) {
const circle = circles.current[i];
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);
};
const startAnimation = () => {
if (isRunning || !isVisibleRef.current || document.hidden) return;
isRunning = true;
const animate = () => {
if (!isVisibleRef.current || document.hidden) {
isRunning = false;
animId = 0;
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);
};
@@ -128,7 +143,6 @@ export function Particles({
resizeCanvas();
drawParticles();
};
window.addEventListener('resize', handleResize, { passive: true });
return () => {
@@ -137,10 +151,10 @@ export function Particles({
document.removeEventListener('visibilitychange', handleVisibility);
window.removeEventListener('resize', handleResize);
};
}, [mounted, isMobile, color]);
}, [mounted, isTouchOrMobile, color, quantity]);
// Completely omitted on phones and mobile touchscreens
if (!mounted || isMobile) {
if (!mounted || isTouchOrMobile) {
return null;
}
@@ -152,13 +166,15 @@ export function Particles({
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 w = canvasContainerRef.current.offsetWidth;
const h = canvasContainerRef.current.offsetHeight;
canvasSize.current.w = w;
canvasSize.current.h = h;
// Fixed 1.0 DPR: eliminates 4x retina CPU rasterization overhead
canvasRef.current.width = w;
canvasRef.current.height = h;
canvasRef.current.style.width = `${w}px`;
canvasRef.current.style.height = `${h}px`;
}
};
@@ -166,18 +182,10 @@ export function Particles({
const x = Math.floor(Math.random() * canvasSize.current.w);
const y = Math.floor(Math.random() * canvasSize.current.h);
const pSize = Math.floor(Math.random() * 2) + size;
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,
size: pSize,
alpha: 0,
targetAlpha,
dx,
dy,
};
const targetAlpha = parseFloat((Math.random() * 0.35 + 0.1).toFixed(2));
const dx = (Math.random() - 0.5) * 0.15;
const dy = (Math.random() - 0.5) * 0.15;
return { x, y, size: pSize, alpha: 0, targetAlpha, dx, dy };
};
const drawCircle = (circle: any, update = false) => {
@@ -197,13 +205,14 @@ export function Particles({
const drawParticles = () => {
clearContext();
for (let i = 0; i < quantity; i++) {
const count = Math.min(quantity, 18);
for (let i = 0; i < count; i++) {
drawCircle(circleParams());
}
};
const clearContext = () => {
if (context.current) {
if (context.current && canvasSize.current.w > 0) {
context.current.clearRect(0, 0, canvasSize.current.w, canvasSize.current.h);
}
};
+5 -4
View File
@@ -19,13 +19,14 @@ export function SmoothCursor() {
useEffect(() => {
if (typeof window === 'undefined') return;
// Detect phones, tablets, or touchscreen devices
// Detect phones, tablets, or touchscreen devices (zero touch overhead)
const isMobileOrTablet =
navigator.maxTouchPoints > 0 ||
'ontouchstart' in window ||
window.innerWidth < 1024 ||
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Tablet/i.test(navigator.userAgent) ||
(window.matchMedia && window.matchMedia('(pointer: coarse)').matches) ||
(window.matchMedia && !window.matchMedia('(hover: hover)').matches) ||
('ontouchstart' in window && window.innerWidth < 1024) ||
(navigator.maxTouchPoints > 0 && window.innerWidth < 1024);
(window.matchMedia && !window.matchMedia('(hover: hover)').matches);
const hasFinePointer =
window.matchMedia && window.matchMedia('(hover: hover) and (pointer: fine)').matches;