Skip to content

Commit

Permalink
frame rate regulation
Browse files Browse the repository at this point in the history
  • Loading branch information
KilledByAPixel authored Feb 22, 2020
1 parent 40bcaa1 commit 14a5e39
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@
let hueShift; // current hue shift for all hsl colors
let road; // the list of road segments
let time; // time left before game over
let lastUpdate = 0; // time of last update
let timeBuffer = 0; // frame rate adjustment

function StartLevel()
{
Expand Down Expand Up @@ -181,6 +183,28 @@

function Update()
{
// time regulation, in case running faster then 60 fps, though it causes judder REMOVE FROM MINFIED
const now = performance.now();
if (lastUpdate)
{
// limit to 60 fps
const timeDelta = 1/60;
const delta = now - lastUpdate;
if (timeBuffer + delta < 0)
{
// running fast
requestAnimationFrame(Update);
return;
}

// update time buffer
timeBuffer += delta;
timeBuffer -= timeDelta * 1e3;
if (timeBuffer > timeDelta * 1e3)
timeBuffer = 0; // if running too slow
}
lastUpdate = now;

// start frame
if (snapshot) {c.width|0} else // DEBUG REMOVE FROM MINFIED
c.width = window.innerWidth,c.height = window.innerHeight; // clear the screen and set size
Expand Down

0 comments on commit 14a5e39

Please sign in to comment.