How to pause canvas animation made with requestAnimationFrame ? I start animation like this:
Code:
window.requestAnimFrame = (function() {
    return  window.requestAnimationFrame       ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame    ||
            function(callback) {
                window.setTimeout(callback, 1000 / 60);
            };
})();
function Start() {
    Update();
    requestAnimFrame(Start);
}
Start();
Now I want to add pause option after keydown. Is there any simple way to do it?
What you could do is create a variable that stores the state of your animation: paused or unpaused. Change that state every time you click a button. Something like this should work:
var isPaused = false;
window.requestAnimFrame = (function() {
    return  window.requestAnimationFrame       ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame    ||
            function(callback) {
                window.setTimeout(callback, 1000 / 60);
            };
})();
function Start() {
    if (isPaused) {
        Update();
    }
    requestAnimFrame(Start);
}
window.onkeydown = function() {
    isPaused = !isPaused; // flips the pause state
};
Start();
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With