So, I'm using canvg and the function which converts svg file to a jpg/png just downloads and ignores id of a svg block element, so I get the blank image, what could be wrong? Maybe Vue does not support converting SVG to jpg/png using canvas. Here is the javascript:
import canvg from "canvg";
function SVG2PNG(svg, callback) {
var canvas = document.createElement("canvas");
var w = 800;
var h = 400;
canvas.width = w;
canvas.height = h; // Create a Canvas element.
var ctx = canvas.getContext("2d"); // For Canvas returns 2D graphic.
debugger;
var data = svg.outerHTML; // Get SVG element as HTML code.
canvg.from(canvas, data); // Render SVG on Canvas.
callback(canvas); // Execute callback function.
}
function generateLink(fileName, data) {
var link = document.createElement("a");
link.download = fileName;
link.href = data;
return link;
}
export default {
methods: {
tipka() {
debugger;
var element = document.getElementById("svg"); // Get SVG element.
SVG2PNG(element, function (canvas) {
// Arguments: SVG element, callback function.
var base64 = canvas.toDataURL("image/png"); // toDataURL return DataURI as Base64 format.
generateLink("SVG2PNG-01.png", base64).click(); // Trigger the Link is made by Link Generator and download.
});
},
},
};
Here is the ending of svg tag:
This is the start of svg tag:

Per the documentation for
Canvg.from, you need to pass it your drawing context, not the canvas itself. So change this line:to:
It also appears, per this answer, that you will need to replace your
viewBoxattribute on the<svg>withwidthandheightattributes, like:You can then actually bypass canvg entirely with the function rewritten like this:
(Note that it now returns the data URL, not the canvas.)