Skip to content

Instantly share code, notes, and snippets.

@jeromeetienne
Last active August 29, 2015 14:22
Show Gist options
  • Save jeromeetienne/3ed0ecb1360ca5e3bd5d to your computer and use it in GitHub Desktop.
Save jeromeetienne/3ed0ecb1360ca5e3bd5d to your computer and use it in GitHub Desktop.
Rendering Loop ala vendor.js
// array of functions for the rendering loop
var onRenderFcts = [];
// Here you add your functions to the rendering loop.
// They will be executed in-order once per requestAnimationFrame()
onRenderFcts.push(function(now, delta){
// now is the absolute time in millisecond
// delta is the delay between now and the last iteration
// put your code here.
})
// -----------------------------------------------
// ... you may put more functions here
// -----------------------------------------------
// run the rendering loop
var previousTime = performance.now()
requestAnimationFrame(function animate(now){
requestAnimationFrame( animate );
onRenderFcts.forEach(function(onRenderFct){
onRenderFct(now, now - previousTime)
})
previousTime = now
})
@angryobject
Copy link

Should it be previousTime = now on the last line?

@jeromeetienne
Copy link
Author

@angryobject good catch! corrected

@tdurand
Copy link

tdurand commented Jun 11, 2015

nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment