add stats display
This commit is contained in:
parent
75c52718c0
commit
6ebddae435
|
@ -9,19 +9,37 @@
|
|||
<style>
|
||||
body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-height: 100vh;
|
||||
margin: 0;
|
||||
background-color: #1e1e2e;
|
||||
color: #cdd6f4;
|
||||
font-family: monospace;
|
||||
font-size: 20px;
|
||||
gap: 20px;
|
||||
}
|
||||
#cv {
|
||||
border: solid 2px black;
|
||||
border: solid 2px #11111b;
|
||||
border-radius: 20px;
|
||||
}
|
||||
#stats {
|
||||
width: 100%;
|
||||
max-width: 800px;
|
||||
box-sizing: border-box;
|
||||
padding: 20px;
|
||||
border: solid 2px #11111b;
|
||||
border-radius: 20px;
|
||||
background-color: #181825;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="cv" width="800" height="600"></canvas>
|
||||
<div id="stats">
|
||||
<div>FPS: <span id="fps"></span></div>
|
||||
<div>Frame time: <span id="ms"></span></div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -6,6 +6,8 @@ let program;
|
|||
|
||||
let vertexBuffer;
|
||||
|
||||
let lastFrame = Date.now();
|
||||
|
||||
async function init() {
|
||||
cv = document.getElementById("cv");
|
||||
gl = cv.getContext("webgl");
|
||||
|
@ -50,7 +52,7 @@ async function init() {
|
|||
5 * Float32Array.BYTES_PER_ELEMENT, // size of individual vertex / distance between values to read
|
||||
0 // offset where to start reading
|
||||
);
|
||||
|
||||
|
||||
// enable attribute
|
||||
gl.enableVertexAttribArray(positionAttribLocation);
|
||||
|
||||
|
@ -75,7 +77,34 @@ async function init() {
|
|||
requestAnimationFrame(draw);
|
||||
}
|
||||
|
||||
let frameCount = 0;
|
||||
let lastStatUpdate = Date.now();
|
||||
|
||||
function updateStats(deltaTime) {
|
||||
frameCount++;
|
||||
|
||||
let now = Date.now();
|
||||
let timeSinceLastUpdate = (now - lastStatUpdate) / 1000;
|
||||
|
||||
if (timeSinceLastUpdate < 0.5) return;
|
||||
|
||||
lastStatUpdate = now;
|
||||
|
||||
let fpsDisplay = document.getElementById("fps");
|
||||
fpsDisplay.innerText = Math.round(frameCount / timeSinceLastUpdate)
|
||||
|
||||
let msDisplay = document.getElementById("ms");
|
||||
msDisplay.innerText = deltaTime * 1000 + "ms";
|
||||
|
||||
frameCount = 0;
|
||||
}
|
||||
|
||||
async function draw() {
|
||||
let now = Date.now();
|
||||
let deltaTime = (now - lastFrame) / 1000;
|
||||
|
||||
updateStats(deltaTime);
|
||||
|
||||
gl.clear(gl.COLOR_BUFFER_BIT);
|
||||
|
||||
gl.useProgram(program);
|
||||
|
@ -84,6 +113,7 @@ async function draw() {
|
|||
|
||||
gl.drawArrays(gl.TRIANGLES, 0, 3);
|
||||
|
||||
lastFrame = now;
|
||||
requestAnimationFrame(draw);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue