简体   繁体   English

无法在Edge浏览器上将画布另存为图像

[英]Can't save canvas as image on Edge browser

I am using fabric js for my web application. 我正在为我的Web应用程序使用Fabric js。 For saving HTML5 canvas as image using fabric js, I found the following code which works in chrome and firefox browser. 为了使用Fabric JS将HTML5画布另存为图像,我发现以下代码可在chrome和firefox浏览器中使用。 But when i run th same code in microsoft edge, the browser just opens a blank new window without any image on it. 但是,当我在Microsoft Edge中运行相同的代码时,浏览器只会打开一个空白的新窗口,上面没有任何图像。

 $(".save").click(function () {
    canvas.deactivateAll().renderAll();
    window.open(canvas.toDataURL('png'));
});

However I also tested many fiddle projects which shows how to convert canvas to an image. 但是,我还测试了许多小提琴项目,这些项目显示了如何将画布转换为图像。 Those fiddles works on chrome but not in edge. 这些小提琴在镀铬上起作用,但在边缘上不起作用。 An example fiddle is here 这里有一个小提琴例子

If you will use FileSaver.js it will download on Edge. 如果您将使用FileSaver.js,它将在Edge上下载。 For using FileSaver.js you need to convert base64 data into the blob data. 为了使用FileSaver.js,您需要将base64数据转换为Blob数据。 To do that, please check this post on StackOverflow 为此,请在StackOverflow上查看此信息

Here is an updated fiddle 这是更新的小提琴

You need to include FileSaver.js into your project, and your save button will have the following code: 您需要将FileSaver.js包含到您的项目中,并且保存按钮将包含以下代码:

$("#canvas2png").click(function(){
    canvas.isDrawingMode = false;

    if(!window.localStorage){alert("This function is not supported by your browser."); return;}

    var blob = new Blob([b64toBlob(canvas.toDataURL('png').replace(/^data:image\/(png|jpg);base64,/, ""),"image/png")], {type: "image/png"});
      saveAs(blob, "testfile1.png");
});

Alternative, quick and dirt solution is to write html data into new tab, and right click on the image and save it. 另一种快速,简便的解决方案是将html数据写入新标签,然后右键单击图片并保存。 This solution is not required any plugins or libraries. 此解决方案不需要任何插件或库。

Your save will simply change to: 您的保存将简单地更改为:

  $("#canvas2png").click(function(){
    canvas.isDrawingMode = false;

    if(!window.localStorage){alert("This function is not supported by your browser."); return;}

       var html="<img src='"+canvas.toDataURL()+"' alt='canvas image'/>";
        var newTab=window.open();
        newTab.document.write(html);
});

Check the modified fiddle: 检查修改过的小提琴:

http://jsfiddle.net/9hrcp/164/ http://jsfiddle.net/9hrcp/164/

document.getElementById('test').src = canvas.toDataURL('image/png');

i added an image tag and and set the src attribute to the dataUrl to the image and it loads fine. 我添加了一个图像标签,并将src属性设置为图像的dataUrl,并且加载良好。

So edge is generating a correct png, is refusing to load it as a dataUrl redirect. 因此edge正在生成正确的png,拒绝将其作为dataUrl重定向加载。 May be a feature and not a bug. 可能是功能而非错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM