Stop simulation when page is hidden

This commit is contained in:
Mattéo Delabre 2020-05-04 18:37:33 +02:00
parent becafed267
commit bc57e7c2ca
Signed by: matteo
GPG Key ID: AE3FBD02DC583ABB
1 changed files with 29 additions and 1 deletions

View File

@ -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();