动画,FPS和KineticJS

时间:2013-01-18 17:01:52

标签: javascript animation kineticjs frame-rate requestanimationframe

所以我在屏幕上移动了一个小球,我想计算FPS。我正在使用KineticJS(4.3.1),它利用了引擎盖下的requestAnimationFrame。

      var anim = new Kinetic.Animation(
          function(frame) {

            //game logic
               //move ball
               //check collisions - if collision occurs, stop animation
               //redraw 

          }
      }

'frame'对象有一个time属性,可以用frame.time访问,它测量自动画首次启动以来的时间,以毫秒为单位。

     var timeSinceAnimationStarted = frame.time;

测量FPS的准确方法是什么?

2 个答案:

答案 0 :(得分:2)

一个简单的实现,“1s间隔内的帧”。你可以使用5s间隔的帧来平滑它

// variables accessible from within function(frame)
var frameCount = 0;
var currentSecond = 0;
var frameRate = 0;

// within function(frame), called with current time on each new frame
function updateFrameRate(time) {
    var second = Math.floor(time / 1000); // ms to integer seconds
    if (second != currentSecond) {
       frameRate = frameCount;
       frameCount = 0;
       currentSecond = second;
    }
    frameCount ++;
}

答案 1 :(得分:0)

建议的结构似乎是:

  var numFrames = 0;
  var anim = new Kinetic.Animation(
      function(frame) {

        //game logic
           //move ball
           //check collisions - if collision occurs, stop animation
           //redraw 
        numFrames++;
      }
  }

  setInterval(function(){
     alert(numFrames/1000);  //number of frames per second
     numFrames = 0;  //reset frame count
  },1000);   //every second

会是这种情况吗?