简体   繁体   English

HTML5 Canvas游戏玩家限制

[英]HTML5 Canvas game player limit

I placed 1000 players on canvas but their movement is not smooth. 我在画布上放置了1000个播放器,但它们的移动并不平稳。 Is there a way for canvas to handle that much players? 画布有没有办法处理那么多球员? What options do I have? 我有什么选择?

Position of players is sent 20 times per second using socket.IO. 玩家的位置每秒通过socket.IO发送20次。 When event reach client it draws players on canvas. 当事件到达客户时,它将在画布上吸引玩家。

var images = {};
images.spriteSheet = new Image();
images.spriteSheet.src = "/images/spriteSheet.png";

socket.on('drawing', function(data){

  ctx.clearRect(0,0,1000,500);

  for(var i = 0; i < data.length; i++){

    ctx.drawImage(images['spriteSheet'], data[i].sx, data[i].sy, 100, 100, data[i].x, data[i].y, 70, 70);

  }

});

I placed 1000 players on canvas but their movement is not smooth. 我在画布上放置了1000个播放器,但它们的移动并不平稳。 Is there a way for canvas to handle that much players? 画布有没有办法处理那么多球员?

I assume you mean you're drawing 1,000 sprites to a canvas 20x per second. 我假设您的意思是您正在以每秒20x的速度将1,000个精灵绘制到画布上。 No doubt, that's going to be tough. 毫无疑问,这将是艰难的。

Position of players is sent 20 times in a second using socket.IO. 玩家的位置在一秒钟内使用socket.IO发送20次。

Surely you can send a position and vector instead, and let the client update per-frame locally, predictively, until the next real update. 当然,您可以发送位置和矢量,然后让客户端按预测方式在本地逐帧更新,直到下一次真正的更新为止。 That's how pretty much all other games work, and for a reason. 这就是所有其他游戏的工作量,这是有原因的。 This idea that you can even get data 20 times a second isn't always going to be the case. 您甚至可以每秒获取20次数据的想法并非总是如此。

Without more details it's hard to give a specific answer, but in the general case I'd suggest looking into SVG. 如果没有更多细节,很难给出具体答案,但是在一般情况下,我建议您研究SVG。 Then, you can update individual elements and let the browser worry about compositing and what not, which is likely going to be faster than what you can pull off in the canvas. 然后,您可以更新单个元素,并让浏览器担心合成和不合成什么,这可能比在画布中完成转换要快。 You'll have to experiment though for your specific use case. 不过,您必须针对特定的用例进行试验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM