我在FireFox中的toDataURL()函数有问题
想象一下,我有这样的base64图像:
var url = " data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMTkgMTE5Ij48Zz48Y2lyY2xlIGN4PSI1NyIgY3k9IjYyIiByPSI1NyIgc3R5bGU9ImZpbGw6IHJnYigxNjAsIDE2MSwgMTYzKTsgb3BhY2l0eTogMC42OyIvPjxjaXJjbGUgY3g9IjU3IiBjeT0iNTciIHI9IjU3IiBzdHlsZT0iZmlsbDogcmdiKDEyMywgMjEwLCAyNDApOyIvPjxjaXJjbGUgY3g9IjU3IiBjeT0iNTciIHI9IjQ1IiBzdHlsZT0iZmlsbDogcmdiKDI1NSwgMjU1LCAyNTUpOyIvPjx0ZXh0IGR4PSI1NyIgZHk9IjY1IiB0ZXh0LWFuY2hvcj0ibWlkZGxlIiBzdHlsZT0iZm9udC1zaXplOjI1cHg7IGZpbGw6ICMyYjJmMmU7IGZvbnQtZmFtaWx5OiAnRElOUHJvIFJlZ3VsYXInLCBzYW5zLXNlcmlmOyBmb250LXdlaWdodDogYm9sZCI+MTUlPC90ZXh0PjwvZz48L3N2Zz4=";
然后我想重新调整它并发回新的uri:
function getImageUri(url, callback) {
image = new Image();
image.onload = function() {
var image = this;
var canvas = document.createElement('canvas'),
context = canvas.getContext('2d'),
dataURL;
context.drawImage(image, 0, 0, 60, 60);
callback(canvas.toDataURL());
}
image.src = url;
}
最后我只是称它为新的uri
getImageUri(url, function (uri) {
console.log(uri);
document.getElementById('circle').innerHTML = "<img src=" + uri + ">";
});
这个例子的Here is the codepen。它适用于Chrome,但不适用于FireFox。
我在这里搜索了很多相似的问题,大多数时候原因是使用图像而不等待它的加载,但我有这个.onload的东西,对吗?
而且我也尝试过与this问题中相同的操作,但FF仍然只显示透明图像。请帮忙!
答案 0 :(得分:6)
Firefox中有一个错误:drawImage() fails silently when drawing an SVG image without @width or @height。
您可以通过向SVG添加(去图)width
和height
来修复它:
<svg
xmlns="http://www.w3.org/2000/svg"
width="119" height="119" viewBox="0 0 119 119">
…