最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - SVG text element speed - Stack Overflow

programmeradmin2浏览0评论

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 plex 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.

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 plex 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.

Share Improve this question asked Nov 20, 2012 at 16:11 gintasgintas 2,1781 gold badge19 silver badges28 bronze badges 6
  • Are you using paper.print() or paper.text() to create your text elements. Also a fiddle might be good. – Bruno Commented Nov 20, 2012 at 16:20
  • Are you targetting a specific set of browsers? The performance differs dramatically between browsers. – Sebastian Commented Nov 20, 2012 at 16:23
  • @Bruno I am using paper.text(), which is still quite slow. – gintas Commented Nov 20, 2012 at 16:51
  • 1 @gintas any chance we could have a look at some of your code that is moving around the different raphael objects – Bruno Commented Nov 20, 2012 at 18:39
  • 1 ISTR that Raphaël is a bit inefficient when it es to text, as a parison here's an example with 1000 text elements scrolling in pure svg - jsfiddle/arJZL – Erik Dahlström Commented Nov 21, 2012 at 12:50
 |  Show 1 more ment

2 Answers 2

Reset to default 6

You can prerender the text using Canvas and embed images into the SVG. I don't know how this pares 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.

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.. They can be used to pare 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

发布评论

评论列表(0)

  1. 暂无评论