简体   繁体   English

大型HTML5 Canvas滚动性能提示

[英]large HTML5 Canvas scrolling performance tips

I've recently started learning to work with HTML5 Canvas and I've got a bunch of questions really. 我最近开始学习使用HTML5 Canvas的工作,确实有很多问题。 All related to performance. 所有与性能有关。 I understand that everything with this is quite subjective to what I'm doing. 我了解,与此相关的一切都取决于我在做什么。 But I'm just hoping to gain some clarity. 但我只是希望获得一些澄清。

In the example of creating game with an aerial view of the player. 在以玩家的鸟瞰图创建游戏的示例中。 all 2d with a map that scrolls around on both X and Y axis. 所有2d都带有在X和Y轴上都滚动的地图。

Question 1: In my understanding, As long as there are no moving objects in my map canvas. 问题1:据我所知,只要我的地图画布中没有移动物体。 it's more efficient to do something like create a large canvas element that I draw once and then use JS to scroll the page / move the canvas. 这样做的效率更高,例如创建一个我绘制一次的大画布元素,然后使用JS滚动页面/移动画布。 rather than create a canvas that is the size of my screen, translate the canvas and redraw the map with each movement. 而不是创建与我的屏幕大小相同的画布,而是翻译画布并随着每次移动重新绘制地图。

Question 2: If I use the above method of having a large canvas that scrolls about to move my map. 问题2:如果我使用上面的方法,有一个大的画布可以滚动来移动我的地图。 will there be much performance difference between a simple drawing on the canvas and a complex drawing? 画布上的简单绘图与复杂的绘图之间在性能上会有很大的区别吗? Example: simple canvas with a couple rows of single colored blocks compared to complex canvas with thousands of lines, circles, gradients, patterns and detail. 示例:简单的画布具有几行单色块,而复杂的画布具有数千条线,圆,渐变,图案和细节。 If the two canvases are the same width and height there shouldn't be a huge performance difference for just having the browser scroll them right? 如果两个画布的宽度和高度相同,那么让浏览器将它们正确滚动就不会有很大的性能差异吗? (no redrawing). (不重绘)。

Question 3: Is there a preferred method for dealing with a map that is bigger than a canvas object can be? 问题3:是否有首选的方法来处理比画布对象大的地图? I know various browsers will limit the physical size of the canvas. 我知道各种浏览器都会限制画布的物理尺寸。 Is it better to design the map in several big canvas blocks. 在几个大的画布块中设计地图是否更好? Load an adjacent block into a brand new canvas element when the player gets near the edge. 当玩家靠近边缘时,将相邻的方块加载到全新的画布元素中。 or will this cause issues. 否则会引起问题。 if I'm working with canvas elements that are say 10,000px by 10,000px and I start placing several of these side by side am i just asking for trouble? 如果我正在使用10,000px x 10,000px的画布元素,并且开始并排放置其中的几个元素,我是否只是在问麻烦? am I better off designing it to run in a single canvas that just redraws the map as the player moves and keep the complexity of the map down. 我是否最好将其设计为在单个画布中运行,以便在玩家移动时重新绘制地图并降低地图的复杂性。 and program it so it doesn't think about parts of the map that aren't close to the player. 对其进行编程,这样它就不会考虑地图上与玩家不近的部分。

I've read a bunch about tricks to increase performance like layering canvases and not redrawing the canvas more than the browser is refreshing. 我已经阅读了很多有关提高性能的技巧,例如将画布分层,而不是在浏览器刷新之前重新绘制画布。 but I've been having trouble finding info on good practice for dealing with large maps and performance of moving around them. 但我一直在寻找有关处理大型地图以及在大型地图上移动的性能的良好做法的信息时遇到麻烦。

I'm really interested to hear everyone's thoughts. 我真的很想听听每个人的想法。

Question 1 is not actually a question, but it's best to create a canvas that is the size of your screen, translate the canvas and redraw PART OF the map with each movement. 问题1实际上不是问题,但最好创建一个与屏幕大小相同的画布,平移画布,并随着每次移动重新绘制地图的PART OF。 Do not redraw the whole map, but add methods to redraw a portion of the map that is slightly larger than the viewport, possibly excluding part of the map that is already drawn. 不要重绘整个地图,而要添加方法来重绘比视口​​稍大的地图部分,可能会排除已经绘制的部分地图。 Otherwise your browser might crash from memory stress. 否则,您的浏览器可能会由于内存压力而崩溃。

Question 2: Right. 问题2:正确。 There will be no difference at all. 完全没有区别。 The browser will simply move the already drawn canvas about behind the viewport. 浏览器将简单地将已绘制的画布在视口后面移动。

Question 3: Play around with the element inspector of Chrome on a google map. 问题3:在Google地图上与Chrome的元素检查器一起玩。 This is a very good example of a huge map that runs on a tiny phone. 这是在微型电话上运行的巨大地图的一个很好的例子。 You will see that it uses a grid of images (you could uses canvasses). 您将看到它使用图像网格(可以使用画布)。 You could cache these blocks outside the viewport for if the user returns back. 如果用户返回,则可以将这些块缓存在视口之外。 Play with the cache size to keep the memory usage sane. 发挥缓存大小以保持内存使用合理。

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

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