Skip to content

Commit

Permalink
Added animation frame callback
Browse files Browse the repository at this point in the history
  • Loading branch information
picosonic committed Aug 26, 2021
1 parent 40154a2 commit 64257dd
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

// Game state
var gs={
// Animation frame of reference
step:(1/60), // target step time @ 60 fps
acc:0, // accumulated time since last frame
lasttime:0, // time of last frame

// Canvas object
canvas:null,
ctx:null,
Expand All @@ -19,6 +24,31 @@ var gs={
timeline:new timelineobj()
};

// Called once per frame
function rafcallback(timestamp)
{
// First time round, just save epoch
if (gs.lasttime>0)
{
// Determine accumulated time since last call
gs.acc+=((timestamp-gs.lasttime) / 1000);

// If it's more than 15 seconds since last call, reset
if ((gs.acc>gs.step) && ((gs.acc/gs.step)>(60*15)))
gs.acc=gs.step*2;

// Process the "steps" since last call
while (gs.acc>gs.step)
gs.acc-=gs.step;
}

// Remember when we were last called
gs.lasttime=timestamp;

// Request that we are called on the next frame
window.requestAnimationFrame(rafcallback);
}

// Startup called once when page is loaded
function startup()
{
Expand All @@ -35,6 +65,8 @@ function startup()

write(gs.ctx, 100, 100, "TEST", 10, "#FF00FF");

window.requestAnimationFrame(rafcallback);

console.log(models);
console.log(gs);
}
Expand Down

0 comments on commit 64257dd

Please sign in to comment.