Hello!
We noticed some problems with SVG files that don’t have an explicit width
and height
attribute on their root element.
Safari works as expected. Chrome renders with wrong dimensions. Firefox does not render.
I would suggest a fallback to the viewbox
attribute for width and height.
2 Likes
There is no magic in the way we deal with SVGs. for us they are nothing more than images that are loaded using the browser’s Image object.
Would you be able to share the svg (and maybe a quick playground) so that we can see that the best way to get all browsers to work the same?
I will prepare a playground.
return svgsrc;
} else {
return value;
}
}
/**
* Sets sourceLeft, sourceTop, sourceWidth, sourceHeight automatically
* given external svg file and icon id
*/
private _getSVGAttribs(svgsrc: HTMLObjectElement, elemid: string) {
var svgDoc = svgsrc.contentDocument;
// get viewbox width and height, get svg document width and height in px
if (svgDoc && svgDoc.documentElement) {
var vb = svgDoc.documentElement.getAttribute("viewBox");
var docwidth = Number(svgDoc.documentElement.getAttribute("width"));
var docheight = Number(svgDoc.documentElement.getAttribute("height"));
// get element bbox and matrix transform
var elem = svgDoc.getElementById(elemid) as Nullable<SVGGraphicsElement>;
if (vb && docwidth && docheight && elem) {
var vb_width = Number(vb.split(" ")[2]);
In this method you are using the attributes to calculate the dimensions, this should also work without the width
and height
attributes.
1 Like
You are totally right. @msDestiny14 - want to take this one?
This is probably the best solution.
And here is a sentence we hardly ever read.
3 Likes
Ha! Ain’t that the truth. Appreciate your attention on this, @RaananW and @msDestiny14 !
1 Like
Yep! Poke me when you have a PG and I will test and push. Nice find.