Currently, I am developing a program that dynamically generates svgs like the one shown below:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="330.75" height="41.91000000000008" viewBox="0 0 330.75 41.91" xml:space="preserve">
<desc>Created with Fabric.js 2.4.3</desc>
<defs></defs>
<g transform="matrix(6.75 0 0 1.27 165.38 20.96)">
<rect style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(25, 205, 255); fill-rule: nonzero; opacity: 1; transform-origin: 50px 0px; transform: translateX(0%) translateY(0px);" x="-24" y="-16" rx="0" ry="0" width="48" height="32"></rect>
</g>
</svg>
When utilizing jQuery to determine the widths of the svg and g elements, I obtain the following results:
$('svg')[0].getBoundingClientRect().width = 802.79
$('svg > g')[0].getBoundingClientRect().width = 786.33
I aim for the width of the g element to precisely match that of the svg element. How can I achieve this?
I experimented with the following approach:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="330.75" height="41.91000000000008" viewBox="0 0 330.75 41.91" xml:space="preserve">
<desc>Created with Fabric.js 2.4.3</desc>
<defs></defs>
<g width="330.75" height="41.91000000000008">
<rect style="stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(25, 205, 255); fill-rule: nonzero; opacity: 1; transform-origin: 50px 0px; transform: translateX(0%) translateY(0px);" x="-24" y="-16" rx="0" ry="0" width="48" height="32"></rect>
</g>
</svg>
However, this solution doesn't work as expected - my rect element loses its initial width/height and becomes very small.
If you have any insights on how to ensure that the g element (along with all its content) always adjusts its width to fit the svg width, please let me know.