-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfps.js
40 lines (39 loc) · 1.05 KB
/
fps.js
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
/**
** 得到浏览器每秒帧数fps
**
** @Date Mar 13 2013
**/
var showFPS = (function(){
var requestAnimationFrame =
window.requestAnimationFrame || //Chromium
window.webkitRequestAnimationFrame || //Webkit
window.mozRequestAnimationFrame || //Mozilla Geko
window.oRequestAnimationFrame || //Opera Presto
window.msRequestAnimationFrame || //IE Trident?
function(callback) { //Fallback function
window.setTimeout(callback, 1000/60);
};
var frameRateElm = document.getElementById('frameRate')
var e,offset;
var fps = 0;
var last = Date.now();
var step = function(){
offset = Date.now() - last;
fps += 1;
if( offset >= 1000 ){
last += offset;
appendFps(fps);
fps = 0;
}
requestAnimationFrame( step );
};
var appendFps = function(fps){
if(!e) e=document.createElement('span');
e.innerHTML = "fps: " + fps;
frameRateElm.appendChild(e);
}
return {
go: function(){step();}
}
})();
showFPS.go()