Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

Question 1: In my understanding, As long as there are no moving objects in my map canvas. 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. 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. 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? 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? 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.

like image 614
tyler mackenzie Avatar asked Dec 23 '14 02:12

tyler mackenzie


People also ask

How can I speed up my canvas?

Step 1: Select the video on the canvas. Step 2: Click on the Speed option at the top. Step 3: Select a specific speed for the video to have it play slower, or faster, than normal.

Is canvas more performant than Dom?

In sum, the overhead of DOM rendering is more poignant when juggling hundreds if not thousands of objects; in this scenario, canvas is the clear winner. However, both the canvas and SVG are invariant to object sizes. Given the final tally, the canvas offers a clear win in performance.

Does animation canvas has a limit when it comes to width and length?

We can create canvas elements that exceed 4096 pixels in width or height as long as the total amount of pixels is below 16777216. const canvas = document. createElement('canvas'); canvas.


1 Answers

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. 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. 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. 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.

like image 139
Hacky Avatar answered Oct 04 '22 10:10

Hacky