36

I realize there are many tutorials and sites and everything answering "Which is better for game applications?" They all say that SVG is not suited towards game applications, that canvas is. But this confuses me.

  • Once something is drawn to the canvas, it is forgotten.
  • SVG elements can be changed after creation, without touching any other visual items in the game.
  • Canvas does not have built in animation
  • SVG has built in animations

So let's say I'm making a platformer. I want the main character to walk across the stage with a beautifully painted background. In Canvas, I either have to recreate her entire hitbox (repainting the spot of every moving entity), or make a separate canvas element and use maybe ctxChar, ctxBG, ctxItems etc. With SVG, on the other hand, it's built in. And I can even just put an <animate> tag inside her <use> tag that changes her x position (or manually change it via js), and another <animate> tag to change her sprite. And then the background never has to be touched.

As for animating a bunch of elements simultaneously, I don't see how that's any faster in Canvas than in SVG. What's wrong with putting svg elements in groups? Can it be any slower than redrawing the hitboxes's backgrounds and redrawing the entities themselves for all entities?


Edit: I think my real question is not if SVG could work, but why people think it couldn't.

9
  • You render every frame when animating. No need to have the canvas hold and do animations you do that in Javascript. It animates and keeps track of all content
    – Blindman67
    Commented Aug 11, 2016 at 17:45
  • 3
    Just a heads-up about SVG: in Chrome "SVG's SMIL animations (<animate>, <set>, etc.) are deprecated and will be removed. Please use CSS animations or Web animations instead."
    – AgataB
    Commented Aug 12, 2016 at 9:09
  • 1
    Just for the record, the SMIL deprecation notice was reversed quickly (well, 5 days after @AgataB's message) so SMIL is not deprecated. Also, with Microsoft's tech shift, SMIL support is broader than ever Commented Apr 18, 2020 at 15:46
  • 1
    1) It's really odd for this to have been closed so long after it being asked and getting mostly positive feedback. I notice a recent discussion below that echoes the same confusion I had. But this went 4 years before that happened. It's pretty clear it wasn't the question that changed. Commented Dec 21, 2020 at 21:03
  • 2
    2) This isn't too much more opinion based than "How do I get X to run in less time?" because that gets just as diverse and argumentative answers. My problem was that all the previous information I could find was saying "SVG is terrible" but no one was stating why it was terrible, and I needed to know. The recent discussion is not so much differing opinions as it is a lack of understanding on the part of one person. Commented Dec 21, 2020 at 21:07

1 Answer 1

48

SVG V Canvas

As a professional games creator my advice is to stay away from SVG if you wish to create browser based games. The main reason is that it is simply too slow. Second reason is that it will make your job as a programmer more difficult because finding solutions to SVG's lack of performance is hard. Third reason, because it is VERY SLOW and games are all about performance.

This page will go over SVG game design An Original Approach to Web Game Development Using SVG but before you dive in and read all of it have a look at the prototype of the game it`s all about Runes and Relics Prototype. A little oh hum in my book.

I am sure there are other attempts this was just first up in my search. But I have never played a good SVG game.

So what is SVG good for? Graphic resources. SVG is a good way to package images for your game, Once you have them on the client you can render them to bitmaps and then use them to render your game. The beauty is you get the resolution independent scalability of SVG and small images size. But by rendering them to bitmap at load time you get the full use of the GPU to move pixels about. SVG is just too slow to use within the game.

Why use Canvas? I would like to say because it's fast, but it is not when you compare it to a native C++ app which will run 10* quicker than the equivalent Javascript / canvas game. Ten times quicker is very significant but then you do not get the cross platform advantage you get with Javascript.

Currently canvas is the best option for truly cross platform game applications. Using either 2D or 3D webGL you can build full screen full frame rate games. HTML5 has a good set of APIs for all gaming needs and is easy to learn.

I am not a fan of game engines, they are a cover all solution, the middle man between you and the already well designed API's that force you to do it their (not that great) way. But they will give you a good overview of what is possible.

Phaser is a javascript canvas game engine that has lots of resources and examples. It is a good place to start and see what can be done. Don't be sucked in by the fact that game engines sell themselves as such. HTML5 & JavaScript is out of the box the best game engine in town, runs native code, has a huge user and support base. The time you spend learning how to use a Game engine's API will not be time spent learning how to use the HTML5 API's (and quite frankly the HTML5 APIs are perfect, the result of the 20 year browser wars) which is actually easier than the Game engines to use and learn.

22
  • 4
    Although it doesn't completely tell me why SVG is slow, I suppose this is as good as I'm going to get at my level of understanding. And you did post a resource, hinted at why, and gave me ideas as to how to use SVG and Canvas together, which is probably more useful to me than the information I started out looking for. Commented Aug 15, 2016 at 13:52
  • 1
    @RoboticRenaissance SVG is very difficult to apply Hardware acceleration to. Much of its rendering back end is a mixture of Software and Hardware and thus much slower than pure Hardware rendering back ends. With webGL you can get high performance vector rendering ( with some limitations) but the webGL API is a complex API to pick up and learn. 2D canvas does vectors like SVG but you can also utilise bitmaps and get great Hardware aided rendering, and you get to control how each rendered item is drawn, unlike SVG ( the main reason its so so slow).
    – Blindman67
    Commented Aug 15, 2016 at 14:12
  • 2
    @Blindman67 Check out the particle effect using animejs. codepen.io/juliangarnier/pen/gmOwJX. They basically have a collection of circle exploding from a pointon the screen; there is a single path node for collections of circles of a specific color. Its pretty nifty. Commented Aug 14, 2017 at 12:50
  • 1
    The runes and relics website link is broken. What kind of game was it? Is there a screenshot available?
    – AnnanFay
    Commented Aug 4, 2018 at 1:39
  • 1
    @bob Totally agree games must be fun, but as a commercial developer poor performance means sacrificing quality, reduced visuals, sound FXs, etc... In a saturated and highly competitive market place its the little extra touches that make a game stand out from the crowd, that or a highly original concept.
    – Blindman67
    Commented Jan 13, 2021 at 23:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.