-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathframecount.js
115 lines (92 loc) · 2.98 KB
/
framecount.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
FILE ARCHIVED ON 9:09:16 May 30, 2016 AND RETRIEVED FROM THE
INTERNET ARCHIVE ON 15:51:22 Aug 21, 2016.
JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.
ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
SECTION 108(a)(3)).
*/
// Basic Frame Counting Class
// For displaying current and average frames per second
// Author: Joseph Huckaby
// Copyright (c) 2010 Joseph Huckaby
// Usage: FrameCount.count() // for every frame
var FrameCount = {
current: 0,
average: 0,
frameCount: 0,
lastSecond: 0,
startTime: 0,
totalFrames: 0,
ie: !!navigator.userAgent.match(/MSIE/),
visible: true,
init: function() {
// create floating widget
if (this.visible) {
var html = '<div id="d_framecount" style="float:right; width:200px; border:1px solid #ccc; background:#eee; margin:10px; padding:10px; font-family:Helvetica,sans-serif; font-size:11px; color:#444;">Waiting for frames...</div>';
if (this.ie) {
setTimeout( function() {
document.body.insertAdjacentHTML('beforeEnd',
'<div style="position:absolute; z-index:9999; left:0px; top:0px; width:100%;">' + html + '</div>'
);
}, 1000 );
}
else {
var div = document.createElement('DIV');
div.style.position = 'fixed';
div.style.zIndex = '9999';
div.style.left = '0px';
div.style.top = '0px';
div.style.width = '100%';
div.innerHTML = html;
document.getElementsByTagName('body')[0].appendChild(div);
}
}
},
update: function() {
// update display
var div = document.getElementById('d_framecount');
if (div) {
var html = '';
html += '<table>';
html += '<tr><td align="right"><b>Current FPS:</b></td><td align="left">' + this.current + '</td></tr>';
html += '<tr><td align="right"><b>Average FPS:</b></td><td align="left">' + this.average + '</td></tr>';
html += '<tr><td align="right"><b>Total Frames:</b></td><td align="left">' + this.totalFrames + '</td></tr>';
html += '</table>';
html += '<br/><a href="#" onClick="FrameCount.reset()">Reset</a>';
div.innerHTML = html;
}
},
reset: function() {
this.current = 0;
this.average = 0;
this.frameCount = 0;
this.lastSecond = 0;
this.startTime = 0;
this.totalFrames = 0;
this.update();
},
_now_epoch: function() {
// return current date/time in hi-res epoch seconds
var _mydate = new Date();
return _mydate.getTime() / 1000;
},
count: function() {
// advance one frame
var _now = this._now_epoch();
var _int_now = parseInt(_now, 10);
var result = false;
if (_int_now != this.lastSecond) {
result = true;
this.totalFrames += this.frameCount;
if (!this.startTime) this.startTime = _int_now;
if (_int_now > this.startTime) this.average = this.totalFrames / (_int_now - this.startTime);
else this.average = this.frameCount;
this.current = this.frameCount;
this.frameCount = 0;
this.lastSecond = _int_now;
if (this.visible) this.update();
}
this.frameCount++;
return result;
}
};