5

I have two SVG images, both together have about 8000 lines and consist of several shapes with gradients. No patterns, filters, texts, just basic graphical elements with strokes, simple- or gradient fill.

At the moment they are injected inline into the HTML while it is generated, so the source code becomes quiet large. Does this decrease performance for animation, so that it would better to embed the svg differently?

Is there a general rule of thumb to follow when embedding svg?

Greetings philipp

2
  • Everything that uses DOM a lot with slow down rendering of the site so will a large SVG. Even if you use iframes or other embedding methods you don't know at which time the browser with load and render it... except you use Javascript to ensure that its only loaded after the page was rendered. Maybe you render a placeholder first. Commented May 2, 2013 at 12:17
  • @John that is a good point, thanks Commented May 2, 2013 at 13:45

1 Answer 1

1

If you use the HTML5 SVG tag to embed the SVG inline that will not just increase the html file size but also keep DOM busy and your browsers renderer.

If you use iframes you don't really know when it gets loaded or rendered, at least not for all browsers.

HTML5 offers you JavaScript and that might be the solution. Just wait for the body to load and then inject the SVG.

You can use body-onLoad or jQueries ready()-funktion

Here is how it's done in jQuery:

<!DOCTYPE html> <html> <body> <div id="svg-01" class="placeholder"></div> <script src="path/to/jQuery.js"></script> <script> $(document).ready(function(){ $("#svg-01").replaceWith('<svg><!--// some svg //--></svg>'); }); </script> </body> </html> 

I would even go a step further and rather use replace it with an iframe and gziped SVG as described here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.