简体   繁体   English

如何在canvas元素中读取SVG的viewbox属性?

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

I have added an SVG to a canvas element. 我在画布元素中添加了SVG。 How do I get access to read the viewbox attribute of the SVG? 如何获得读取SVG的viewbox属性的权限?

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. 浏览器将加载SVG并将其渲染为位图。 All you have then is the Image() / HTMLImageElement . 然后,您所拥有的只是Image() / HTMLImageElement The actual SVG is gone by that point. 到那时,实际的SVG已经消失了。

You would need to load the SVG separately. 您需要单独加载SVG。 There are various ways to do that, eg. 有多种方法可以做到这一点,例如。 with AJAX, or create an <object> element in your DOM. 使用AJAX,或在DOM中创建<object>元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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