Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 Canvas and Game Programming

Tags:

html

canvas

I hope this isn't too open ended.

I'm wondering if there is a better (more battery-friendly) way of doing this --

I have a small HTML 5 game, drawn in a canvas (let's say 500x500). I have some objects whose positions I update every 50ms or so. My current implementation re-draws the entire canvas every 50ms. I can't imagine that being very good for battery life on mobile platforms.

Is there a better way to do this? This must be a common pattern with games.

EDIT:

as requested, here are some more updates:

Right now, the objects are geometric primitives drawn via arcs and lines. I'm not opposed to making these small png/jpg/gif files instead of that'd help out. These are small graphics -- just 15x15 or so.

As the game progresses, more and more of the screen changes at a time. However, at the start, the screen changes relatively slowly (the objects randomly moved a few pixels every 50ms).

like image 684
jglouie Avatar asked Dec 20 '22 20:12

jglouie


2 Answers

Nearly every game with continuous animation like this redraws everything every frame; clever updating algorithms are only applicable when a small part of the screen is changing and there is a nice rule to figure out what is overlapping that part.

Here is some general optimization advice:

  • Make sure that as much as possible of your graphics are handled by the GPU and not the CPU. (This may be impossible if the user's browser does not use the GPU for 2D canvas rendering, but you can expect upgrades may change that as HTML5 gaming gains popularity.)

    • This means that you should avoid elaborate clever algorithms in favor of doing as little work as possible in JS code — except that avoiding performing a lot of drawing when it is easy to determine that it will be invisible (e.g. outside the bounds of the screen) is generally worthwhile.

    • If your target platforms support it (generally not the case for current mobile devices), try using WebGL instead of 2D Canvas. You will have to do more detail work, but WebGL allows you to use operations which are much more likely to be provided efficiently by the GPU hardware.

  • If your game becomes idle — that is, nothing is actually animating at the moment — stop redrawing. Stop your update loop until the user interacts with the game or a timeout occurs.

It may be helpful for you to add to your question details of what types of graphics you are drawing (e.g. are you using sprites, or geometric primitives? Are you drawing images rotated/scaled? Does most of the screen change or just a few small objects? Are you blending many layers?) and perhaps even a screenshot or two; then we can suggest what sort of optimizations are suitable for your particular game.

like image 101
Kevin Reid Avatar answered Dec 23 '22 08:12

Kevin Reid


Don't draw a background, make it an image and set the CSS background-image of the canvas.

Using requestAnimationFrame should help with battery life.

http://paulirish.com/2011/requestanimationframe-for-smart-animating/

Only do a redraw if something has actually changed. If you haven't already, introduce the concept of invalidations. (ie, the canvas is valid so nothing redraws until something moves. Anything moving within the window of the canvas causes the canvas to become invalid, thus needing a redraw)

like image 35
Simon Sarris Avatar answered Dec 23 '22 08:12

Simon Sarris