exupero / saveSvgAsPng

Save SVGs as PNGs from the browser.
MIT License
1.09k stars 362 forks source link

HTMLElement or SVGElement in Chrome #250

Open canonex opened 3 years ago

canonex commented 3 years ago

hi, I have in the HTML:

<object id="myid" data="mySvg.svg" height="100%" ></object>

Then, to get the element:

let svgObject = document.getElementById( "myid" );
let svgDom = svgObject.contentDocument.querySelector("svg");

saveSvgAsPng(svgDom, "myname.png");

This works in Firefox but in Chrome I receive an error: Error: an HTMLElement or SVGElement is required; got [object SVGSVGElement]

svgDom should be an Element...

I don't understand if it's my mistake, a browser problem or a problem with saveSvgAsPng. How can I solve the problem?

Thank you, Riccardo

canonex commented 3 years ago

After further investigation I noticed that SVGSVGElement is fully compatible with saveSvgAsPng.

The only real problem is on the preliminary check: const isElement = obj => obj instanceof HTMLElement || obj instanceof SVGElement;

Of course [object SVGSVGElement] will fail the test. I can't figure out what this object is instance of but adding a check on nodeName: const isElement = obj => obj instanceof HTMLElement || obj instanceof SVGElement || obj.nodeName == "svg";

Probably this is not the best way but makes the same code works for Firefox and Chrome.