forked from kof/animation-frame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFpsMeter.js
More file actions
45 lines (33 loc) · 868 Bytes
/
FpsMeter.js
File metadata and controls
45 lines (33 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(function() {
var now;
now = Date.now || function() {
return (new Date).getTime();
};
function FpsMeter(opts) {
opts || (opts = {});
this.updateRate = opts.updateRate || 1000;
this._tickCounter = 0;
this._updateTimeoutId = null;
}
FpsMeter.prototype.start = function(callback) {
var self = this;
this._startTime = now();
this._tickCounter = 0;
this._updateTimeoutId = setTimeout(function() {
if (self._tickCounter) {
callback(Math.round(1000 / ((now() - self._startTime) / self._tickCounter)));
}
self.start(callback);
}, this.updateRate);
return this;
};
FpsMeter.prototype.stop = function() {
clearTimeout(this._updateTimeoutId);
return this;
};
FpsMeter.prototype.tick = function() {
++this._tickCounter;
return this;
};
window.FpsMeter = FpsMeter;
}());