Skip to content

Commit ea5f9ef

Browse files
committed
add frame counter
1 parent f7bcc4d commit ea5f9ef

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

frame.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
var FrameCounter=function(){
3+
this.fps=-1;
4+
this.lastFrame=-1;
5+
this.frameGaps=[]
6+
this.frameCount=0;
7+
}
8+
FrameCounter.prototype.update=function(){
9+
var now=("performance" in window) ?
10+
window.performance.now() : (new Date().getTime()*1000);
11+
if(this.lastFrame>=0){
12+
var gap=now-this.lastFrame;
13+
if(this.frameGaps.length>300)
14+
this.frameGaps.shift();
15+
if(gap>5000){
16+
// treat as a discontinuity, so discard all the
17+
// frame gaps recorded so far
18+
this.frameGaps=[];
19+
}
20+
this.frameGaps.push(gap);
21+
}
22+
this.lastFrame=now;
23+
this.frameCount++;
24+
if(this.frameGaps.length>0 && this.frameCount>=30){
25+
this.frameCount=0;
26+
var total=0;
27+
for(var i=0;i<this.frameGaps.length;i++){
28+
total+=this.frameGaps[i];
29+
}
30+
total/=1.0*this.frameGaps.length;
31+
this.fps=(total<=0) ? 60 : 1000.0/total;
32+
}
33+
}
34+
FrameCounter.prototype.getFPS=function(){
35+
return this.fps;
36+
}
37+
function FrameCounterDiv(scene){
38+
var canvas=scene.getContext().canvas
39+
var div=document.createElement("div")
40+
div.style.backgroundColor="white"
41+
div.style.position="absolute"
42+
div.style.left=canvas.offsetLeft+"px"
43+
div.style.top=canvas.offsetTop+"px"
44+
document.body.appendChild(div)
45+
this.div=div;
46+
this.count=0;
47+
this.fc=new FrameCounter();
48+
}
49+
FrameCounterDiv.prototype.update=function(){
50+
this.fc.update();
51+
this.count+=1;
52+
if(this.count>=20){
53+
var fps=this.fc.getFPS();
54+
fps=Math.round(fps*100);
55+
fps/=100;
56+
if(fps>=0){
57+
this.div.innerHTML=fps+" fps"
58+
this.count=0;
59+
}
60+
}
61+
}

glutil.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ function FrameBuffer(context, width, height){
11271127
// give the framebuffer its own texture unit, since the
11281128
// shader program may bind samplers to other texture
11291129
// units, such as texture unit 0
1130-
this.textureUnit=1;
1130+
this.textureUnit=1;
11311131
this.buffer=context.createFramebuffer();
11321132
// create color texture
11331133
this.colorTexture = context.createTexture();

glutil_min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

squares.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<meta charset=utf-8>
22
<head>
33
<script type="text/javascript" src="glutil_min.js"></script>
4+
<script type="text/javascript" src="frame.js"></script>
45
</head>
56
<h1>Shader Filters</h1>
67
<a href="javascript:link1()">Grayscale</a>,
@@ -341,6 +342,7 @@ <h1>Shader Filters</h1>
341342
// to Scene3D.
342343
var scene=new Scene3D((document.getElementById("canvas")));
343344
scene.setClearColor("white")
345+
var fc=new FrameCounterDiv(scene)
344346
var blinds=blindsGroup(scene)
345347
var platonic=platonicGroup(scene)
346348
linknone();
@@ -370,6 +372,7 @@ <h1>Shader Filters</h1>
370372
rotation[0]+=0.5;
371373
rotation[1]+=1;
372374
scene.render();
375+
fc.update();
373376
});
374377
//-->
375378
</script>

0 commit comments

Comments
 (0)