jquery将页面内的svg转换为png图片并下载
2016/05/23    标签: svg   
var $svg = $('svg');
$svg.each(function(){
    var $svgXml=$(blog_post).prop('outerHTML')
    var image = new Image();
    image.src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(svgXml))); //给图片对象写入base64编码的svg流
    var canvas = document.createElement('canvas');  //准备空画布
    canvas.width = parseInt($('svg').width());
    canvas.height = parseInt($('svg').height());
    var context = canvas.getContext('2d');  //取得画布的2d绘图上下文
    context.drawImage(image, 0, 0);
    var a = document.createElement('a');
    a.href = canvas.toDataURL('image/png');  //将画布内的信息导出为png图片数据
    a.download = "MapByMathArtSys";  //设定下载名称
    a.click(); //点击触发下载
});

复制上面的代码,放在console文件里,回车执行