RequestAnimationFrame () server implementation in NodeJS

I have a few questions regarding the wildly used requestAnimationFrame() functions. Recently, I came across an implementation in multiplayer games that used it on the client, and not on the server side.

  1. Is there any benefit to this?
  2. Can you refer to any "best practices" of server implementation in NodeJS?

Refresh

I got a little confused between animation and the game loop - I was looking for an implementation in NodeJS =>, for example, setInterval .

Example - client side implementation

 (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); }; }()); 
+9
source share
3 answers

Is there any benefit to this?

In the client - is. While setTimeout and his friends are starting in the timer queue - requestAnimationFrame synchronizes with the page display in the browser (draws it), so when you use it, there is no jitter, as you tell him what to draw, and the picture in the browser is in sync.

Usually games have two loops - a rendering cycle (what to draw) and a game cycle (the logic of what happens). The first is in requestAnimationFrame , and the other in setTimeout - both should be very fast.

Here is a link to requestAnimationFrame from Paul Irish.

Can you refer to any server-side implementation of "best practice" in NodeJS?

Since the server does not display any image, there is no point in requestAnimationFrame polyphony on the server. You would use setImmediate in Node / io.js so that you would use requestAnimationFrame for the client.

Simply put - requestAnimationFrame was added to solve a problem (rendering graphic data without jitter) that does not exist on the servers.

+8
source
 function requestAnimationFrame(f){ setImmediate(()=>f(Date.now())) } 
0
source
 if(!window.requestAnimationFrame) window.requestAnimationFrame = window.setImmediate 
-1
source

All Articles