Canvas requestAnimationFrame pause

前端 未结 1 1664
猫巷女王i
猫巷女王i 2021-02-19 12:25

How to pause canvas animation made with requestAnimationFrame ? I start animation like this:

Code:

window.requestAnimFrame = (function() {
    return  w         


        
相关标签:
1条回答
  • 2021-02-19 12:48

    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();
    
    0 讨论(0)
提交回复
热议问题