I am getting the width of a bounding box by using the code:
document.getElementById("dataId").getBBox().width;
This will return me the width of the bounding box with the entered id. This is working fine in all browsers(Chrome,firefox21, IE7) but not in IE10. In IE10, it is returning me the value of 0.
This is the example I created in jsfiddle. Kindly check it. http://jsfiddle.net/L82rn/
I am curious if there any compatibility issues between IE10 and svg getBBox method, please do let me know if there is any issue on this.
As quoted from the spec in this thread I think it's this
Note that getBBox must return the actual bounding box at the time the method was called, even in case the element has not yet been rendered.
The "even in the case the element has not yet been rendered" seems to be the sticking point for IE, this problem persists a year later now on IE11. The only other possibility that I can see out of experience is that with g tags not having an explicit width and height according to the spec IE might just be foregoing this check completely, as in "to not have a width" is presumably the same as 0 for the IE codebase.
So the workaround would be to look for the biggest child as you go. In your particular case as a workaround you can just grab the lastChild
when you add it to the g tag. Change the JS code in the fiddle to this (I fixed the svgns thing too as mentioned by Thomas W in the comments).
var temp;
temp = document.createElementNS("http://www.w3.org/2000/svg","text");
temp.appendChild(document.createTextNode(".."));
temp.setAttribute("x",38);
temp.setAttribute("y",18);
document.getElementById("tata").appendChild(temp);
alert(document.getElementById("tata").lastChild.getBBox().width);
As you can see it's just the last line I've changed shoving in lastChild
before getBBox()
, should alert an 8 instead of a 0 now as Firefox and your other browsers did.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With