mirror of
https://github.com/timmypidashev/web.git
synced 2026-04-14 11:03:50 +00:00
Fix requestAnimationFrame blurring background after switching views
This commit is contained in:
@@ -529,6 +529,24 @@ const Background: React.FC<BackgroundProps> = ({
|
|||||||
canvas.addEventListener('mouseup', handleMouseUp, { signal });
|
canvas.addEventListener('mouseup', handleMouseUp, { signal });
|
||||||
canvas.addEventListener('mouseleave', handleMouseLeave, { signal });
|
canvas.addEventListener('mouseleave', handleMouseLeave, { signal });
|
||||||
|
|
||||||
|
const handleVisibilityChange = () => {
|
||||||
|
if (document.hidden) {
|
||||||
|
// Tab is hidden
|
||||||
|
if (animationFrameRef.current) {
|
||||||
|
cancelAnimationFrame(animationFrameRef.current);
|
||||||
|
animationFrameRef.current = undefined;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Tab is visible again
|
||||||
|
if (!animationFrameRef.current) {
|
||||||
|
// Reset timing references to prevent catching up
|
||||||
|
lastUpdateTimeRef.current = performance.now();
|
||||||
|
lastCycleTimeRef.current = performance.now();
|
||||||
|
animationFrameRef.current = requestAnimationFrame(animate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const animate = (currentTime: number) => {
|
const animate = (currentTime: number) => {
|
||||||
if (signal.aborted) return;
|
if (signal.aborted) return;
|
||||||
|
|
||||||
@@ -540,6 +558,10 @@ const Background: React.FC<BackgroundProps> = ({
|
|||||||
|
|
||||||
// Calculate time since last frame
|
// Calculate time since last frame
|
||||||
const deltaTime = currentTime - lastUpdateTimeRef.current;
|
const deltaTime = currentTime - lastUpdateTimeRef.current;
|
||||||
|
|
||||||
|
// Limit delta time to prevent large jumps when tab becomes active again
|
||||||
|
const clampedDeltaTime = Math.min(deltaTime, 100);
|
||||||
|
|
||||||
lastUpdateTimeRef.current = currentTime;
|
lastUpdateTimeRef.current = currentTime;
|
||||||
|
|
||||||
// Calculate time since last cycle update
|
// Calculate time since last cycle update
|
||||||
@@ -552,7 +574,7 @@ const Background: React.FC<BackgroundProps> = ({
|
|||||||
lastCycleTimeRef.current = currentTime;
|
lastCycleTimeRef.current = currentTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateCellAnimations(gridRef.current, deltaTime);
|
updateCellAnimations(gridRef.current, clampedDeltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw frame
|
// Draw frame
|
||||||
@@ -641,11 +663,13 @@ const Background: React.FC<BackgroundProps> = ({
|
|||||||
animationFrameRef.current = requestAnimationFrame(animate);
|
animationFrameRef.current = requestAnimationFrame(animate);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
document.addEventListener('visibilitychange', handleVisibilityChange, { signal });
|
||||||
window.addEventListener('resize', handleResize, { signal });
|
window.addEventListener('resize', handleResize, { signal });
|
||||||
animate(performance.now());
|
animate(performance.now());
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
|
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
||||||
window.removeEventListener('resize', handleResize);
|
window.removeEventListener('resize', handleResize);
|
||||||
if (animationFrameRef.current) {
|
if (animationFrameRef.current) {
|
||||||
cancelAnimationFrame(animationFrameRef.current);
|
cancelAnimationFrame(animationFrameRef.current);
|
||||||
|
|||||||
Reference in New Issue
Block a user