I am using engine.getDeltaTime ensure my simulation performs at a constant rate, but have found that if the tab loses focus, when the user refocuses a potentially huge getDeltaTime is returned.
My solution is below, just wondered if there was a better way.
let hasFocus = true;
let firstOnFocus=false;
engine.runRenderLoop(function ()
if (hasFocus){
if (firstOnFocus){
mainLoop.updateTime(0); // trash the DeltaTime if just returned focus
firstOnFocus=false;
} else {
mainLoop.updateTime(engine.getDeltaTime());
}
scene.render();
}
});
document.addEventListener(“visibilitychange”, function() {
if (document.visibilityState === ‘visible’) {
hasFocus=true;
firstOnFocus=true;
} else {
hasFocus=false;
firstOnFocus=false;
}
});