我正在尝试使用画布将外部svg图标转换为base64 png格式。在除了Firefox以外的所有浏览器中都可以正常工作,但在Firefox中会抛出错误"NS_ERROR_NOT_AVAILABLE"。
var img = new Image();
img.src = "icon.svg";
img.onload = function() {
var canvas = document.createElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL;
};
有人能帮我吗?谢谢您的帮助。
<svg>
元素上是否有宽度/高度属性?如果有,这些属性值是百分比吗? - Robert LongsondrawImage()
时甚至没有抛出错误。在<svg>中添加width/height确实解决了它! - Eldritch Conundrum