stats.module.js 3.4 KB

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