From b00566fb991abb7dbf1637869c875d5d94527ec7 Mon Sep 17 00:00:00 2001 From: vshnosngh Date: Thu, 5 Nov 2026 15:04:28 +0530 Subject: [PATCH] fix(performance): eliminate device thermal throttling and remove expand icon from trendy bento --- src/components/3d/HeroCrystalCanvas.tsx | 71 ++++++++++++++----- src/components/magicui/particles.tsx | 39 ++++++++-- src/components/magicui/smooth-cursor.tsx | 53 +++++++++++++- .../sections/HowItWorks3DSection.tsx | 34 ++------- .../sections/ShiftableToolsGridSection.tsx | 48 ++++++++++--- .../sections/TrendyBentoSection.tsx | 36 +--------- src/components/ui/SmoothScrollProvider.tsx | 36 ++++++++-- 7 files changed, 220 insertions(+), 97 deletions(-) diff --git a/src/components/3d/HeroCrystalCanvas.tsx b/src/components/3d/HeroCrystalCanvas.tsx index 2941c3b..f9ad055 100644 --- a/src/components/3d/HeroCrystalCanvas.tsx +++ b/src/components/3d/HeroCrystalCanvas.tsx @@ -128,23 +128,16 @@ export function HeroCrystalCanvas({ className }: HeroCrystalCanvasProps) { scene.add(indigoLight); let animId = 0; + let isRunning = false; const clock = new THREE.Clock(); let isVisible = true; - // IntersectionObserver to pause loop when scrolled out of view (0% CPU/GPU offscreen) - const observer = new IntersectionObserver( - (entries) => { - entries.forEach((entry) => { - isVisible = entry.isIntersecting; - }); - }, - { threshold: 0.05 } - ); - observer.observe(container); - const animate = () => { - animId = requestAnimationFrame(animate); - if (!isVisible) return; + if (!isVisible || document.hidden) { + isRunning = false; + animId = 0; + return; + } const elapsed = clock.getElapsedTime(); @@ -167,17 +160,60 @@ export function HeroCrystalCanvas({ className }: HeroCrystalCanvasProps) { try { renderer.render(scene, camera); } catch { - cancelAnimationFrame(animId); + isRunning = false; + animId = 0; + return; } } + + animId = requestAnimationFrame(animate); + }; + + const startLoop = () => { + if (isRunning || !isVisible || document.hidden) return; + isRunning = true; + animId = requestAnimationFrame(animate); + }; + + const stopLoop = () => { + isRunning = false; + if (animId) { + cancelAnimationFrame(animId); + animId = 0; + } }; - animate(); + // IntersectionObserver to pause loop completely when scrolled out of view (0% CPU/GPU offscreen) + const observer = new IntersectionObserver( + (entries) => { + entries.forEach((entry) => { + isVisible = entry.isIntersecting; + if (entry.isIntersecting) { + startLoop(); + } else { + stopLoop(); + } + }); + }, + { threshold: 0.05 } + ); + observer.observe(container); + + startLoop(); + + const handleVisibility = () => { + if (document.hidden) { + stopLoop(); + } else if (isVisible) { + startLoop(); + } + }; + document.addEventListener('visibilitychange', handleVisibility); const canvas = renderer.domElement; const handleContextLost = (e: Event) => { e.preventDefault(); - cancelAnimationFrame(animId); + stopLoop(); }; canvas.addEventListener('webglcontextlost', handleContextLost, false); @@ -193,8 +229,9 @@ export function HeroCrystalCanvas({ className }: HeroCrystalCanvasProps) { window.addEventListener('resize', handleResize, { passive: true }); return () => { - cancelAnimationFrame(animId); + stopLoop(); observer.disconnect(); + document.removeEventListener('visibilitychange', handleVisibility); canvas.removeEventListener('webglcontextlost', handleContextLost); window.removeEventListener('resize', handleResize); if (renderer && renderer.domElement && container.contains(renderer.domElement)) { diff --git a/src/components/magicui/particles.tsx b/src/components/magicui/particles.tsx index f0774d7..c7c4e84 100644 --- a/src/components/magicui/particles.tsx +++ b/src/components/magicui/particles.tsx @@ -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(null); const canvasContainerRef = useRef(null); const context = useRef(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(); diff --git a/src/components/magicui/smooth-cursor.tsx b/src/components/magicui/smooth-cursor.tsx index 14fd107..fd2f966 100644 --- a/src/components/magicui/smooth-cursor.tsx +++ b/src/components/magicui/smooth-cursor.tsx @@ -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]); diff --git a/src/components/sections/HowItWorks3DSection.tsx b/src/components/sections/HowItWorks3DSection.tsx index 0e9e94a..060fd06 100644 --- a/src/components/sections/HowItWorks3DSection.tsx +++ b/src/components/sections/HowItWorks3DSection.tsx @@ -1,6 +1,6 @@ 'use client'; -import React, { useState, useEffect } from 'react'; +import React from 'react'; import { Sparkles, Wand2, @@ -103,15 +103,6 @@ export function HowItWorks3DSection({ // Card 1: Upload Your Picture (Super Warm & Visual) // ------------------------------------------------------------- function StepUploadCard() { - const [bounce, setBounce] = useState(false); - - useEffect(() => { - const timer = setInterval(() => { - setBounce((prev) => !prev); - }, 2000); - return () => clearInterval(timer); - }, []); - return (
@@ -127,12 +118,8 @@ function StepUploadCard() {
{/* Animated floating dashed dropzone */}
- {/* Playful Floating Photo Frame */} -
+ {/* Playful Floating Photo Frame (GPU-composited CSS float, 0% JS overhead) */} +
Your Photo { - const interval = setInterval(() => { - setSparkleRot((prev) => (prev + 30) % 360); - }, 250); - return () => clearInterval(interval); - }, []); - return (
@@ -216,10 +194,10 @@ function StepMagicCard() {
- {/* Orbiting Stars & Sparkles */} + {/* Orbiting Stars & Sparkles (Pure CSS 60/120fps hardware rotation - 0% JS intervals!) */}
✨ ⭐ diff --git a/src/components/sections/ShiftableToolsGridSection.tsx b/src/components/sections/ShiftableToolsGridSection.tsx index 6a2124b..f4a572e 100644 --- a/src/components/sections/ShiftableToolsGridSection.tsx +++ b/src/components/sections/ShiftableToolsGridSection.tsx @@ -288,15 +288,47 @@ function ToolWindowMedia({ item, isHovered }: { item: ToolMediaItem; isHovered: } }, []); - // Continuous Autoplay Muted Loop (Plays all the time on phones and desktop without requiring hover) + // Continuous Autoplay Muted Loop when visible in viewport + // Pauses automatically when scrolled out of view or tab is hidden to save GPU/battery and eliminate device heating useEffect(() => { - if (item.mediaType === 'video' && videoRef.current) { - const video = videoRef.current; - video.muted = true; - video.defaultMuted = true; - video.playsInline = true; - video.play().catch(() => {}); - } + if (item.mediaType !== 'video') return; + const video = videoRef.current; + if (!video) return; + + video.muted = true; + video.defaultMuted = true; + video.playsInline = true; + + let isIntersecting = false; + + const updatePlayState = () => { + if (isIntersecting && !document.hidden) { + video.play().catch(() => {}); + } else { + video.pause(); + } + }; + + const observer = new IntersectionObserver( + ([entry]) => { + isIntersecting = entry.isIntersecting; + updatePlayState(); + }, + { threshold: 0.05 } + ); + + observer.observe(video); + + const handleVisibility = () => { + updatePlayState(); + }; + document.addEventListener('visibilitychange', handleVisibility); + + return () => { + observer.disconnect(); + document.removeEventListener('visibilitychange', handleVisibility); + video.pause(); + }; }, [item.mediaType]); // When unhovered, reset slider smoothly to 50% diff --git a/src/components/sections/TrendyBentoSection.tsx b/src/components/sections/TrendyBentoSection.tsx index d4cb343..a59a53f 100644 --- a/src/components/sections/TrendyBentoSection.tsx +++ b/src/components/sections/TrendyBentoSection.tsx @@ -11,8 +11,6 @@ import { SunMedium, Layers, ArrowRight, - Maximize2, - Minimize2, GripHorizontal, Smile, Camera, @@ -40,7 +38,6 @@ interface TileItem { badge: string; icon: React.ElementType; imageUrl: string; - isWide?: boolean; } const CATEGORIES: { id: EffectCategory; label: string }[] = [ @@ -170,24 +167,13 @@ export function TrendyBentoSection({ subtitle = 'Discover our collection of viral AI effects. Tap any category to explore specialized tools.', }: TrendyBentoSectionProps) { const [activeCategory, setActiveCategory] = useState('all'); - const [tiles, setTiles] = useState(INITIAL_TILES); + const tiles = INITIAL_TILES; // Filtered tiles based on category const displayedTiles = activeCategory === 'all' ? tiles : tiles.filter((t) => t.category === activeCategory); - // Toggle individual tile width (1x1 to 2x1) - const toggleTileSize = (id: string, e: React.MouseEvent) => { - e.preventDefault(); - e.stopPropagation(); - setTiles((prev) => - prev.map((tile) => - tile.id === id ? { ...tile, isWide: !tile.isWide } : tile - ) - ); - }; - return (
{/* Symbolic Elemental Ambient Background Animation */} @@ -252,9 +238,7 @@ export function TrendyBentoSection({
{displayedTiles.map((tile) => { const Icon = tile.icon; - const tileClasses = tile.isWide - ? 'col-span-1 sm:col-span-2 aspect-[16/9] sm:aspect-[2/1] rounded-[36px] sm:rounded-[42px]' - : 'col-span-1 aspect-square rounded-[36px] sm:rounded-[42px]'; + const tileClasses = 'col-span-1 aspect-square rounded-[36px] sm:rounded-[42px]'; return ( - {/* Top Tile Bar: Badge and Quick-Settings Resize Button */} + {/* Top Tile Bar: Badge */}
{tile.badge} - - {/* Android Quick Settings Size Toggle Button (1x1 to 2x1) */} -
{/* Bottom Tile Info & Link */} diff --git a/src/components/ui/SmoothScrollProvider.tsx b/src/components/ui/SmoothScrollProvider.tsx index 6b26651..b3eb76f 100644 --- a/src/components/ui/SmoothScrollProvider.tsx +++ b/src/components/ui/SmoothScrollProvider.tsx @@ -21,6 +21,7 @@ export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) { let lenis: any = null; let animId = 0; let isDisposed = false; + let visibilityHandler: (() => void) | null = null; // Detect mobile / touch devices: iOS (iPhone, iPad, iPod) and touch screens const isTouchDevice = @@ -51,12 +52,36 @@ export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) { (window as any).__lenis = lenis; - const raf = (time: number) => { - if (isDisposed) return; - lenis?.raf(time); + let isRunning = false; + const startRaf = () => { + if (isRunning || isDisposed || document.hidden) return; + isRunning = true; + const raf = (time: number) => { + if (isDisposed || document.hidden) { + isRunning = false; + animId = 0; + return; + } + lenis?.raf(time); + animId = requestAnimationFrame(raf); + }; animId = requestAnimationFrame(raf); }; - animId = requestAnimationFrame(raf); + + startRaf(); + + visibilityHandler = () => { + if (document.hidden) { + isRunning = false; + if (animId) { + cancelAnimationFrame(animId); + animId = 0; + } + } else { + startRaf(); + } + }; + document.addEventListener('visibilitychange', visibilityHandler); } catch { // Graceful fallback to native scrolling } @@ -104,6 +129,9 @@ export function SmoothScrollProvider({ children }: SmoothScrollProviderProps) { return () => { isDisposed = true; if (animId) cancelAnimationFrame(animId); + if (visibilityHandler) { + document.removeEventListener('visibilitychange', visibilityHandler); + } document.removeEventListener('click', handleAnchorClick, { capture: true }); if (lenis && typeof lenis.destroy === 'function') { lenis.destroy();