Server Side Implementation of requestAnimationFrame() in NodeJS
if(!window.requestAnimationFrame)
window.requestAnimationFrame = window.setImmediate
function requestAnimationFrame(f){
setImmediate(()=>f(Date.now()))
}
Is there any benefit in doing so?
In the client - there is. While setTimeout
and its friends run in the timers queue - requestAnimationFrame
is synced to a browser's rendering of the page (drawing it) so when you use it there is no jitter since you telling it what to draw and the browser drawing are in sync.
Typically games have two loops - the render loop (what to draw) and the game loop (logic of where things are). The first one is in a requestAnimationFrame
and the other in a setTimeout
- both must run very fast.
Here is a reference on requestAnimationFrame by Paul Irish.
Can you reference me to any "best practices" server side implementation in NodeJS?
Since the server does not render any image - there is no point in polyfilling requestAnimationFrame
in the server. You'd use setImmediate
in Node/io.js for what you'd use requestAnimationFrame
for in the client.
Simply put - requestAnimationFrame was added to solve a problem (jitterless rendering of graphic data) that does not exist in servers.