Calculate FPS in Canvas using requestAnimationFrame

前端 未结 10 2195
滥情空心
滥情空心 2020-11-30 00:31

How could I calculate the FPS of a canvas game application? I\'ve seen some examples, but none of them use requestAnimationFrame, and im not sure how to apply their solution

相关标签:
10条回答
  • 2020-11-30 01:08

    I have a different approach, because if you calculate the the FPS you'll get this flickering when returning the number. I decided to count every Frame and return it once a second

    window.countFPS = (function () {
      var lastLoop = (new Date()).getMilliseconds();
      var count = 1;
      var fps = 0;
    
      return function () {
        var currentLoop = (new Date()).getMilliseconds();
        if (lastLoop > currentLoop) {
          fps = count;
          count = 1;
        } else {
          count += 1;
        }
        lastLoop = currentLoop;
        return fps;
      };
    }());
    
    requestAnimationFrame(function () {
      console.log(countFPS());
    });
    

    jsfiddle

    0 讨论(0)
  • 2020-11-30 01:08

    Actually none of the answers were sufficient for me. Here is a better solution which:

    • Use's performance.now()
    • Calculates the actual average fps per second
    • Average per second and decimal places are configurable

    Code:

    // Options
    const outputEl         = document.getElementById('fps-output');
    const decimalPlaces    = 2;
    const updateEachSecond = 1;
    
    // Cache values
    const decimalPlacesRatio = Math.pow(10, decimalPlaces);
    let timeMeasurements     = [];
    
    // Final output
    let fps = 0;
    
    const tick = function() {
      timeMeasurements.push(performance.now());
    
      const msPassed = timeMeasurements[timeMeasurements.length - 1] - timeMeasurements[0];
    
      if (msPassed >= updateEachSecond * 1000) {
        fps = Math.round(timeMeasurements.length / msPassed * 1000 * decimalPlacesRatio) / decimalPlacesRatio;
        timeMeasurements = [];
      }
    
      outputEl.innerText = fps;
    
      requestAnimationFrame(() => {
        tick();
      });
    }
    
    tick();
    

    JSFiddle

    0 讨论(0)
  • 2020-11-30 01:13

    Chrome has a built-in fps counter: https://developer.chrome.com/devtools/docs/rendering-settings

    Just open the dev-console (F12), open the drawer (Esc), and add the "Rendering" tab.

    Here, you can activate the FPS-Meter overlay to see the current framerate (incl. a nice graph), as well as GPU memory consumption.

    Cross-browser solution: You can get a similar overlay with the JavaScript library stat.js: https://github.com/mrdoob/stats.js/

    It also provides a nice overlay for the framerate (incl. graph) and is very easy to use.

    When comparing the results from stats.js and the chrome dev tools, both show the exact same measurements. So you can trust that library to actually do the correct thing.

    0 讨论(0)
  • 2020-11-30 01:21

    Do not use new Date()

    This API has several flaws and is only useful for getting the current date + time. Not for measuring timespans.

    The Date-API uses the operating system's internal clock, which is constantly updated and synchronized with NTP time servers. This means, that the speed / frequency of this clock is sometimes faster and sometimes slower than the actual time - and therefore not useable for measuring durations and framerates.

    If someone changes the system time (either manually or due to DST), you could at least see the problem if a single frame suddenly needed an hour. Or a negative time. But if the system clock ticks 20% faster to synchronize with world-time, it is practically impossible to detect.

    Also, the Date-API is very imprecise - often much less than 1ms. This makes it especially useless for framerate measurements, where one 60Hz frame needs ~17ms.

    Instead, use performance.now()

    The Performance API has been specificly made for such use cases and can be used equivalently to new Date(). Just take one of the other answers and replace new Date() with performance.now(), and you are ready to go.

    Sources:

    Also unlike Date.now(), the values returned by Performance.now() always increase at a constant rate, independent of the system clock (which might be adjusted manually or skewed by software like NTP). Otherwise, performance.timing.navigationStart + performance.now() will be approximately equal to Date.now().

    https://developer.mozilla.org/en-US/docs/Web/API/Performance/now

    And for windows:

    [The time service] adjusts the local clock rate to allow it to converge toward the correct time. If the time difference between the local clock and the [accurate time sample] is too large to correct by adjusting the local clock rate, the time service sets the local clock to the correct time.

    https://technet.microsoft.com/en-us/library/cc773013(v=ws.10).aspx

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