stats.module.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var Stats = function () {
  5. var mode = 0;
  6. var container = document.createElement( 'div' );
  7. container.style.cssText = 'position:fixed;top:0;left:0;cursor:pointer;opacity:0.9;z-index:10000';
  8. container.addEventListener( 'click', function ( event ) {
  9. event.preventDefault();
  10. showPanel( ++ mode % container.children.length );
  11. }, false );
  12. //
  13. function addPanel( panel ) {
  14. container.appendChild( panel.dom );
  15. return panel;
  16. }
  17. function showPanel( id ) {
  18. for ( var i = 0; i < container.children.length; i ++ ) {
  19. container.children[ i ].style.display = i === id ? 'block' : 'none';
  20. }
  21. mode = id;
  22. }
  23. //
  24. var beginTime = ( performance || Date ).now(), prevTime = beginTime, frames = 0;
  25. var fpsPanel = addPanel( new Stats.Panel( 'FPS', '#0ff', '#002' ) );
  26. var msPanel = addPanel( new Stats.Panel( 'MS', '#0f0', '#020' ) );
  27. if ( self.performance && self.performance.memory ) {
  28. var memPanel = addPanel( new Stats.Panel( 'MB', '#f08', '#201' ) );
  29. }
  30. showPanel( 0 );
  31. return {
  32. REVISION: 16,
  33. dom: container,
  34. addPanel: addPanel,
  35. showPanel: showPanel,
  36. begin: function () {
  37. beginTime = ( performance || Date ).now();
  38. },
  39. end: function () {
  40. frames ++;
  41. var time = ( performance || Date ).now();
  42. msPanel.update( time - beginTime, 200 );
  43. if ( time >= prevTime + 1000 ) {
  44. fpsPanel.update( ( frames * 1000 ) / ( time - prevTime ), 100 );
  45. prevTime = time;
  46. frames = 0;
  47. if ( memPanel ) {
  48. var memory = performance.memory;
  49. memPanel.update( memory.usedJSHeapSize / 1048576, memory.jsHeapSizeLimit / 1048576 );
  50. }
  51. }
  52. return time;
  53. },
  54. update: function () {
  55. beginTime = this.end();
  56. },
  57. // Backwards Compatibility
  58. domElement: container,
  59. setMode: showPanel
  60. };
  61. };
  62. Stats.Panel = function ( name, fg, bg ) {
  63. var min = Infinity, max = 0, round = Math.round;
  64. var PR = round( window.devicePixelRatio || 1 );
  65. var WIDTH = 80 * PR, HEIGHT = 48 * PR,
  66. TEXT_X = 3 * PR, TEXT_Y = 2 * PR,
  67. GRAPH_X = 3 * PR, GRAPH_Y = 15 * PR,
  68. GRAPH_WIDTH = 74 * PR, GRAPH_HEIGHT = 30 * PR;
  69. var canvas = document.createElement( 'canvas' );
  70. canvas.width = WIDTH;
  71. canvas.height = HEIGHT;
  72. canvas.style.cssText = 'width:80px;height:48px';
  73. var context = canvas.getContext( '2d' );
  74. context.font = 'bold ' + ( 9 * PR ) + 'px Helvetica,Arial,sans-serif';
  75. context.textBaseline = 'top';
  76. context.fillStyle = bg;
  77. context.fillRect( 0, 0, WIDTH, HEIGHT );
  78. context.fillStyle = fg;
  79. context.fillText( name, TEXT_X, TEXT_Y );
  80. context.fillRect( GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT );
  81. context.fillStyle = bg;
  82. context.globalAlpha = 0.9;
  83. context.fillRect( GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT );
  84. return {
  85. dom: canvas,
  86. update: function ( value, maxValue ) {
  87. min = Math.min( min, value );
  88. max = Math.max( max, value );
  89. context.fillStyle = bg;
  90. context.globalAlpha = 1;
  91. context.fillRect( 0, 0, WIDTH, GRAPH_Y );
  92. context.fillStyle = fg;
  93. context.fillText( round( value ) + ' ' + name + ' (' + round( min ) + '-' + round( max ) + ')', TEXT_X, TEXT_Y );
  94. context.drawImage( canvas, GRAPH_X + PR, GRAPH_Y, GRAPH_WIDTH - PR, GRAPH_HEIGHT, GRAPH_X, GRAPH_Y, GRAPH_WIDTH - PR, GRAPH_HEIGHT );
  95. context.fillRect( GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, GRAPH_HEIGHT );
  96. context.fillStyle = bg;
  97. context.globalAlpha = 0.9;
  98. context.fillRect( GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, round( ( 1 - ( value / maxValue ) ) * GRAPH_HEIGHT ) );
  99. }
  100. };
  101. };
  102. export default Stats;