Skip to content

Commit 737c565

Browse files
committed
First
0 parents  commit 737c565

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

README

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2010 Mr.doob
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

src/stats.js

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* stats.js 1.2
3+
* http://code.google.com/p/mrdoob/wiki/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+
* dom_element.appendChild( stats.getDisplayElement() );
12+
*
13+
* setInterval(loop, 1000/60);
14+
*
15+
* function loop()
16+
* {
17+
* stats.update();
18+
* }
19+
*
20+
* version log:
21+
*
22+
* 10.03.01 1.2 Mr.doob + Simplified
23+
* 10.02.21 1.1 Mr.doob + Accurate FPS calculation (thx Spite!)
24+
* 09.08.10 1.0 Mr.doob + Base code
25+
*
26+
*/
27+
28+
function Stats()
29+
{
30+
this.init();
31+
}
32+
33+
Stats.prototype =
34+
{
35+
init: function()
36+
{
37+
this.frames = 0;
38+
this.framesMin = 1000;
39+
this.framesMax = 0;
40+
41+
this.time = new Date().getTime();
42+
this.timePrev = new Date().getTime();
43+
44+
this.container = document.createElement("div");
45+
this.container.style.position = 'absolute';
46+
this.container.style.fontFamily = 'Arial';
47+
this.container.style.fontSize = '10px';
48+
this.container.style.backgroundColor = '#000020';
49+
this.container.style.opacity = '0.9';
50+
this.container.style.width = '80px';
51+
this.container.style.paddingTop = '2px';
52+
53+
this.framesText = document.createElement("div");
54+
this.framesText.style.color = '#00ffff';
55+
this.framesText.style.marginLeft = '3px';
56+
this.framesText.style.marginBottom = '3px';
57+
this.framesText.innerHTML = '<strong>FPS</strong>';
58+
this.container.appendChild(this.framesText);
59+
60+
this.canvas = document.createElement("canvas");
61+
this.canvas.width = 74;
62+
this.canvas.height = 30;
63+
this.canvas.style.display = 'block';
64+
this.canvas.style.marginLeft = '3px';
65+
this.canvas.style.marginBottom = '3px';
66+
this.container.appendChild(this.canvas);
67+
68+
this.context = this.canvas.getContext("2d");
69+
this.context.fillStyle = '#101030';
70+
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height );
71+
72+
this.contextImageData = this.context.getImageData(0, 0, this.canvas.width, this.canvas.height);
73+
},
74+
75+
getDisplayElement: function()
76+
{
77+
return this.container;
78+
},
79+
80+
update: function()
81+
{
82+
this.frames++
83+
84+
this.time = new Date().getTime();
85+
86+
if (this.time >= this.timePrev + 1000)
87+
{
88+
this.fps = Math.round((this.frames * 1000 ) / (this.time - this.timePrev));
89+
90+
this.framesMin = Math.min(this.framesMin, this.fps);
91+
this.framesMax = Math.max(this.framesMax, this.fps);
92+
93+
this.framesText.innerHTML = '<strong>' + this.fps + ' FPS</strong> (' + this.framesMin + '-' + this.framesMax + ')';
94+
95+
this.contextImageData = this.context.getImageData(1, 0, this.canvas.width - 1, 30);
96+
this.context.putImageData(this.contextImageData, 0, 0);
97+
98+
this.context.fillStyle = '#101030';
99+
this.context.fillRect(this.canvas.width - 1, 0, 1, 30);
100+
101+
this.index = ( Math.floor(30 - Math.min(30, (this.fps / 60) * 30)) );
102+
103+
this.context.fillStyle = '#80ffff';
104+
this.context.fillRect(this.canvas.width - 1, this.index, 1, 1);
105+
106+
this.context.fillStyle = '#00ffff';
107+
this.context.fillRect(this.canvas.width - 1, this.index + 1, 1, 30 - this.index);
108+
109+
this.timePrev = this.time;
110+
this.frames = 0;
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)