canvg
接受SVG
数据文件路径或单行字符串
但在你的代码中,你正在传递 div 的 html 内容#ec
as
canvg('canvas', svg, {renderCallback: saveDaPicture, ignoreMouse: true, ignoreAnimation: true});
两个 jQuery 的$.html()
和 DOM 的innerHTML
方法按原样返回元素的 HTML 内容,因此很可能在多行中。
canvg
将此多行 html 内容解释为文件路径,
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="600" height="512">
并尝试从格式错误的 URL 中获取数据。
因此,SVG 到 Canvas 的转换过程失败,这就是您没有按预期获得图像的原因。
这是应该可以使用的更新版本,
function saveDaPicture(){
var img = document.getElementById('canvas').toDataURL("image/png");
$('body').append('<img src="'+img+'"/>');
}
$('#save').click(function(){
var svg = $('#ec').html().replace(/>\s+/g, ">").replace(/\s+</g, "<");
// strips off all spaces between tags
//alert(svg);
canvg('canvas', svg, {renderCallback: saveDaPicture, ignoreMouse: true, ignoreAnimation: true});
});