Skip to content

Commit

Permalink
Added ES6 module support. (mrdoob#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 authored and mrdoob committed May 30, 2019
1 parent 1b71f88 commit 9d23c79
Show file tree
Hide file tree
Showing 6 changed files with 223 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var Stats = function () {

msPanel.update( time - beginTime, 200 );

if ( time > prevTime + 1000 ) {
if ( time >= prevTime + 1000 ) {

fpsPanel.update( ( frames * 1000 ) / ( time - prevTime ), 100 );

Expand Down
2 changes: 1 addition & 1 deletion build/stats.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

171 changes: 171 additions & 0 deletions build/stats.module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/**
* @author mrdoob / http://mrdoob.com/
*/

var Stats = function () {

var mode = 0;

var container = document.createElement( 'div' );
container.style.cssText = 'position:fixed;top:0;left:0;cursor:pointer;opacity:0.9;z-index:10000';
container.addEventListener( 'click', function ( event ) {

event.preventDefault();
showPanel( ++ mode % container.children.length );

}, false );

//

function addPanel( panel ) {

container.appendChild( panel.dom );
return panel;

}

function showPanel( id ) {

for ( var i = 0; i < container.children.length; i ++ ) {

container.children[ i ].style.display = i === id ? 'block' : 'none';

}

mode = id;

}

//

var beginTime = ( performance || Date ).now(), prevTime = beginTime, frames = 0;

var fpsPanel = addPanel( new Stats.Panel( 'FPS', '#0ff', '#002' ) );
var msPanel = addPanel( new Stats.Panel( 'MS', '#0f0', '#020' ) );

if ( self.performance && self.performance.memory ) {

var memPanel = addPanel( new Stats.Panel( 'MB', '#f08', '#201' ) );

}

showPanel( 0 );

return {

REVISION: 16,

dom: container,

addPanel: addPanel,
showPanel: showPanel,

begin: function () {

beginTime = ( performance || Date ).now();

},

end: function () {

frames ++;

var time = ( performance || Date ).now();

msPanel.update( time - beginTime, 200 );

if ( time >= prevTime + 1000 ) {

fpsPanel.update( ( frames * 1000 ) / ( time - prevTime ), 100 );

prevTime = time;
frames = 0;

if ( memPanel ) {

var memory = performance.memory;
memPanel.update( memory.usedJSHeapSize / 1048576, memory.jsHeapSizeLimit / 1048576 );

}

}

return time;

},

update: function () {

beginTime = this.end();

},

// Backwards Compatibility

domElement: container,
setMode: showPanel

};

};

Stats.Panel = function ( name, fg, bg ) {

var min = Infinity, max = 0, round = Math.round;
var PR = round( window.devicePixelRatio || 1 );

var WIDTH = 80 * PR, HEIGHT = 48 * PR,
TEXT_X = 3 * PR, TEXT_Y = 2 * PR,
GRAPH_X = 3 * PR, GRAPH_Y = 15 * PR,
GRAPH_WIDTH = 74 * PR, GRAPH_HEIGHT = 30 * PR;

var canvas = document.createElement( 'canvas' );
canvas.width = WIDTH;
canvas.height = HEIGHT;
canvas.style.cssText = 'width:80px;height:48px';

var context = canvas.getContext( '2d' );
context.font = 'bold ' + ( 9 * PR ) + 'px Helvetica,Arial,sans-serif';
context.textBaseline = 'top';

context.fillStyle = bg;
context.fillRect( 0, 0, WIDTH, HEIGHT );

context.fillStyle = fg;
context.fillText( name, TEXT_X, TEXT_Y );
context.fillRect( GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT );

context.fillStyle = bg;
context.globalAlpha = 0.9;
context.fillRect( GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT );

return {

dom: canvas,

update: function ( value, maxValue ) {

min = Math.min( min, value );
max = Math.max( max, value );

context.fillStyle = bg;
context.globalAlpha = 1;
context.fillRect( 0, 0, WIDTH, GRAPH_Y );
context.fillStyle = fg;
context.fillText( round( value ) + ' ' + name + ' (' + round( min ) + '-' + round( max ) + ')', TEXT_X, TEXT_Y );

context.drawImage( canvas, GRAPH_X + PR, GRAPH_Y, GRAPH_WIDTH - PR, GRAPH_HEIGHT, GRAPH_X, GRAPH_Y, GRAPH_WIDTH - PR, GRAPH_HEIGHT );

context.fillRect( GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, GRAPH_HEIGHT );

context.fillStyle = bg;
context.globalAlpha = 0.9;
context.fillRect( GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, round( ( 1 - ( value / maxValue ) ) * GRAPH_HEIGHT ) );

}

};

};

export default Stats;
38 changes: 38 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.17.0",
"description": "JavaScript Performance Monitor",
"main": "build/stats.js",
"module": "build/stats.module.js",
"repository": "mrdoob/stats.js",
"directories": {
"example": "examples"
Expand Down
14 changes: 11 additions & 3 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
export default {
entry: 'src/Stats.js',
dest: 'build/stats.js',
moduleName: 'Stats',
format: 'umd'
targets: [
{
format: 'umd',
moduleName: 'Stats',
dest: 'build/stats.js'
},
{
format: 'es',
dest: 'build/stats.module.js'
}
]
};

0 comments on commit 9d23c79

Please sign in to comment.