tags:

views:

394

answers:

1

So how to get the size of an SVG file with javascript when width and height are not set on the svg element? The svg is a string, but could be made a DOM if necessary.

+1  A: 

SVGs are scalable vector graphics, and thus can have any arbitrary height and width. Only the ratio is fixed by the format.

jball
Thanks a lot, i know this, but in the SVG Viever widget i make(http://my.opera.com/SpShut/blog/show.dml/5803641) i need to display thumbnails of SVG files(say 100x100), and if a file has neither width/height nor viewBox attributes, it's displayed at *some size* and only 100x100 portion of it is visible. I want to process the file somehow to get the dimensions and set width/height and viewBox attributes.
Spadar Shut
You'll have to make an assumption about it then. If your thumbs are 100x100 then why not display the SVG at 100x100?
jball
Spadar Shut
With no height and width set in the tag, there is no intrinsic size. You could look through each element in the svg and see what the largest x + width is and largest y + height, and call that the size. I'm not sure that you gain any advantages from that. Conceptually, what are you thinking of as size if not the intrinsic height and width?
jball
Spadar Shut
You can also try using an 'img' element instead of 'object', since that will automatically generate a viewBox for you as long as you have 'width' and 'height' set in the svg.
Erik Dahlström