javascript - Alter dynamically-generated SVG to make it responsive? -


i'm trying use svg library two.js or svg.js , alter (on-the-fly) dynamically generated svg elements in order make them responsive. have basic example here, showing how predefined, inline svg can responsive.

it involves wrapping svg element in container, assigning css values, adding viewbox , preserveaspectratio attributes. appears common way responsive svg elements:

http://jsfiddle.net/n4pk4/

<div class='container'>     <div class='svg-container'>         <svg version="1.1" viewbox="0 0 500 500" preserveaspectratio="xminymin meet" class="svg-content">             <circle fill="#f7941e" stroke="#231f20" stroke-width="10" cx="250" cy="250" r="200" opacity="1" />         </svg>     </div> </div>  .svg-container {     display: inline-block;     position: relative;     width: 100%;     outline: 1px solid red;     padding-bottom: 100%;     vertical-align: middle;     overflow: hidden; }  .svg-content {     display: inline-block;     position: absolute;     top: 0;     left: 0; }  .container {     max-width: 400px; } 

however, when dynamically generate svg element two.js, , try use jquery edit dom , make svg element responsive (similar above example), svg element no longer visible:

http://jsfiddle.net/g5wzj/5/

you can see have 2 svgs in example. first 1 one doesn't show up. second, inline, static svg markup copied generated in first example. obviously, not markup problem.

also, here similar example using svg.js. same result:

http://jsfiddle.net/3luaw/5/

so basically, problem lies in fact if create static svg element in html, it's easy make responsive. if try dynamically create svg element, difficult (or impossible dunno?) make responsive.

i know svg dom kind of different normal html dom, there can unexpected results when trying make changes dynamically.

how overcome hurdle?

sure, svg dom kind of different normal html dom. you'd better use special-purpose svg lib handle that. use svg.js or two.js modify svg node, not jquery.

if want app compatible old bjust start work, can try raphealjs too.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -