Skip to content Skip to sidebar Skip to footer

HTML5 Canvas Performance: Loading Images Vs Drawing

I'm planning on writing a game using javascript / canvas and I just had 1 question: What kind of performance considerations should I think about in regards to loading images vs jus

Solution 1:

If you're drawing simple shapes with solid fills then drawing them procedurally is the best method for you.

If you're drawing more detailed entities with strokes, gradient fills and other performance sensitive make-up you'd be better off using image sprites. Generating graphics procedurally is not always efficient.

It is possible to get away with a mix of both. Draw graphical entities procedurally on the canvas once as your application starts up. After that you can reuse the same sprites by painting copies of them instead of generating the same drop-shadow, gradient and strokes repeatedly.

If you do choose to draw sprites you should read some of the tips and optimization techniques on this thread.

My personal suggestion is to just draw shapes. I've learned that if you're going to use images instead, then the more you use the slower things get, and the more likely you'll end up needing to do off-screen rendering.


Solution 2:

This article discusses the subject and has several tests to benchmark the differences.

Conculsions

In brief — Canvas likes small size of canvas and DOM likes working with few elements (although DOM in Firefox is so slow that it's not always true).

And if you are planing to use particles I thought that you might want to take a look to Doodle-js.


Solution 3:

Image loading out of the cache is faster than generating it / loading it from the original resource. But then you have to preload the images, so they get into the cache.


Solution 4:

It really depends on the type of graphics you'll use, so I suggest you implement the easiest solution and solve the performance problems as they appear.

Generally I would expect copying a bitmap (drawing an image) to get faster compared to recreating it from primitives, as the complexity of the image gets higher.

That is drawing a couple of squares per scene should need about the same time using either method, but a complex image will be faster to copy from a bitmap.


Solution 5:

As with most gaming considerations, you may want to look at what you need to do, and use a mixture of both.

For example, if you are using a background image, then loading the bitmap makes sense, especially if you will crop it to fit in the canvas, but if you are making something that is dynamic then you will need to using the drawing API.

If you target IE9 and FF4, for example, then on Windows you should get some good performance from drawing as they are taking advantage of the graphics card, but, for more general browsers you will want to perhaps look at using sprites, which will either be images you draw as part of the initialization and move, or load bitmapped images.

It would help to know what type of game you are looking at, how dynamic the graphics will need to be, how large the bitmapped images would be, what type of framerate you are hoping for.


Post a Comment for "HTML5 Canvas Performance: Loading Images Vs Drawing"