|
1 | 1 | /* |
2 | | - * stats.js r3 |
| 2 | + * stats.js r4 |
3 | 3 | * http://github.com/mrdoob/stats.js |
4 | 4 | * |
5 | 5 | * Released under MIT license: |
|
10 | 10 | * var stats = new Stats(); |
11 | 11 | * parentElement.appendChild(stats.domElement); |
12 | 12 | * |
13 | | - * setInterval(loop, 1000/60); |
| 13 | + * setInterval(function () { |
14 | 14 | * |
15 | | - * function loop() { |
16 | | - * stats.update(); |
17 | | - * } |
| 15 | + * stats.update(); |
| 16 | + * |
| 17 | + * }, 1000/60); |
18 | 18 | * |
19 | 19 | */ |
20 | 20 |
|
21 | 21 | var Stats = function () { |
22 | 22 |
|
23 | | - var _frames, _framesMin, _framesMax, _time, _timePrev, |
24 | | - _container, _text, _canvas, _context, _imageData; |
25 | | - |
26 | | - _frames = 0; |
27 | | - _framesMin = 1000; |
28 | | - _framesMax = 0; |
29 | | - |
30 | | - _time = new Date().getTime(); |
31 | | - _timePrev = _time; |
32 | | - |
33 | | - _container = document.createElement('div'); |
| 23 | + var _container, _mode = 'fps', |
| 24 | + _frames = 0, _time = new Date().getTime(), _timeLastFrame = _time, _timeLastSecond = _time, |
| 25 | + _fps = 0, _fpsMin = 1000, _fpsMax = 0, _fpsText, _fpsCanvas, _fpsContext, _fpsImageData, |
| 26 | + _ms = 0, _msMin = 1000, _msMax = 0, _msText, _msCanvas, _msContext, _msImageData; |
| 27 | + |
| 28 | + _container = document.createElement( 'div' ); |
34 | 29 | _container.style.fontFamily = 'Helvetica, Arial, sans-serif'; |
35 | 30 | _container.style.fontSize = '9px'; |
36 | 31 | _container.style.backgroundColor = '#000020'; |
37 | 32 | _container.style.opacity = '0.9'; |
38 | 33 | _container.style.width = '80px'; |
39 | 34 | _container.style.paddingTop = '2px'; |
40 | | - |
41 | | - _text = document.createElement('div'); |
42 | | - _text.style.color = '#00ffff'; |
43 | | - _text.style.marginLeft = '3px'; |
44 | | - _text.style.marginBottom = '3px'; |
45 | | - _text.innerHTML = '<strong>FPS</strong>'; |
46 | | - _container.appendChild(_text); |
47 | | - |
48 | | - _canvas = document.createElement('canvas'); |
49 | | - _canvas.width = 74; |
50 | | - _canvas.height = 30; |
51 | | - _canvas.style.display = 'block'; |
52 | | - _canvas.style.marginLeft = '3px'; |
53 | | - _canvas.style.marginBottom = '3px'; |
54 | | - _container.appendChild(_canvas); |
55 | | - |
56 | | - _context = _canvas.getContext('2d'); |
57 | | - _context.fillStyle = '#101030'; |
58 | | - _context.fillRect(0, 0, _canvas.width, _canvas.height); |
59 | | - |
60 | | - _imageData = _context.getImageData(0, 0, _canvas.width, _canvas.height); |
61 | | - |
| 35 | + _container.style.cursor = 'pointer'; |
| 36 | + _container.addEventListener( 'click', swapMode, false ); |
| 37 | + |
| 38 | + _fpsText = document.createElement( 'div' ); |
| 39 | + _fpsText.innerHTML = '<strong>FPS</strong>'; |
| 40 | + _fpsText.style.color = '#00ffff'; |
| 41 | + _fpsText.style.marginLeft = '3px'; |
| 42 | + _fpsText.style.marginBottom = '3px'; |
| 43 | + _container.appendChild(_fpsText); |
| 44 | + |
| 45 | + _fpsCanvas = document.createElement( 'canvas' ); |
| 46 | + _fpsCanvas.width = 74; |
| 47 | + _fpsCanvas.height = 30; |
| 48 | + _fpsCanvas.style.display = 'block'; |
| 49 | + _fpsCanvas.style.marginLeft = '3px'; |
| 50 | + _fpsCanvas.style.marginBottom = '3px'; |
| 51 | + _container.appendChild(_fpsCanvas); |
| 52 | + |
| 53 | + _fpsContext = _fpsCanvas.getContext( '2d' ); |
| 54 | + _fpsContext.fillStyle = '#101030'; |
| 55 | + _fpsContext.fillRect(0, 0, _fpsCanvas.width, _fpsCanvas.height); |
| 56 | + |
| 57 | + _fpsImageData = _fpsContext.getImageData( 0, 0, _fpsCanvas.width, _fpsCanvas.height ); |
| 58 | + |
| 59 | + _msText = document.createElement( 'div' ); |
| 60 | + _msText.innerHTML = '<strong>MS</strong>'; |
| 61 | + _msText.style.color = '#00ffff'; |
| 62 | + _msText.style.marginLeft = '3px'; |
| 63 | + _msText.style.marginBottom = '3px'; |
| 64 | + _msText.style.display = 'none'; |
| 65 | + _container.appendChild(_msText); |
| 66 | + |
| 67 | + _msCanvas = document.createElement( 'canvas' ); |
| 68 | + _msCanvas.width = 74; |
| 69 | + _msCanvas.height = 30; |
| 70 | + _msCanvas.style.display = 'block'; |
| 71 | + _msCanvas.style.marginLeft = '3px'; |
| 72 | + _msCanvas.style.marginBottom = '3px'; |
| 73 | + _msCanvas.style.display = 'none'; |
| 74 | + _container.appendChild(_msCanvas); |
| 75 | + |
| 76 | + _msContext = _msCanvas.getContext( '2d' ); |
| 77 | + _msContext.fillStyle = '#101030'; |
| 78 | + _msContext.fillRect(0, 0, _msCanvas.width, _msCanvas.height); |
| 79 | + |
| 80 | + _msImageData = _msContext.getImageData( 0, 0, _msCanvas.width, _msCanvas.height ); |
| 81 | + |
| 82 | + function updateGraph( data, value ) { |
| 83 | + |
| 84 | + var x, y, index; |
| 85 | + |
| 86 | + for ( y = 0; y < 30; y++ ) { |
| 87 | + |
| 88 | + for ( x = 0; x < 73; x++ ) { |
| 89 | + |
| 90 | + index = (x + y * 74) * 4; |
| 91 | + |
| 92 | + data[ index ] = data[ index + 4 ]; |
| 93 | + data[ index + 1 ] = data[ index + 5 ]; |
| 94 | + data[ index + 2 ] = data[ index + 6 ]; |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + for ( y = 0; y < 30; y++ ) { |
| 101 | + |
| 102 | + index = (73 + y * 74) * 4; |
| 103 | + |
| 104 | + if ( y < value ) { |
| 105 | + |
| 106 | + data[ index ] = 16; |
| 107 | + data[ index + 1 ] = 16; |
| 108 | + data[ index + 2 ] = 48; |
| 109 | + |
| 110 | + } else { |
| 111 | + |
| 112 | + data[ index ] = 0; |
| 113 | + data[ index + 1 ] = 255; |
| 114 | + data[ index + 2 ] = 255; |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + function swapMode() { |
| 123 | + |
| 124 | + switch( _mode ) { |
| 125 | + |
| 126 | + case 'fps': |
| 127 | + |
| 128 | + _mode = 'ms'; |
| 129 | + |
| 130 | + _fpsText.style.display = 'none'; |
| 131 | + _fpsCanvas.style.display = 'none'; |
| 132 | + _msText.style.display = 'inline'; |
| 133 | + _msCanvas.style.display = 'inline'; |
| 134 | + |
| 135 | + break; |
| 136 | + |
| 137 | + case 'ms': |
| 138 | + |
| 139 | + _mode = 'fps'; |
| 140 | + |
| 141 | + _fpsText.style.display = 'inline'; |
| 142 | + _fpsCanvas.style.display = 'inline'; |
| 143 | + _msText.style.display = 'none'; |
| 144 | + _msCanvas.style.display = 'none'; |
| 145 | + |
| 146 | + break; |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | + |
62 | 152 | return { |
63 | | - |
| 153 | + |
64 | 154 | domElement: _container, |
65 | | - |
| 155 | + |
66 | 156 | update: function () { |
67 | 157 |
|
68 | | - var fps, index; |
| 158 | + _frames ++; |
69 | 159 |
|
70 | 160 | _time = new Date().getTime(); |
71 | | - _frames += 1; |
72 | | - |
73 | | - if (_time >= _timePrev + 1000) { |
74 | | - |
75 | | - fps = Math.round((_frames * 1000) / (_time - _timePrev)); |
76 | | - |
77 | | - _framesMin = Math.min(_framesMin, fps); |
78 | | - _framesMax = Math.max(_framesMax, fps); |
79 | | - |
80 | | - _text.innerHTML = '<strong>' + fps + ' FPS</strong> (' + _framesMin + '-' + _framesMax + ')'; |
81 | | - |
82 | | - _imageData = _context.getImageData(1, 0, _canvas.width - 1, 30); |
83 | | - _context.putImageData(_imageData, 0, 0); |
84 | | - |
85 | | - _context.fillStyle = '#101030'; |
86 | | - _context.fillRect(_canvas.width - 1, 0, 1, 30); |
87 | | - |
88 | | - index = Math.floor(30 - Math.min(30, (fps / 60) * 30)); |
89 | | - |
90 | | - _context.fillStyle = '#80ffff'; |
91 | | - _context.fillRect(_canvas.width - 1, index, 1, 1); |
92 | | - |
93 | | - _context.fillStyle = '#00ffff'; |
94 | | - _context.fillRect(_canvas.width - 1, index + 1, 1, 30 - index); |
95 | | - |
96 | | - _timePrev = _time; |
| 161 | + |
| 162 | + _ms = _time - _timeLastFrame; |
| 163 | + _msMin = Math.min(_msMin, _ms); |
| 164 | + _msMax = Math.max(_msMax, _ms); |
| 165 | + |
| 166 | + updateGraph( _msImageData.data, Math.min( 30, 30 - ( _ms / 200 ) * 30 ) ); |
| 167 | + |
| 168 | + _msText.innerHTML = '<strong>' + _ms + ' MS</strong> (' + _msMin + '-' + _msMax + ')'; |
| 169 | + _msContext.putImageData( _msImageData, 0, 0 ); |
| 170 | + |
| 171 | + _timeLastFrame = _time; |
| 172 | + |
| 173 | + if ( _time > _timeLastSecond + 1000 ) { |
| 174 | + |
| 175 | + _fps = Math.round((_frames * 1000) / (_time - _timeLastSecond)); |
| 176 | + _fpsMin = Math.min(_fpsMin, _fps); |
| 177 | + _fpsMax = Math.max(_fpsMax, _fps); |
| 178 | + |
| 179 | + updateGraph( _fpsImageData.data, Math.min( 30, 30 - ( _fps / 100 ) * 30 ) ); |
| 180 | + |
| 181 | + _fpsText.innerHTML = '<strong>' + _fps + ' FPS</strong> (' + _fpsMin + '-' + _fpsMax + ')'; |
| 182 | + _fpsContext.putImageData( _fpsImageData, 0, 0 ); |
| 183 | + |
| 184 | + _timeLastSecond = _time; |
97 | 185 | _frames = 0; |
| 186 | + |
98 | 187 | } |
| 188 | + |
99 | 189 | } |
| 190 | + |
100 | 191 | }; |
| 192 | + |
101 | 193 | }; |
0 commit comments