Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 28 additions & 52 deletions src/Stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,93 @@
* @author mrdoob / http://mrdoob.com/
*/

var Stats = function () {
let conf = {
containerStyle : 'position:fixed;top:0;left:0;cursor:pointer;opacity:0.9;z-index:10000',
canvas : {
cssText : 'width:180px;height:148px',
textBaseline : 'top',
},
panels: {
fpsPanel : {foreground : '#0ff', background: '#002'},
msPanel : {foreground : '#1f0', background: '#020'},
memPanel : {foreground : '#f08', background: '#201'},
}
};
const CANVAS_CONTEXT = '2d';

var Stats = function (customConf) {
if ( customConf !== undefined ) {
Object.assign(conf, customConf);
}

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.style.cssText = conf.continerStyle;
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' ) );
var fpsPanel = addPanel( new Stats.Panel( 'FPS', conf.panels.fpsPanel.foreground, conf.panels.fpsPanel.background ) );
var msPanel = addPanel( new Stats.Panel( 'MS', conf.panels.msPanel.foreground, conf.panels.msPanel.background ) );

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

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

var memPanel = addPanel( new Stats.Panel( 'MB', conf.panels.memPanel.foreground, conf.panels.memPanel.background ) );
}

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 ) {
Expand All @@ -122,11 +104,11 @@ Stats.Panel = function ( name, fg, bg ) {
var canvas = document.createElement( 'canvas' );
canvas.width = WIDTH;
canvas.height = HEIGHT;
canvas.style.cssText = 'width:80px;height:48px';
canvas.style.cssText = conf.canvas.cssText;
canvas.style = 'cursor: move;';

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

context.fillStyle = bg;
context.fillRect( 0, 0, WIDTH, HEIGHT );
Expand All @@ -137,14 +119,12 @@ Stats.Panel = function ( name, fg, bg ) {

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 );

Expand All @@ -156,16 +136,12 @@ Stats.Panel = function ( name, fg, bg ) {

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.fillRect( GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, GRAPH_HEIGHT+PR );
context.fillStyle = bg;
context.globalAlpha = 0.9;
context.fillRect( GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, round( ( 1 - ( value / maxValue ) ) * GRAPH_HEIGHT ) );

context.fillRect( GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, round( ( 1 - ( value / maxValue ) ) * (GRAPH_HEIGHT + PR) ) );
}

};

};

export { Stats as default };