Search code examples
javascriptcanvassvgcanvg

draw svg to canvas with canvg


I use canvg for set svg file in canvas. All is ok when I set svg url:

var canvas = doc.getCanvas(num);
var context = canvas.getContext('2d');

context.drawSvg("http://localhost:8084/manual.svg", 0, 0, width, height);

But when I try to write (it doesn't work):

context.drawSvg("data:image/svg+xml,"+svg, 0, 0, width, height);

where variable svg get manual.svg content.


Solution

  • img.src = "data:image/svg+xml;base64,"+btoa(svgContent);
    context.drawImage(img, 0, 0, width, height);
    

    it's one variant to solve the problem.