NodeJS中requestAnimationFrame()的服务器端实现 [英] Server Side Implementation of requestAnimationFrame() in NodeJS
问题描述
我对普遍使用的 requestAnimationFrame()
函数有一些疑问.最近,我在多人游戏中遇到了一些实现,这些实现是在客户端而不是服务器端使用的.
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.
- 这样做有什么好处吗?
- 您能参考一下NodeJS中任何最佳实践"服务器端实现吗?
更新
我在动画和游戏循环之间有些困惑-我想要的是NodeJS中的实现=>例如 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);
};
}());
推荐答案
这样做有什么好处吗?
Is there any benefit in doing so?
在客户端中-有.当 setTimeout
及其朋友在计时器队列中运行时- requestAnimationFrame
会同步到浏览器的页面呈现(绘制页面),因此当您使用它时,不会出现抖动告诉它要绘制的内容和浏览器的图片是同步的.
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.
游戏通常具有两个循环-渲染循环(绘制内容)和游戏循环(事物的逻辑位置).第一个在 requestAnimationFrame
中,另一个在 setTimeout
中-两者都必须非常快地运行.
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.
此处是Paul Irish在 requestAnimationFrame上的引用./p>
Here is a reference on requestAnimationFrame by Paul Irish.
您能参考一下NodeJS中任何最佳实践"服务器端实现吗?
Can you reference me to any "best practices" server side implementation in NodeJS?
由于服务器未渲染任何图像-在服务器中填充 requestAnimationFrame
毫无意义.您将在Node/io.js中使用 setImmediate
来在客户端中使用 requestAnimationFrame
.
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.
简单地说-添加了requestAnimationFrame以解决服务器中不存在的问题(图形数据的无抖动渲染).
Simply put - requestAnimationFrame was added to solve a problem (jitterless rendering of graphic data) that does not exist in servers.
这篇关于NodeJS中requestAnimationFrame()的服务器端实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!