Optimize GradientBlinds: Pause animation when off-screen

- Added IntersectionObserver to track component visibility.
- Paused requestAnimationFrame loop when component is not intersecting.
- Resumed loop when component becomes visible.
- Added unit tests to verify start/stop behavior.
This commit is contained in:
google-labs-jules[bot]
2026-01-22 08:17:31 +00:00
parent dfd5461485
commit 1dc96269e5
2 changed files with 132 additions and 0 deletions

View File

@@ -333,11 +333,28 @@ void main() {
}
}
};
const observer = new IntersectionObserver(([entry]) => {
if (entry.isIntersecting) {
if (!rafRef.current) {
lastTimeRef.current = 0;
rafRef.current = requestAnimationFrame(loop);
}
} else {
if (rafRef.current) {
cancelAnimationFrame(rafRef.current);
rafRef.current = null;
}
}
});
observer.observe(container);
rafRef.current = requestAnimationFrame(loop);
return () => {
if (rafRef.current) cancelAnimationFrame(rafRef.current);
canvas.removeEventListener('pointermove', onPointerMove);
observer.disconnect();
ro.disconnect();
if (canvas.parentElement === container) {
container.removeChild(canvas);