Canvas 2d context or WebGL for 2D game

17,014

Solution 1

The short answer is yes. WebGL can be quite a bit more efficient if you use it well. A naive implementation will either yield no benefit or perform worse, so if you're not already familiar with the OpenGL API you may want to stick to canvas for the time being.

A few more detailed notes: WebGL can draw textured quads (sprites) very very fast, but if you need more advanced 2D drawing features such as path tracing you'll want to stick to a 2D canvas as implementing those types of algorithms in WebGL is non-trivial. The nature of your game also makes a difference in your choice. If you only have a few moving items on screen at a time Canvas will be fairly fast and reasonably simple. If you're redrawing the entire scene every frame, however, WebGL is better suited to that type of render loop.

My recommendation? If you're just learning both, start with Canvas2D and make your game work with that. Abstract your drawing in a simple manner, such as having a DrawPlayer function rather than ctx.drawImage(playerSprite, ....), and when you reach a point where the game is either functioning and you want it to run faster or the game design dictates that you MUST use a faster drawing method, create an alterate rendering backend for all those abstract functions with WebGL. This gives you the advantages of not getting hung up on rendering tech earlier on (which is ALWAYS a mistake!), let's you focus on gameplay, and if you end up implementing both methods you have a great fallback for non-WebGL browsers like Internet Explorer. Chances are you won't really need the increased speed for a while anyway.

Solution 2

WebGL can be much faster than canvas 2D. See http://blog.tojicode.com/2012/07/sprite-tile-maps-on-gpu.html as one example.

That said, I think you're mostly on your own right now. I don't know of any 2d libraries for WebGL except for maybe PlayN http://code.google.com/p/playn/ though that's in Java and uses the Google Web Toolkit to get converted to JavaScript. I'm also pretty sure it doesn't use the techniques mentioned in that blog post above, although if your game does not use tiles maybe that technique is not useful for you.

three.js is arguably not the library you want if you're planning on 2d.

Share:
17,014

Related videos on Youtube

Pic
Author by

Pic

Updated on September 15, 2022

Comments

  • Pic
    Pic over 1 year

    I'm planning on writing a game, which will use a lot of sprites and images. At first I tried EaselJS but playing some other canvas-based games I realized it's not that fast. And when I saw BananaBread by Mozilla I thought "if WebGL can do 3D so fast, then it can do 2D even faster". So I moved to three.js (using planes and transparent textures, texture offset for sprites).

    The question is: is it better? Faster? Most of the WebGL games are 3D so should I use canvas 2D context for 2D and WebGL for 3D? I've also noticed that there are no libraries for WebGL in 2D (except WebGL-2d, but it's quite low level).

    Please note that compatibility is not my greatest concern as I'm not planning on releasing anything anytime soon :) .

  • Pic
    Pic over 11 years
    Thanks a lot! That's what I'm doing right now, having a separate file for display. As gman mentioned below, three.js is not a library for 2d so I might stick with canvas as it has some good libraries (I'm not interested in low-level graphics programming, so libraries are a must).
  • Chris Cooper
    Chris Cooper about 11 years
    This was an excellent answer; thanks a lot. I have just enough OpenGL experience that I have been foolish enough to throw myself at WebGL for a simple game. The approach you described in your last paragraph is exactly what I should be doing.
  • gdw2
    gdw2 almost 11 years
    pixi.js is a another webgl 2d library
  • klh
    klh about 10 years
    Pixi.js is great unless you need access that low-level canvas APIs, since it's pretty inefficient to redraw everything on PIXI.Graphics every frame. Other than that it's perfect for 2D on WebGL.