-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfpscounterscript.js
66 lines (61 loc) · 2.69 KB
/
fpscounterscript.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
var fpscountersetactive = null;
var fpscountergetactive = null;
var fpsconttopleftelemname;
document.addEventListener("DOMContentLoaded", ()=>{
var lastfpstime = 0;
var lasttimedifflog = [];
var fpscounter = document.getElementById("fpscounter");
var mindiff = 99999;
var maxfps = 60;
const fpscont = document.getElementById("fpscountercontainer");
const fpschartelem = document.getElementById("fpschart");
//true if using `file` protocol, or reduced motion off and not mobile and more than 8 logical processors
var fpscounteractive = !(window.matchMedia("(prefers-reduced-motion)")?.matches??true)//reduced motion off
&& (!navigator.userAgent.match(/Mobile/i) && navigator.hardwareConcurrency>=8)//and not mobile and more than 8 logical processors
|| window.location.protocol.match(/^file/i);//if using `file` protocol
function nextFrame(time){
var timediff = time - lastfpstime;
lastfpstime = time;
/*if(timediff < mindiff){
mindiff = timediff;
maxfps = 1000/mindiff;//Math.min(...lasttimedifflog);
}*/
maxfps = 1000/Math.min(...lasttimedifflog);
lasttimedifflog.push(timediff);
var maxframes = Number(fpschartelem.dataset["maxlasttimediffloglen"]);
while(lasttimedifflog.length > maxframes){
lasttimedifflog.shift()
}
const fpschartwidth = Number(fpscont.getAttribute("width"));
const fpschartheight = Number(fpscont.getAttribute("height"));
var fpschartwidthmulti = fpschartwidth/maxframes;
var fpschartheightmulti = fpschartheight/maxfps;
var fpsavg = (1000/(lasttimedifflog.reduce((a,b)=>a+b)/lasttimedifflog.length)).toFixed(5);
var chartavg = fpsavg*fpschartheightmulti;
var xoffset = maxframes-lasttimedifflog.length;
fpscounter.textContent = fpsavg + " fps avg (last "+lasttimedifflog.length+" frames)\n"
+ (1000/timediff).toFixed(5) + " fps";
fpschartelem.setAttribute("d", "M"+Object.entries(lasttimedifflog).map(a=>((Number(a[0])+xoffset)*fpschartwidthmulti+","+(1000/a[1]*fpschartheightmulti).toFixed(3))).join(" "));
document.getElementById("fpschartavg").setAttribute("d", "M 0,"+chartavg+"h"+fpschartwidth);
if(fpsconttopleftelemname!=null){
let fpsconttopleftelem = document.getElementById(fpsconttopleftelemname);
if(fpsconttopleftelem){
fpscont.style.top = window.getComputedStyle(fpsconttopleftelem).height;
fpscont.style.left = window.getComputedStyle(fpsconttopleftelem).left;
}
}
if(fpscounteractive){
window.requestAnimationFrame(nextFrame);
}
}
fpscountersetactive = function(active){
if(!fpscounteractive && active){
window.requestAnimationFrame(nextFrame);
}
fpscounteractive = active;
}
fpscountergetactive = function(){
return fpscounteractive;
}
window.requestAnimationFrame(nextFrame);
});