计算帧率的简单方法

通过requestAnimationFrame方法简单计算帧率,代码如下:
var calculateFPS = (function() {

var lastTime = 0;
var fps = 0;
var count = 0;
var callback;
return function calcFPS() {
    if (count++ === 100) {
        calcFPS.callback(fps);
    }
    var now = +(new Date());
    var newFps = 1000 / (now - lastTime);
    lastTime = now;
    if (!fps) {
        fps = newFps;
        requestAnimationFrame(calcFPS);
    } else {
        if (Math.abs(fps - newFps) < 1e-6) {
            calcFPS.callback(fps);
        } else {
            fps = newFps;
            requestAnimationFrame(calcFPS);
        }
    }        
}

})();
calculateFPS.callback = (fps) => {

console.log(fps)

}
calculateFPS()

    原文作者:helloweilei
    原文地址: https://segmentfault.com/a/1190000020193736
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞