Stop simulation when page is hidden
This commit is contained in:
parent
becafed267
commit
bc57e7c2ca
30
index.mjs
30
index.mjs
|
@ -45,6 +45,7 @@ for (let i = 0; i < 300; ++i)
|
|||
});
|
||||
}
|
||||
|
||||
let paused = false;
|
||||
let lastTime = null;
|
||||
|
||||
const loop = time =>
|
||||
|
@ -60,7 +61,34 @@ const loop = time =>
|
|||
boids.update(activeBoids, params, delta);
|
||||
draw.fill(activeBoids, params, boidsCtx, width, height, center);
|
||||
|
||||
if (!paused)
|
||||
{
|
||||
requestAnimationFrame(loop);
|
||||
}
|
||||
};
|
||||
|
||||
const start = () =>
|
||||
{
|
||||
lastTime = null;
|
||||
paused = false;
|
||||
requestAnimationFrame(loop);
|
||||
};
|
||||
|
||||
requestAnimationFrame(loop);
|
||||
const pause = () =>
|
||||
{
|
||||
paused = true;
|
||||
};
|
||||
|
||||
document.onvisibilitychange = () =>
|
||||
{
|
||||
if (document.visibilityState === 'visible')
|
||||
{
|
||||
start();
|
||||
}
|
||||
else
|
||||
{
|
||||
pause();
|
||||
}
|
||||
};
|
||||
|
||||
start();
|
||||
|
|
Loading…
Reference in New Issue