When using an SVG generated by a chart plugin ( https://wordpress.org/plugins/visualizer/ ) I can't figure out how to retrieve the source of the SVG-image being generated. I have tried to use other SVG's with exactly the same code and it works like a charm, but SVG's generated from the Visualizer plugin - for some reason - does not work.
The onload-function is not triggered.
No errors are shown in console.
No image is created in canvas
My suspicion is that there are stuff in that SVG that are not valid - which makes the source of SVG not being valid!? I'm not sure, but that seems to be som kind of issue. Please tell me if I'm totally on the "wrong track" here..
Javascript:
var svgText = document.getElementById("myViewer").outerHTML;
var myCanvas = document.getElementById("canvas");
var ctxt = myCanvas.getContext("2d");
function drawInlineSVG(ctx, rawSVG, callback) {
var svg = new Blob([rawSVG], {type:"image/svg+xml;charset=utf-8"}),
domURL = self.URL || self.webkitURL || self,
url = domURL.createObjectURL(svg),
img = new Image;
img.onload = function () {
//Does not come here...
ctx.drawImage(this, 0, 0);
domURL.revokeObjectURL(url);
callback(this);
};
img.src = url;
}
// usage:
drawInlineSVG(ctxt, svgText, function() {
console.log(canvas.toDataURL()); // -> PNG
alert("see console for output...");
});