Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the real size of a SVG/G element

Tags:

Is there any accurate way to get the real size of a svg element that includes stroke, filters or other elements contributing to the element's real size from within Javascript?

I have tried pretty much everything coming to my mind and now I feel I'm coming to a dead end :-(

Updated question to add more context (Javascript)

like image 921
Emil Avatar asked Mar 23 '12 23:03

Emil


People also ask

How do I get the SVG file size?

The thing is: SVG images don't have a "size" in the sense you are probably thinking of. On the other hand, they DO have a height-to-width ratio. This ratio can usually be found in the viewBox attribute.

What is the G element in SVG?

The SVG <g> element is a container used to group other SVG elements. Transformations applied to the <g> element are also performed on its child elements, and its attributes are inherited by its children.

Does SVG have scale?

Once you add a viewBox to your <svg> (and editors like Inkscape and Illustrator will add it by default), you can use that SVG file as an image, or as inline SVG code, and it will scale perfectly to fit within whatever size you give it. However, it still won't scale quite like any other image.

How do I move G in SVG?

Use transform="translate(x,y)" to move the g around and things within the g will move in relation to the g .

What is the <G> element in SVG?

The <g> SVG element is a container used to group other SVG elements. Transformations applied to the <g> element are performed on all of its child elements, and any of its attributes are inherited by its child elements. It can also group multiple elements to be referenced later with the <use> element.

How to get the size of an SVG object?

SVG has properties width and height. They return an object SVGAnimatedLength with two properties: animVal and baseVal. This interface is used for animation, where baseVal is the value before animation. From what I can see, this method returns consistent values in both Chrome and Firefox, so I think it can also be used to get calculated size of SVG.

What happens if I set the height or width to auto for SVGS?

Same with SVG. By default, it will be drawn at the size specified in the code, regardless of the size of the canvas.What happens if you set the height or width (or both) to auto for these SVGs? The default size for HTML replaced elements will be used: 300px wide, 150px tall.

Does SVG scale like other images?

However, scaling SVG goes beyond what is possible with other images. A first glance at the SVG specifications would suggest that the height and width attributes on the top-level svg element will implicitly set an aspect ratio and therefore make SVG scale like other images.


2 Answers

You can't get the values directly. However, you can get the dimensions of the bounding rectangle:

var el   = document.getElementById("yourElement"); // or other selector like querySelector() var rect = el.getBoundingClientRect(); // get the bounding rectangle  console.log( rect.width ); console.log( rect.height); 

It is supported at least in the actual versions of all major browser.

Check fiddle

like image 156
Christoph Avatar answered Nov 11 '22 01:11

Christoph


Both raphael js http://dmitrybaranovskiy.github.io/raphael/ and d3 js http://d3js.org/ have various methods to find the size of an svg object or sets of svg object. It depends on if it's a circle, square, path, etc... as to which method to use.
I suspect you are using complex shapes, so in that case bounding box would be your best bet http://raphaeljs.com/reference.html#Element.getBBox (Edit: updated reference site.) http://dmitrybaranovskiy.github.io/raphael/reference.html#Element.getBBox

like image 41
Thaddeus Albers Avatar answered Nov 11 '22 01:11

Thaddeus Albers