mirror of
https://git.leinelab.org/Mal/mr-crocs-adventures.git
synced 2025-09-11 02:57:36 +02:00
Performance optimizations and refacturing.
This commit is contained in:
86
js/FrameRateMeasurer.js
Normal file
86
js/FrameRateMeasurer.js
Normal file
@@ -0,0 +1,86 @@
|
||||
import FrameRateMeasuredEvent from "./events/FrameRateMeasuredEvent.js";
|
||||
|
||||
export default class FrameRateMeasurer
|
||||
{
|
||||
constructor(rounds = 100)
|
||||
{
|
||||
this.rounds = rounds;
|
||||
this.round = 0;
|
||||
this.lastTimestamp = undefined;
|
||||
this.frameDurations = [];
|
||||
|
||||
window.requestAnimationFrame(
|
||||
(timestamp) => {
|
||||
this.measureFrameRate(timestamp);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
measureFrameRate(timestamp)
|
||||
{
|
||||
if (this.lastTimestamp !== undefined) {
|
||||
this.round++;
|
||||
this.frameDurations.push(timestamp - this.lastTimestamp);
|
||||
|
||||
this.lastTimestamp = timestamp;
|
||||
|
||||
if (this.round < this.rounds) {
|
||||
window.requestAnimationFrame(
|
||||
(timestamp) => {
|
||||
this.measureFrameRate(timestamp);
|
||||
}
|
||||
);
|
||||
} else {
|
||||
window.dispatchEvent(new FrameRateMeasuredEvent(this.getAverageFrameRate()));
|
||||
}
|
||||
} else {
|
||||
this.round = 0;
|
||||
this.lastTimestamp = timestamp;
|
||||
window.requestAnimationFrame(
|
||||
(timestamp) => {
|
||||
this.measureFrameRate(timestamp);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
this.lastTimestamp = timestamp
|
||||
}
|
||||
|
||||
getAverageFrameRate()
|
||||
{
|
||||
let frameDurationSum = 0;
|
||||
|
||||
this.frameDurations.forEach(
|
||||
(duration) => {
|
||||
frameDurationSum += duration;
|
||||
}
|
||||
);
|
||||
|
||||
let durationAverage = frameDurationSum / this.frameDurations.length;
|
||||
|
||||
return 1000 / durationAverage;
|
||||
}
|
||||
|
||||
getFrameRate()
|
||||
{
|
||||
const DEFAULT_FRAME_RATES = [30, 60, 120];
|
||||
|
||||
let averageFrameRate = this.getAverageFrameRate();
|
||||
let closestDistance = {frameRate: 0, distance: 99999};
|
||||
|
||||
DEFAULT_FRAME_RATES.forEach(
|
||||
(frameRate) => {
|
||||
let distance = Math.abs(frameRate - averageFrameRate);
|
||||
|
||||
if (closestDistance.distance > distance) {
|
||||
closestDistance.frameRate = frameRate;
|
||||
closestDistance.distance = distance;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
console.log(this.frameDurations);
|
||||
|
||||
return closestDistance.frameRate;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user