将SVG内容下载为PNG文件,以下代码在Chrome中工作正常,但在IE浏览器中无效。任何建议?

时间:2016-02-23 11:03:01

标签: javascript

将SVG内容下载为PNG文件,以下代码在Chrome中工作正常,但在IE浏览器中无法正常工作。有什么建议吗?

var svg = document.querySelector( "svg" );
var svgData = new XMLSerializer().serializeToString( svg );
var canvas = document.createElement( "canvas" );

var svgSize = svg.getBoundingClientRect();
canvas.width = svgSize.width;
canvas.height = svgSize.height;
var ctx = canvas.getContext( "2d" );
var img = document.createElement( "img" );
img.setAttribute( "src", "data:image/svg+xml;base64," + btoa( svgData ) );


img.onload = function() {
    ctx.drawImage( img, 0, 0 );

    setTimeout(function(){
        var a = document.createElement("a");
        a.download = "networkmap.png";
        a.href = canvas.toDataURL("image/png");console.log(a.href);
        a.click();
     }, 500);       
};

1 个答案:

答案 0 :(得分:0)

对于Canvas / PNG元素,

data:image/svg+xml;似乎很可疑,data:image/png是PNG的正确Mime类型。