Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server Side Implementation of requestAnimationFrame() in NodeJS

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.

  1. Is there any benefit in doing so?
  2. Can you reference me to any "best practices" server side implementation in NodeJS?

Update

I got a bit confused between the animation and game loop - what I was looking for is an implementation in NodeJS => e.g 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);
        };
}());
like image 796
HansMusterWhatElse Avatar asked May 25 '15 17:05

HansMusterWhatElse


People also ask

What is requestAnimationFrame ()?

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.

Where is requestAnimationFrame used?

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.

What is node js server side JavaScript process?

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?

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.


2 Answers

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.

like image 129
Benjamin Gruenbaum Avatar answered Oct 25 '22 11:10

Benjamin Gruenbaum


function requestAnimationFrame(f){
  setImmediate(()=>f(Date.now()))
}
like image 23
cancerbero Avatar answered Oct 25 '22 11:10

cancerbero