T
T
Triborg-3332020-01-18 20:21:50
JavaScript
Triborg-333, 2020-01-18 20:21:50

How to release frame counter - FPS in CANVAS game?

How to release frame counter - FPS in CANVAS game?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Loli E1ON, 2020-01-18
@Triborg-333

With requestAnimationFrame:

const times = [];
let fps;

function refreshLoop() {
  window.requestAnimationFrame(() => {
    const now = performance.now();
    while (times.length > 0 && times[0] <= now - 1000) {
      times.shift();
    }
    times.push(now);
    fps = times.length;
    refreshLoop();
  });
}

refreshLoop();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question