Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 just drawing using canvas' methods. Because my game will be using very simple geometry for the art (circles, squares, lines), either method will be easy to use. I also plan to implement a simple particle engine in the game, so I want to be able to draw lots of small objects without much of a performance hit.

Thoughts?

like image 376
user430166 Avatar asked Aug 15 '10 17:08

user430166


People also ask

How fast is HTML5 Canvas?

The Canvas tab loaded in one second and takes up 30MB. It also takes up 13% of CPU time all of the time, regardless of whether or not one is looking at it. Video on the HTML page, while I am not moving objects, is actually perfectly smooth.

What are the advantages of HTML5 draw a square using canvas?

Advantages - Interactivity - Canvas is fully interactive and can react to a user's actions by listening to the keyboard, mouse, or touch events. So, a developer does not get restricted to only static graphics and images. Animation – Each and every object created on the canvas can be animated.

How can I speed up my canvas?

You can adjust the playback speed of videos you view within Canvas and UD Capture Space. At the bottom of your video playback window, locate the “1x” button and click it. You are then presented with additional playback speed, both faster and slower. See screenshot below.


2 Answers

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.

like image 120
jaredwilli Avatar answered Sep 28 '22 06:09

jaredwilli


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.

like image 35
Mariano Desanze Avatar answered Sep 28 '22 05:09

Mariano Desanze