Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get width and height of SVG on Image.load in IE 11

It works fine everywhere but not in IE 11 (I have not tested other IE versions yet).

var img = new Image();
img.onload = function(){

   alert( 'img: ' + img.width + 'x' + img.height + 
          '  natural: ' + img.naturalWidth + 'x' + img.naturalHeight );

};
img.src = 'http://upload.wikimedia.org/wikipedia/en/b/b5/Boeing-Logo.svg';

JSFiddle: JSFiddle

Result:

img: 121x30 natural: 121x30 - Real browsers (Chrome, Safari, Firefox, ...)

img: 0x0 natural: 0x0 - IE 11

There is a similar question here: Getting image width on image load fails on IE

None of the solutions from those answers work for svg.

Is there a way to get the width and height of a svg file loaded with Image() in Internet Explorer 11?

Note: I am looking for a solution without having to add the element to the DOM for measuring, as I want to avoid any unnecessary re-flow/repaint.

like image 471
sanchez Avatar asked Apr 02 '14 20:04

sanchez


People also ask

How to get exact size of SVG image file in IE11?

When you try to get size of svg image file using “image.onload” event, this returns exact file size in all browsers (Firefox, Chrome, Safari) except IE11. It returns 0 for width and 0 for height in IE11. 1. Open IE11.

How do I auto-size an SVG image?

If you completely auto-size the image, Internet Explorer applies the standard default 300×150 size. However, other browsers will apply { width: 100%; height: auto; } by default if the image has a viewBox; this behaviour is not defined in any specification. So to recap: To auto-scale SVG used as <img>, Set a viewBox attribute.

How do I make an SVG jacket fit in IE11?

What you can do is add the height="320" attribute to your SVG tag. So IE can render correctly. I believe IE11 is thrown off by using width 200% in your CSS. But since xml:space="preserve" is the default, setting only the height will keep the proportions of your SVG jacket.

How do I set the default height of an SVG file?

# The height and width attributes. So a rule like svg {width: 100%; height: auto;} will cancel out the dimensions and aspect ratio you set in the code, and give you the default height for inline SVG. Which, as mentioned above, will be either 150px or 100vh, depending on the browser.


2 Answers

This code works in IE11:

var img = new Image();
img.onload = function(){
    document.body.appendChild(this);
    alert( 'img: ' + this.offsetWidth + 'x' + this.offsetHeight);
    document.body.removeChild(this);
};
img.src = 'http://upload.wikimedia.org/wikipedia/en/b/b5/Boeing-Logo.svg';

And yes - this solution is not ideal.

like image 193
dimacpp Avatar answered Nov 06 '22 06:11

dimacpp


Well, I don't think you can access the SVG content if it is loaded as a src attribute, and not inline.

One solution might be to change the way the SVG is loaded, so perhaps load via AJAX, and then append to the document by another means. This gives you a chance to have full access the the SVG source before adding to the document...

/* use ajax (and in this example jQuery) to get SVG as XML */
$.get('http://upload.wikimedia.org/wikipedia/en/b/b5/Boeing-Logo.svg', function(svgxml){

    /* now with access to the source of the SVG, lookup the values you want... */
    var attrs = svgxml.documentElement.attributes;
    alert( 'img: ' + attrs.width.value + 'x' + attrs.height.value );

    /* do something with the svg, like add it to the document for example... */
    $(document.body).append(document.importNode(svgxml.documentElement,true));

}, "xml");

JSFiddle

This example has used jQuery, and loads the content of the SVG as xml, but you could do it in many ways following the same principle, for example loading as text string, and accessing with regular jQuery methods, or without jQuery at all.

The moral of the story, is that if you load it via AJAX, you can get a reference to the content of the SVG and have more control over it before it gets added to the page.

like image 31
Billy Moon Avatar answered Nov 06 '22 08:11

Billy Moon