Skip to content

Commit

Permalink
- Make sure we actually poll canvas fps.
Browse files Browse the repository at this point in the history
  • Loading branch information
hardiesoft committed May 29, 2022
1 parent 3da1ad3 commit 0cd71be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cptv-player-vue",
"version": "1.3.0",
"version": "1.3.1",
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build --target lib --name CptvPlayerVue ./src/CptvPlayer.vue",
Expand Down
52 changes: 29 additions & 23 deletions src/CptvPlayer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ export default class CptvPlayerComponent extends Vue {
trackExportOptions: TrackExportOption[] = [];
scale = 1;
raqFps = 60;
polledFps = false;
set frameNum(num: number) {
this.internalFrameNum = num;
Expand Down Expand Up @@ -684,32 +685,37 @@ export default class CptvPlayerComponent extends Vue {
const frameTimes: number[] = [];
const pollFrameTimes = () => {
frameTimes.push(performance.now());
if (frameTimes.length < 10) {
requestAnimationFrame(pollFrameTimes);
} else {
const diffs = [];
for (let i = 1; i < frameTimes.length; i++) {
diffs.push(frameTimes[i] - frameTimes[i - 1]);
}
let total = 0;
for (const val of diffs) {
total += val;
}
// Get the average frame time
const multiplier = Math.round(1000 / (total / diffs.length) / 30);
if (multiplier === 1) {
// 30fps
this.raqFps = 30;
} else if (multiplier === 2 || multiplier === 3) {
// 60fps
this.raqFps = 60;
} else if (multiplier >= 4) {
// 120fps
this.raqFps = 120;
if (!this.polledFps) {
frameTimes.push(performance.now());
if (frameTimes.length < 10) {
requestAnimationFrame(pollFrameTimes);
} else {
const diffs = [];
for (let i = 1; i < frameTimes.length; i++) {
diffs.push(frameTimes[i] - frameTimes[i - 1]);
}
let total = 0;
for (const val of diffs) {
total += val;
}
// Get the average frame time
const multiplier = Math.round(1000 / (total / diffs.length) / 30);
if (multiplier === 1) {
// 30fps
this.raqFps = 30;
} else if (multiplier === 2 || multiplier === 3) {
// 60fps
this.raqFps = 60;
} else if (multiplier >= 4) {
// 120fps
this.raqFps = 120;
}
this.polledFps = true;
}
}
};
pollFrameTimes();
window.addEventListener("load", pollFrameTimes);
// This makes button active styles work in safari iOS.
Expand Down

0 comments on commit 0cd71be

Please sign in to comment.