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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user