简体   繁体   中英

How do I read the viewbox attribute of an SVG in a canvas element?

I have added an SVG to a canvas element. How do I get access to read the viewbox attribute of the SVG?

var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');

var img = new Image();
img.id = "svgObjectElement";
img.type = "image/svg+xml";

img.onload = function(){

    ctx.drawImage(img,0,0);

};

img.src = "mySvg.svg";

You can't. The browser will load the SVG and render it as a bitmap. All you have then is the Image() / HTMLImageElement . The actual SVG is gone by that point.

You would need to load the SVG separately. There are various ways to do that, eg. with AJAX, or create an <object> element in your DOM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM