Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG text element speed

I am building a web application which relies on svg heavily. For the reference, I am using raphael js library to deal with all of it.

In this particular case I have implemented something that mimics a scrollbar and moves bunch of svg features (~500 elements) across the screen. Part of those features are <text> (~100) elements. Other elements include <rect>, <image> and <path> elements.

So, I noticed that my application is not really very snappy on my laptop, and is down right annoying to deal with on an ipad, due to speed. However, whenever text elements are removed or ignored during scrolling, it immediately gets up to decent speed.

I tried doing some speed tests (very crude ones, using new Date().getTime()) and discovered that it takes ~10 ms to move all the elements except for <text> elements, however it takes ~120 ms when <text> elements are included.

I believe this happens because each letter is rendered as a vector shape and it takes loads of processing power to calculate what exactly is obstructed by such a complex structure.

Is it possible to just embed the text, so the text is rendered as raster graphic, and not as shapes? Or improve performance of rendering text in any other way?

I do not need background transparency, and I do not use any fancy fonts.

like image 952
gintas Avatar asked Nov 20 '12 16:11

gintas


2 Answers

You can prerender the text using Canvas and embed images into the SVG. I don't know how this compares to text element rendering in general, but for our demos this works quite well (see the drop shadow in the "hierarchy" example - they are rendered into canvas first and then replicated and referenced from within the SVG).

Note that these demos also make heavy use of virtualization, i.e. if you zoom into the image and only some of the elements are actually inside the viewport, the others are removed from the SVG, which gives a huge speedup.

The demos do a lot more than just moving the elements around, so it should be easy to get the same or even better performance.

I don't know how to do this with raphael, though, but I believe you should be able to put the data url from the canvas image into the SVG with raphael, too.

like image 145
Sebastian Avatar answered Oct 02 '22 01:10

Sebastian


Paper.print() according to the Raphael site

Creates path that represent given text written using given font at given position with given size

Essentially your text is converted to a path. Obviously this has performance issues.

Probably best to stick to using Paper.text()

UPDATE

So not content with just dishing out advice I have set up some tests on http://www.jsperf.com. They can be used to compare the differences in performance to animate and transform different types of Raphael objects.

If you run these on your iPad it should show if text elements are really much slower to move. One other thing to note is that, at least in the tests I ran, paper.print() and paper.text() were not that different in terms of performance.

Run the tests on jsperf

like image 39
Bruno Avatar answered Oct 02 '22 01:10

Bruno