Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to speed up my SVG. Could I convert to WebGL or canvas?

I have an SVG drawing with 10138 parts, so it runs slowly.
I want to make it run more like http://workshop.chromeexperiments.com/globe

Here are the solutions/questions I'm considering

  • Is there any way to have the SVG parts render in a less processor-intensive way?
  • Can I convert the SVG to WebGL or canvas?
  • Can I have it run as SVG but render thru WebGL or canvas?

I just want to make it faster .. thoughts?

Here is a screenshot

like image 998
Kirk Strobeck Avatar asked Jul 24 '12 20:07

Kirk Strobeck


People also ask

Which is faster SVG or Canvas?

SVG gives better performance with smaller number of objects or larger surface. Canvas gives better performance with smaller surface or larger number of objects. SVG is vector based and composed of shapes. Canvas is raster based and composed of pixel.

Why Canvas is better than SVG?

Canvas gives better performance with smaller surface or larger number of objects. SVG can be modified through script and CSS. Canvas can be modified through script only. Multiple graphical elements, which become the part of the page's DOM tree.

Is SVG faster than HTML?

The long answer: Those SVG DOM references mean that some of the footwork of dealing with the things you draw is done for you. And SVG is faster when rendering really large objects, but slower when rendering many objects.


2 Answers

As a simplified rule of thumb:

  • SVG scales reciprocally with number of objects to be drawn
  • Canvas scales reciprocally with resolution.

So I 10138 objects to be kept in memory with SVG will slow down things (though I can't say what the hard limits are). If you get into this range of objects I believe that canvas and WebGL might provide better performances. Most modern browser already support hardware accelerated canvas rendering.
However doing user interaction is more involved with Canvas compared to SVG.

You can also try to mix them (see here for more details).
Here are some useful resources:

  • SVG swarm vs Canvas swarm (using D3.js)
  • Stackoverflow thread with useful infos
like image 155
Ümit Avatar answered Oct 22 '22 08:10

Ümit


The slowdown for an SVG with a very large number of pieces comes from the non-retained mode of SVG. Without more details about what your SVG looks like and how it behaves, it's hard to make concrete suggestions. So in general:

  • If your graphic is generally static (you don't need to track mouse events per graphical object) you can use an HTML5 canvas instead (where the drawing commands blit pixels to an image and thereafter you basically have a static image).

  • If your graphic has a lot of repeated parts, using SVG with <use> elements may reduce the memory footprint and improve performance.

like image 40
Phrogz Avatar answered Oct 22 '22 08:10

Phrogz