3
3
*/
4
4
5
5
var Stats = function ( options ) {
6
+ // Use high resolution timing API, if available
7
+ window . performance = window . performance || { } ;
8
+ performance . now = performance . now || function ( ) { return new Date ( ) . getTime ( ) ; } ;
6
9
7
10
options = options || { } ;
8
11
9
12
var barHeight = options . barHeight || 30 ;
10
13
var bars = options . bars || 74 ;
11
14
12
- var startTime = Date . now ( ) , prevTime = startTime ;
15
+ var startTime = performance . now ( ) , prevTime = startTime ;
13
16
var ms = 0 , msMin = Infinity , msMax = 0 ;
14
17
var fps = 0 , fpsMin = Infinity , fpsMax = 0 ;
15
18
var frames = 0 , mode = 0 ;
16
19
17
20
var container = document . createElement ( 'div' ) ;
18
21
container . id = 'stats' ;
19
22
container . addEventListener ( 'mousedown' , function ( event ) { event . preventDefault ( ) ; setMode ( ++ mode % 2 ) ; } , false ) ;
20
- container . style . cssText = 'width:' + ( bars + 6 ) + 'px;opacity:0.9;cursor:pointer;font-family:Helvetica ,Arial,sans-serif ;font-size:9px;font-weight:bold;line-height:15px;text-align:left' ;
23
+ container . style . cssText = 'width:' + ( bars + 6 ) + 'px;opacity:0.9;cursor:pointer;font-family:Consolas ,Arial,monospace ;font-size:9px;font-weight:bold;line-height:15px;text-align:left' ;
21
24
22
25
var fpsDiv = document . createElement ( 'div' ) ;
23
26
fpsDiv . id = 'fps' ;
24
27
fpsDiv . style . cssText = 'padding:0 0 3px 3px;background-color:#002;color:#0ff' ;
25
28
container . appendChild ( fpsDiv ) ;
26
29
30
+ var fpsMinMax = document . createElement ( 'div' ) ;
31
+ fpsMinMax . id = 'fpsTextMinMax' ;
32
+ fpsMinMax . style . cssText = 'text-align:right;height:0;padding-right:3px' ;
33
+ fpsDiv . appendChild ( fpsMinMax ) ;
34
+
27
35
var fpsText = document . createElement ( 'div' ) ;
28
36
fpsText . id = 'fpsText' ;
29
37
fpsText . innerHTML = 'FPS' ;
@@ -47,6 +55,11 @@ var Stats = function (options) {
47
55
msDiv . style . cssText = 'padding:0 0 3px 3px;background-color:#020;display:none;color:#0f0;' ;
48
56
container . appendChild ( msDiv ) ;
49
57
58
+ var msMinMax = document . createElement ( 'div' ) ;
59
+ msMinMax . id = 'msTextMinMax' ;
60
+ msMinMax . style . cssText = 'text-align:right;height:0;padding-right:3px' ;
61
+ msDiv . appendChild ( msMinMax ) ;
62
+
50
63
var msText = document . createElement ( 'div' ) ;
51
64
msText . id = 'msText' ;
52
65
msText . innerHTML = 'MS' ;
@@ -100,30 +113,32 @@ var Stats = function (options) {
100
113
101
114
begin : function ( ) {
102
115
103
- startTime = Date . now ( ) ;
116
+ startTime = performance . now ( ) ;
104
117
105
118
} ,
106
119
107
120
end : function ( ) {
108
121
109
- var time = Date . now ( ) ;
122
+ var time = performance . now ( ) ;
110
123
111
124
ms = time - startTime ;
112
125
msMin = Math . min ( msMin , ms ) ;
113
126
msMax = Math . max ( msMax , ms ) ;
114
127
115
- msText . textContent = ms + ' MS (' + msMin + '-' + msMax + ')' ;
128
+ msText . textContent = ms . toFixed ( 1 ) + ' MS' ;
129
+ msMinMax . textContent = '(' + msMin . toFixed ( 1 ) + '-' + msMax . toFixed ( 1 ) + ')'
116
130
updateGraph ( msGraph , Math . min ( barHeight , barHeight - ( ms / 200 ) * barHeight ) ) ;
117
131
118
132
frames ++ ;
119
133
120
134
if ( time > prevTime + 1000 ) {
121
135
122
- fps = Math . round ( ( frames * 1000 ) / ( time - prevTime ) ) ;
136
+ fps = ( ( frames * 1000 ) / ( time - prevTime ) ) . toFixed ( 1 ) ;
123
137
fpsMin = Math . min ( fpsMin , fps ) ;
124
138
fpsMax = Math . max ( fpsMax , fps ) ;
125
139
126
- fpsText . textContent = fps + ' FPS (' + fpsMin + '-' + fpsMax + ')' ;
140
+ fpsText . textContent = fps + ' FPS' ;
141
+ fpsMinMax . textContent = '(' + fpsMin + '-' + fpsMax + ')' ;
127
142
updateGraph ( fpsGraph , Math . min ( barHeight , barHeight - ( fps / 100 ) * barHeight ) ) ;
128
143
129
144
prevTime = time ;
0 commit comments