小编Dan*_*neh的帖子

向requestAnimationFrame返回的函数添加其他参数

我正在寻找创建一个函数,使用requestAnimationFrame和delta time在HTML5画布上滚动图像元素x像素.我无法弄清楚的是当requestAnimationFrame allready用一个参数(一个DOMHighResTimeStamp)回调我的函数时,如何为我的函数添加更多参数.我很确定以下代码不起作用:

function scroll(timestamp, distanceToScroll, secondsToScroll) {
  //delta = how many milliseconds have passed between this and last draw
  if (!lastDraw) {var lastDraw = timestamp;};
  delta = (0.5 + (timestamp - lastDraw)) << 0; //bitwise hack for rounding integers
  lastDraw = timestamp;

  //speed (pixels per millisecond) = amount of pixels to move / length of animation (in milliseconds)
  speed = distanceToScroll / secondsToScroll;

  //new position = current position + (speed * delta)
  position += (speed * delta);

  context.drawImage(myImage,0,position,50,50/*of 200*/,0,0,100,100); …
Run Code Online (Sandbox Code Playgroud)

javascript html5-canvas requestanimationframe

6
推荐指数
1
解决办法
5038
查看次数

每当节点断开连接时,通过节点代理服务器运行的基于Socket.io的应用程序将断开所有套接字的连接

我做了使用基本的聊天应用程序node.jsexpresssocket.io。与socket.io 的教程聊天应用程序没有太大区别,它只是在连接的客户端之间发出事件。当我在服务器上的端口3001上运行它时,它工作正常。

然后,我制作了一个代理服务器应用程序,使用node-http-proxy该服务器侦听端口80并将基于请求的url的流量重定向到我在不同端口上运行的各种独立节点应用程序。非常简单。但是有些东西坏了。每当有人断开连接时,每个单个插座都会断开并重新连接。这对具有基于连接事件的聊天应用程序不利。客户端控制台均显示:

WebSocket与“ ws:// [某些套接字信息] ”的连接失败:在收到握手响应之前,连接已关闭

我认为这是代码的重要部分。

proxy-server.js

var http = require('http');
var httpProxy = require('http-proxy');

//create proxy template object with websockets enabled
var proxy = httpProxy.createProxyServer({ws: true});

//check the header on request and return the appropriate port to proxy to
function sites (req) {
    //webapps get their own dedicated port
    if (req == 'mychatwebsite.com') {return 'http://localhost:3001';}
    else if (req == 'someothersite.com') {return 'http://localhost:3002';}
    //static sites are …
Run Code Online (Sandbox Code Playgroud)

sockets node.js socket.io node-http-proxy

5
推荐指数
1
解决办法
3238
查看次数

默认情况下,在多个画布上禁用imageSmoothingEnabled

我正在创建一个使用分层画布和精灵图像的基于浏览器的游戏,出于视觉和性能原因,我想默认禁用imageSmoothingEnabled.我的理解是imageSmoothingEnabled并非在所有浏览器中都可用,但有供应商前缀版本.我试图在我的所有画布(尽可能多的浏览器中)中找到一种优雅的方法来禁用此属性.到目前为止,这是我的方法:

context1.imageSmoothingEnabled = false;
context1.mozImageSmoothingEnabled = false;
context1.oImageSmoothingEnabled = false;
context1.webkitImageSmoothingEnabled = false;

context2.imageSmoothingEnabled = false;
context2.mozImageSmoothingEnabled = false;
context2.oImageSmoothingEnabled = false;
context2.webkitImageSmoothingEnabled = false;

context3.imageSmoothingEnabled = false;
context3.mozImageSmoothingEnabled = false;
context3.oImageSmoothingEnabled = false;
context3.webkitImageSmoothingEnabled = false;
//etc...
Run Code Online (Sandbox Code Playgroud)

有更优雅的方法吗?在实际创建每个画布上下文之前,是否可以将上下文的API更改为默认值为false?

javascript canvas html5-canvas

1
推荐指数
1
解决办法
2722
查看次数