html5 - How to place two <g> side by side in an svg? -


hello new html5 , svg tag.i want ask question svg html element. here code

<html>  <div>    <svg width = "1335" height = "400">      // want have 2 svg:g side side 1 of more width , second of less width         such width of svg = first g + second g       <g>           // elements inside g should have same width g       </g>       <g>       </g>     </svg>  <div> </html> 

i have tried using transform.but failed. possible have 2 g elements side side can't set x , y of g ? can 1 guide me of doingthis way.

you can use transform, problem how such values make transformed g @ right place. possible way (the simplest, really) difference between coordinates of bounding boxes. have bounding box bb1 group g1 , bb2 g2, compute translation applied g2.

of course need script computation runtime. such script use

var bb1 = document.getelementbyid("g1").getbbox() 

here code

<svg>     <script>         function align(evt) {             var g1 = document.getelementbyid("g1")             var g2 = document.getelementbyid("g2")             var bb1 = g1.getbbox()             var bb2 = g2.getbbox()             g2.setattribute("transform", "translate("+ ((bb1.x+bb1.width)-bb2.x) + " " + ((bb1.y+bb1.height)-bb2.y) + ")")         }     </script>                     <g id="g1">         <rect fill="red" x="10" y="10" width="40" height="30" />     </g>     <g id="g2" onclick="align(evt)">         <rect fill="blue" x="70" y="60" width="100" height="50" />     </g> </svg> 

you can experiment on jsfiddle it


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -