I am trying to convert an SVG to a PNG image using the canvas as the "proxy". I can get it to work in Chromium, but not in Firefox. It seams that I need a width and height defined in the SVG, but in my case the SVG does not have width and height attributes. When the SVG is displayed in the browser the size depends on the size of the container/window.
The SVGs are converted to PNG successfully, but this one shell SVG.
Here is the JSFiddle: https://jsfiddle.net/8mqtLw6p/
var img = new Image();
var svg = new Blob([svgString], {
type: "image/svg+xml;charset=utf-8"
});
var url = DOMURL.createObjectURL(svg);
img.onload = function() {
ctx.drawImage(img, 0, 0);
var png = canvas.toDataURL("image/png");
var mg = document.createElement("img");
mg.setAttribute("src", png);
document.body.appendChild(canvas);
document.body.appendChild(mg);
DOMURL.revokeObjectURL(png);
};
img.setAttribute("src", url);
To be able to draw a bitmap image it needs a size (width and height). If the SVG does not have a size specified and you would like to copy it as it is rendered in the browser you can use getBoundingClientRect() to get the width and height.
Now, that you SVG is rather big I just replaced all the paths with a circle and modified the viewBox a bit.