1 | const MILLISECONDS_IN_SECOND = 1000;
|
2 |
|
3 | const FILTER_STRENGTH = Symbol();
|
4 | const SAMPLE_RATE = Symbol();
|
5 | const HISTORY_DURATION = Symbol();
|
6 | const FPS = Symbol();
|
7 | const HISTORY = Symbol();
|
8 | const SAMPLE_INTERVAL = Symbol();
|
9 | const ON_SAMPLE = Symbol();
|
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 | export default class FrameRate {
|
36 | constructor(settings) {
|
37 | const self = this;
|
38 | let frameTime = 0;
|
39 | let lastLoop = performance.now();
|
40 |
|
41 | self[HISTORY] = [];
|
42 |
|
43 | self.onSample(settings.onSample);
|
44 | self.filterStrength(settings.filterStrength || 5);
|
45 | self.historyDuration(settings.historyDuration || 30);
|
46 | self.sampleRate(settings.sampleRate || 10);
|
47 |
|
48 | const refreshLoop = () => {
|
49 | window.requestAnimationFrame((now) => {
|
50 | frameTime += (now - lastLoop - frameTime) / self[FILTER_STRENGTH];
|
51 | lastLoop = now;
|
52 | self[FPS] = Math.round(MILLISECONDS_IN_SECOND / frameTime);
|
53 | refreshLoop();
|
54 | });
|
55 | };
|
56 |
|
57 | refreshLoop();
|
58 | }
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | get fps() {
|
70 | return this[FPS];
|
71 | }
|
72 |
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 | get history() {
|
83 | return this[HISTORY];
|
84 | }
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | filterStrength(value) {
|
101 | const self = this;
|
102 |
|
103 | if (value !== undefined) {
|
104 | self[FILTER_STRENGTH] = value;
|
105 |
|
106 | return self;
|
107 | }
|
108 |
|
109 | return self[FILTER_STRENGTH];
|
110 | }
|
111 |
|
112 | |
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | sampleRate(value) {
|
125 | const self = this;
|
126 | let lastSample;
|
127 | let now;
|
128 |
|
129 | if (value !== undefined) {
|
130 | const DURATION = MILLISECONDS_IN_SECOND / value;
|
131 | self[SAMPLE_RATE] = value;
|
132 | self.historyDuration(self.historyDuration());
|
133 | lastSample = performance.now();
|
134 |
|
135 | clearInterval(self[SAMPLE_INTERVAL]);
|
136 |
|
137 | if (self[SAMPLE_RATE] && self.historyDuration()) {
|
138 | self[SAMPLE_INTERVAL] = setInterval(() => {
|
139 | now = performance.now();
|
140 |
|
141 | while (lastSample + DURATION < now) {
|
142 | self[HISTORY].push(self[FPS]);
|
143 | self[HISTORY].shift();
|
144 | lastSample += DURATION;
|
145 | }
|
146 |
|
147 | if (self[ON_SAMPLE]) {
|
148 | self[ON_SAMPLE](self[HISTORY]);
|
149 | }
|
150 | }, DURATION);
|
151 | }
|
152 |
|
153 | return self;
|
154 | }
|
155 |
|
156 | return self[SAMPLE_RATE];
|
157 | }
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | onSample(callback) {
|
171 | const self = this;
|
172 |
|
173 | if (callback !== undefined) {
|
174 | self[ON_SAMPLE] = callback;
|
175 |
|
176 | return self;
|
177 | }
|
178 |
|
179 | return self[ON_SAMPLE];
|
180 | }
|
181 |
|
182 | |
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 | historyDuration(value) {
|
195 | const self = this;
|
196 |
|
197 | if (value !== undefined) {
|
198 | self[HISTORY_DURATION] = value;
|
199 | const totalHistory = self[SAMPLE_RATE] * self[HISTORY_DURATION];
|
200 |
|
201 | while (self[HISTORY].length < totalHistory) {
|
202 | self[HISTORY].unshift(0);
|
203 | }
|
204 | while (self[HISTORY].length > totalHistory) {
|
205 | self[HISTORY].shift();
|
206 | }
|
207 |
|
208 | return self;
|
209 | }
|
210 |
|
211 | return self[HISTORY_DURATION];
|
212 | }
|
213 | }
|