从最新版本(从GitHub下载并编译),这就是你想要的版本:
canvas.toDataURL(left, top, width, height);
请查看问题fabric.js - 从canvas API的ImageData对象创建图像对象,仅针对kangax的评论。
var dataURL = myFabricCanvas.toDataURL();
var win=window.open();
win.document.write("<img src='"+myFabricCanvas.toDataURL()+"'/>");
试试这个
<button class="btn btn-success" id="rasterize">Convert Image To PNG</button>
//************************canvas to png image**********
document.getElementById('rasterize').onclick = function() {
if (!fabric.Canvas.supports('toDataURL')) {
alert('This browser doesn\'t provide means to serialize canvas to an image');
}
else {
window.open(canvas.toDataURL('png'));
var gh=(canvas.toDataURL('png'));+
alert(gh);
}
};
//--------------end canvas to png image-----------------------