fix(performance): eliminate device thermal throttling and remove expand icon from trendy bento
This commit is contained in:
@@ -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)) {
|
||||
|
||||
@@ -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]);
|
||||
|
||||
|
||||
@@ -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 (
|
||||
<div className="group relative flex flex-col justify-between rounded-[36px] bg-white border border-slate-200/90 p-7 shadow-cream-sm hover:shadow-cream-lg transition-all duration-300 hover:-translate-y-1.5 hover:border-blue-400/60">
|
||||
<div>
|
||||
@@ -127,12 +118,8 @@ function StepUploadCard() {
|
||||
<div className="relative w-full h-[230px] rounded-3xl bg-gradient-to-b from-blue-50/70 to-indigo-50/40 border border-blue-100 overflow-hidden flex flex-col items-center justify-center p-4 select-none">
|
||||
{/* Animated floating dashed dropzone */}
|
||||
<div className="w-full h-full border-2 border-dashed border-blue-300/80 rounded-2xl flex flex-col items-center justify-center p-4 relative">
|
||||
{/* Playful Floating Photo Frame */}
|
||||
<div
|
||||
className={`w-36 h-28 rounded-2xl bg-white shadow-xl border-2 border-white overflow-hidden p-1.5 transition-transform duration-700 ${
|
||||
bounce ? 'translate-y-0 rotate-1 scale-105' : '-translate-y-2 -rotate-2 scale-100'
|
||||
}`}
|
||||
>
|
||||
{/* Playful Floating Photo Frame (GPU-composited CSS float, 0% JS overhead) */}
|
||||
<div className="w-36 h-28 rounded-2xl bg-white shadow-xl border-2 border-white overflow-hidden p-1.5 animate-float-slow transform-gpu">
|
||||
<img
|
||||
src="/management/pages/home/sections/how-it-works/media/before.jpg"
|
||||
alt="Your Photo"
|
||||
@@ -185,15 +172,6 @@ function StepUploadCard() {
|
||||
// Card 2: The Magic Happens (Playful & Magical)
|
||||
// -------------------------------------------------------------
|
||||
function StepMagicCard() {
|
||||
const [sparkleRot, setSparkleRot] = useState(0);
|
||||
|
||||
useEffect(() => {
|
||||
const interval = setInterval(() => {
|
||||
setSparkleRot((prev) => (prev + 30) % 360);
|
||||
}, 250);
|
||||
return () => clearInterval(interval);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="group relative flex flex-col justify-between rounded-[36px] bg-white border border-slate-200/90 p-7 shadow-cream-sm hover:shadow-cream-lg transition-all duration-300 hover:-translate-y-1.5 hover:border-amber-400/60">
|
||||
<div>
|
||||
@@ -216,10 +194,10 @@ function StepMagicCard() {
|
||||
<Wand2 className="w-10 h-10 text-amber-500 animate-bounce" />
|
||||
</div>
|
||||
|
||||
{/* Orbiting Stars & Sparkles */}
|
||||
{/* Orbiting Stars & Sparkles (Pure CSS 60/120fps hardware rotation - 0% JS intervals!) */}
|
||||
<div
|
||||
className="absolute inset-[-20px] pointer-events-none transition-transform duration-300"
|
||||
style={{ transform: `rotate(${sparkleRot}deg)` }}
|
||||
className="absolute inset-[-20px] pointer-events-none animate-spin"
|
||||
style={{ animationDuration: '8s' }}
|
||||
>
|
||||
<span className="absolute top-0 left-1/2 -translate-x-1/2 text-amber-400 text-base">✨</span>
|
||||
<span className="absolute bottom-0 left-1/2 -translate-x-1/2 text-purple-400 text-sm">⭐</span>
|
||||
|
||||
@@ -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%
|
||||
|
||||
@@ -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<EffectCategory>('all');
|
||||
const [tiles, setTiles] = useState<TileItem[]>(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 (
|
||||
<section id="trendy-effects" className="py-14 sm:py-20 px-4 sm:px-6 md:px-8 lg:px-10 bg-[#FAF8F5] relative overflow-hidden border-t border-slate-900/[0.06]">
|
||||
{/* Symbolic Elemental Ambient Background Animation */}
|
||||
@@ -252,9 +238,7 @@ export function TrendyBentoSection({
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
|
||||
{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 (
|
||||
<Link
|
||||
@@ -279,25 +263,11 @@ export function TrendyBentoSection({
|
||||
{/* Vignette Overlay for Readability (GPU Opacity) */}
|
||||
<div className="absolute inset-0 bg-gradient-to-t from-slate-950/90 via-slate-950/30 to-slate-950/20 opacity-100 group-hover:opacity-85 transition-opacity duration-500 ease-[cubic-bezier(0.16,1,0.3,1)] pointer-events-none" />
|
||||
|
||||
{/* Top Tile Bar: Badge and Quick-Settings Resize Button */}
|
||||
{/* Top Tile Bar: Badge */}
|
||||
<div className="relative z-20 flex items-center justify-between p-5">
|
||||
<span className="px-3 py-1 rounded-full text-[10px] font-mono font-bold tracking-wider uppercase bg-black/60 backdrop-blur-md text-cyan-400 border border-cyan-500/30 shadow-sm">
|
||||
{tile.badge}
|
||||
</span>
|
||||
|
||||
{/* Android Quick Settings Size Toggle Button (1x1 to 2x1) */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={(e) => toggleTileSize(tile.id, e)}
|
||||
title={tile.isWide ? 'Shrink tile' : 'Expand tile'}
|
||||
className="w-9 h-9 rounded-2xl bg-white/90 hover:bg-white text-slate-800 shadow-md flex items-center justify-center transition-[transform,background-color] duration-200 ease-[cubic-bezier(0.16,1,0.3,1)] hover:scale-110 active:scale-95 z-30"
|
||||
>
|
||||
{tile.isWide ? (
|
||||
<Minimize2 className="w-4 h-4 text-blue-600" />
|
||||
) : (
|
||||
<Maximize2 className="w-4 h-4 text-blue-600" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Bottom Tile Info & Link */}
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user