I have some questions regarding the wildly used requestAnimationFrame()
functions. Recently I came across some implementation in multiplayer games who used it on the client instead of the server side.
I got a bit confused between the animation and game loop - what I was looking for is an implementation in NodeJS => e.g setInterval
.
(function () {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
window.cancelAnimationFrame =
window[vendors[x] + 'CancelAnimationFrame'] ||
window[vendors[x] + 'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function (callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function () {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function (id) {
clearTimeout(id);
};
}());
requestAnimationFrame() The window. requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser calls a specified function to update an animation before the next repaint. The method takes a callback as an argument to be invoked before the repaint.
The requestAnimationFrame() method tells the browser to run a callback function right before the next repaint happens. It's particularly useful when using JavaScript for animations and repeating UI updates.
Node. js is an open source JavaScript runtime environment that lets developers run JavaScript code on the server. If that's too complex for you to understand then you should think of it this way: Node. js is JavaScript that runs outside the browser — on the server.
Is requestAnimationFrame asynchronous? As now you know that the rAF is a Web API, that means the callback will be called asynchronously. Unlike setInterval , requestAnimationFrame does not accept delay argument, instead, it only calls the callback function when the browser is ready to perform the next paint operation.
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.
function requestAnimationFrame(f){
setImmediate(()=>f(Date.now()))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With