requestAnimationFrame at a limited framerate

前端 未结 3 1180
梦毁少年i
梦毁少年i 2020-12-30 04:16

As I understand it, usage of the JS requestAnimationFrame API is intended for cases where the framerate isn\'t in need of being controlled, but I have a use cas

相关标签:
3条回答
  • 2020-12-30 05:10

    Yoshi's answer is probably the best code solution to this problem. But still I'll mark this answer as correct, because after some research I basically found that my question was invalid. requestAnimationFrame is really meant to keep frame rates as high as possible, and it optimizes for scenarios where animation is meant to be kept consistent and smooth.

    Worth noting though is that you don't need requestAnimationFrame to get optimization (even though rAF was touted as a great performance booster) since browsers still optimize regular drawing of a <canvas>. For example, when a tab isn't focused, Chrome for one stops drawing its canvases.

    So my conclusion was that this question was invalid. Hope this helps anyone who was wondering something similar to me.

    0 讨论(0)
  • 2020-12-30 05:11

    This is just a proof of concept.

    All we do is set our frames per second and intervals between each frame. In the drawing function we deduct our last frame’s execution time from the current time to check whether the time elapsed since the last frame is more than our interval (which is based on the fps) or not. If the condition evaluates to true, we set the time for our current frame which is going to be the “last frame execution time” in the next drawing call.

    var Timer = function(callback, fps){
      var now = 0;
      var delta = 0;
      var then = Date.now();
    
      var frames = 0;
      var oldtime = 0;
    
      fps = 1000 / (this.fps || fps || 60);
    
      return requestAnimationFrame(function loop(time){
        requestAnimationFrame(loop);
    
        now = Date.now();
        delta = now - then;
    
        if (delta > fps) {
          // Update time stuffs
          then = now - (delta % fps);
    
          // Calculate the frames per second.
          frames = 1000 / (time - oldtime)
          oldtime = time;
    
          // Call the callback-function and pass
          // our current frame into it.
          callback(frames);
        }
      });
    };
    

    Usage:

    var set;
    document.onclick = function(){
      set = true;
    };
    
    Timer(function(fps){
      if(set) this.fps = 30;
      console.log(fps);
    }, 5);
    

    http://jsfiddle.net/ARTsinn/rPAeN/

    0 讨论(0)
  • 2020-12-30 05:13

    What you can do, though I don't really know if this is really any better is:

    • render to an invisible context with requestAnimationFrame
    • update a visible context with setInterval using a fixed fps

    Example:

    <canvas id="canvas"></canvas>​
    
    <script type="text/javascript">
      (function () {
        var
          ctxVisible = document.getElementById('canvas').getContext('2d'),
          ctxHidden = document.createElement('canvas').getContext('2d');
    
        // quick anim sample
        (function () {
          var x = 0, y = 75;
    
          (function animLoop() {
            // too lazy to use a polyfill here
            webkitRequestAnimationFrame(animLoop);
    
            ctxHidden.clearRect(0, 0, 300, 150);
            ctxHidden.fillStyle = 'black';
            ctxHidden.fillRect(x - 1, y - 1, 3, 3);
    
            x += 1;
            if (x > 300) {
              x = 0;
            }
          }());
        }());
    
        // copy the hidden ctx to the visible ctx on a fixed interval (25 fps)
        setInterval(function () {
          ctxVisible.putImageData(ctxHidden.getImageData(0, 0, ctxHidden.canvas.width, ctxHidden.canvas.height), 0, 0);
        }, 1000/40);
      }());
    </script>
    

    Demo: http://jsfiddle.net/54vWN/

    0 讨论(0)
提交回复
热议问题