javascripthtmlloopscanvaselapsed

HTML5 Canvas - Elapsed Time when Pausing


I am having trouble with the delta/ elapsed time.

When the window is blurred the loop pauses correctly.

If you wait a few seconds and click back onto the window (focus), the elapsed time starts at a higher number and then resets back to 0.

How do I stop the elapsed time from increasing when the window is blurred? This higher number affects my animation and makes it run too fast until the delta is correct again.

I have setup an example loop. You can see what I mean in the console: Example

if (window.requestAnimationFrame !== undefined) {
    window.requestAnimFrame = (function () {
        'use strict';

        return window.requestAnimationFrame ||
               window.webkitRequestAnimationFrame ||
               window.oRequestAnimationFrame ||
               window.mozRequestAnimationFrame ||
               function (callback) {
                    window.setTimeout(callback, 1000 / 60); //Should be 60 FPS
               };
    }());
}

(function () {
    'use strict';

    var elapsed,
        isPaused = false,
        lastTime = 0,
        loop,
        setElapsed,
        startTime = 0,
        update;

    //Set the elapsed time
    setElapsed = function () {
        startTime = Date.now();
        elapsed = startTime - lastTime; //Calculate the elapsed time since the last frame. Dividing by 1000 turns it to seconds
        lastTime = startTime;
    };

    update = function () {
        //Update the animation etc.
        console.log('animation');
    };

    loop = function () {
        setElapsed();
        update(elapsed);    

        console.log('elapsed: ' + elapsed);

        requestAnimFrame(function () {
            if (isPaused === false) {
                loop();
            } 
        }); //Re-loop constantly using callback window.setTimeout(callback, 1000 / 60); //Should be 60 FPS

    };

    //When the window blurs, pause it
    window.onblur = function () {
        isPaused = true; //Pause the game
    };

    //When the window is in focus, resume it
    window.onfocus = function () {
        isPaused = false;
        loop(); //Re-init the game
    };

    loop();

}());

Thanks


Solution

  • elapsed assignment (inside setElapsed function) should use lastTime only when the latter is non-zero. Otherwise it should be set to 0 (which means the 1st call).

    Furthermore you have to reset lastTime to 0 when onblur event occurs.

    setElapsed = function () {
        startTime = Date.now();
        elapsed = lastTime? startTime - lastTime : 0;
        lastTime = startTime;
    };
    ...
    window.onblur = function () {
        isPaused = true; // Pause the game
        lastTime = 0; // reset lastTime
    };