-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fps-npm.js
108 lines (92 loc) · 2.77 KB
/
fps-npm.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
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = (() => {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
}
const getTime = () => {
return (window.performance && window.performance.now) ? window.performance.now() : +new Date();
};
module.exports = class FPSMeter {
constructor(opts) {
this.ui = opts.ui || false;
this.fps = 0;
this.isRunning = false;
this.defaultStyles = 'z-index:999;position:fixed;bottom:0;left:0;padding:10px;font-weight:600;font-style:normal;font-size:12px;font-family:Consolas,Menlo,Monaco,"Lucida Console","Liberation Mono","DejaVu Sans Mono","Bitstream Vera Sans Mono","Courier New",Courier,monospace,sans-serif';
}
measure() {
const time = getTime();
window.requestAnimationFrame(() => {
const _fps = Math.round((1 / (getTime() - time)) * 1000);
this.fps = _fps;
if (this.ui && this.element) {
let i = 4 - `${_fps}`.length;
let pad = '';
while (i > 0) {
pad += ' ';
i--;
}
this.text.nodeValue = `${_fps}${pad}fps`;
switch (false) {
case !(_fps < 7):
this.element.style.color = '#FFF';
this.element.style.backgroundColor = '#FF4500';
break;
case !(_fps < 25):
this.element.style.color = '#FF4500';
this.element.style.backgroundColor = '#000';
break;
case !(_fps < 40):
this.element.style.color = 'orange';
this.element.style.backgroundColor = '#000';
break;
case !(_fps > 70):
this.element.style.color = '#0f0';
this.element.style.backgroundColor = '#000';
break;
default:
this.element.style.color = '#018801';
this.element.style.backgroundColor = '#000';
break;
}
}
if (this.isRunning) {
this.measure();
}
});
}
start() {
if (!this.isRunning) {
this.isRunning = true;
if (this.ui === true) {
this.text = document.createTextNode('');
this.element = document.createElement('div');
this.element.style = this.defaultStyles;
this.element.appendChild(this.text);
document.body.appendChild(this.element);
}
this.measure();
}
}
pause() {
this.isRunning = false;
}
resume() {
if (!this.isRunning) {
this.isRunning = true;
this.measure();
}
}
toggle() {
this.isRunning ? this.pause() : this.resume();
}
stop() {
if (this.isRunning) {
this.isRunning = false;
if (this.ui === true && this.element) {
this.element.remove();
}
}
}
};