Stats.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * stats.js r4
  3. * http://github.com/mrdoob/stats.js
  4. *
  5. * Released under MIT license:
  6. * http://www.opensource.org/licenses/mit-license.php
  7. *
  8. * How to use:
  9. *
  10. * var stats = new Stats();
  11. * parentElement.appendChild(stats.domElement);
  12. *
  13. * setInterval(function () {
  14. *
  15. * stats.update();
  16. *
  17. * }, 1000/60);
  18. *
  19. */
  20. var Stats = function () {
  21. var _container, _mode = 'fps',
  22. _frames = 0, _time = new Date().getTime(), _timeLastFrame = _time, _timeLastSecond = _time,
  23. _fps = 0, _fpsMin = 1000, _fpsMax = 0, _fpsText, _fpsCanvas, _fpsContext, _fpsImageData,
  24. _ms = 0, _msMin = 1000, _msMax = 0, _msText, _msCanvas, _msContext, _msImageData;
  25. _container = document.createElement( 'div' );
  26. _container.style.fontFamily = 'Helvetica, Arial, sans-serif';
  27. _container.style.fontSize = '9px';
  28. _container.style.backgroundColor = '#000020';
  29. _container.style.opacity = '0.9';
  30. _container.style.width = '80px';
  31. _container.style.paddingTop = '2px';
  32. _container.style.cursor = 'pointer';
  33. _container.addEventListener( 'click', swapMode, false );
  34. _fpsText = document.createElement( 'div' );
  35. _fpsText.innerHTML = '<strong>FPS</strong>';
  36. _fpsText.style.color = '#00ffff';
  37. _fpsText.style.marginLeft = '3px';
  38. _fpsText.style.marginBottom = '3px';
  39. _container.appendChild(_fpsText);
  40. _fpsCanvas = document.createElement( 'canvas' );
  41. _fpsCanvas.width = 74;
  42. _fpsCanvas.height = 30;
  43. _fpsCanvas.style.display = 'block';
  44. _fpsCanvas.style.marginLeft = '3px';
  45. _fpsCanvas.style.marginBottom = '3px';
  46. _container.appendChild(_fpsCanvas);
  47. _fpsContext = _fpsCanvas.getContext( '2d' );
  48. _fpsContext.fillStyle = '#101030';
  49. _fpsContext.fillRect( 0, 0, _fpsCanvas.width, _fpsCanvas.height );
  50. _fpsImageData = _fpsContext.getImageData( 0, 0, _fpsCanvas.width, _fpsCanvas.height );
  51. _msText = document.createElement( 'div' );
  52. _msText.innerHTML = '<strong>MS</strong>';
  53. _msText.style.color = '#00ffff';
  54. _msText.style.marginLeft = '3px';
  55. _msText.style.marginBottom = '3px';
  56. _msText.style.display = 'none';
  57. _container.appendChild(_msText);
  58. _msCanvas = document.createElement( 'canvas' );
  59. _msCanvas.width = 74;
  60. _msCanvas.height = 30;
  61. _msCanvas.style.display = 'block';
  62. _msCanvas.style.marginLeft = '3px';
  63. _msCanvas.style.marginBottom = '3px';
  64. _msCanvas.style.display = 'none';
  65. _container.appendChild(_msCanvas);
  66. _msContext = _msCanvas.getContext( '2d' );
  67. _msContext.fillStyle = '#101030';
  68. _msContext.fillRect( 0, 0, _msCanvas.width, _msCanvas.height );
  69. _msImageData = _msContext.getImageData( 0, 0, _msCanvas.width, _msCanvas.height );
  70. function updateGraph( data, value ) {
  71. var x, y, index;
  72. for ( y = 0; y < 30; y++ ) {
  73. for ( x = 0; x < 73; x++ ) {
  74. index = (x + y * 74) * 4;
  75. data[ index ] = data[ index + 4 ];
  76. data[ index + 1 ] = data[ index + 5 ];
  77. data[ index + 2 ] = data[ index + 6 ];
  78. }
  79. }
  80. for ( y = 0; y < 30; y++ ) {
  81. index = (73 + y * 74) * 4;
  82. if ( y < value ) {
  83. data[ index ] = 16;
  84. data[ index + 1 ] = 16;
  85. data[ index + 2 ] = 48;
  86. } else {
  87. data[ index ] = 0;
  88. data[ index + 1 ] = 255;
  89. data[ index + 2 ] = 255;
  90. }
  91. }
  92. }
  93. function swapMode() {
  94. switch( _mode ) {
  95. case 'fps':
  96. _mode = 'ms';
  97. _fpsText.style.display = 'none';
  98. _fpsCanvas.style.display = 'none';
  99. _msText.style.display = 'block';
  100. _msCanvas.style.display = 'block';
  101. break;
  102. case 'ms':
  103. _mode = 'fps';
  104. _fpsText.style.display = 'block';
  105. _fpsCanvas.style.display = 'block';
  106. _msText.style.display = 'none';
  107. _msCanvas.style.display = 'none';
  108. break;
  109. }
  110. }
  111. return {
  112. domElement: _container,
  113. update: function () {
  114. _frames ++;
  115. _time = new Date().getTime();
  116. _ms = _time - _timeLastFrame;
  117. _msMin = Math.min( _msMin, _ms );
  118. _msMax = Math.max( _msMax, _ms );
  119. updateGraph( _msImageData.data, Math.min( 30, 30 - ( _ms / 200 ) * 30 ) );
  120. _msText.innerHTML = '<strong>' + _ms + ' MS</strong> (' + _msMin + '-' + _msMax + ')';
  121. _msContext.putImageData( _msImageData, 0, 0 );
  122. _timeLastFrame = _time;
  123. if ( _time > _timeLastSecond + 1000 ) {
  124. _fps = Math.round( ( _frames * 1000) / ( _time - _timeLastSecond ) );
  125. _fpsMin = Math.min( _fpsMin, _fps );
  126. _fpsMax = Math.max( _fpsMax, _fps );
  127. updateGraph( _fpsImageData.data, Math.min( 30, 30 - ( _fps / 100 ) * 30 ) );
  128. _fpsText.innerHTML = '<strong>' + _fps + ' FPS</strong> (' + _fpsMin + '-' + _fpsMax + ')';
  129. _fpsContext.putImageData( _fpsImageData, 0, 0 );
  130. _timeLastSecond = _time;
  131. _frames = 0;
  132. }
  133. }
  134. };
  135. };