I would like to adjust the dimensions of a SVG-Element to its content.
JSfiddle: http://jsfiddle.net/4pD9N/
Here I got an example. The SVG should 'shrink' to fit to the elements + 10px margin on each side. In other words: I want to crop the svg for the unused space...
I know that I have to use getBBox
. But how can I calculate the total width and height?
By default, the origin of the SVG drawing is the same as the origin of the container. It means that the
(0,0)
of your drawing is in the upper left corner of the SVG element. To center the content, you should translate the drawing relatively to the container. It can be achieved by modifying theviewBox
attribute (four values expected :"minx miny width height"
) of the SVG element with respect to the result of thegetBBox()
method. A live example at http://jsfiddle.net/3cp3c/.