【问题标题】:Save canvas to disk in Node-webkit在 Node-webkit 中将画布保存到磁盘
【发布时间】:2014-12-24 18:35:25
【问题描述】:

在我的应用程序中,我有从磁盘加载一些图像的画布。现在修改画布后,我需要一种将画布保存到磁盘的方法。我的所有工作都在 Node-Webkit 平台上,所以它是一个桌面应用程序,我无法将canvas.dataToUrl 发送到服务器。我也知道一些像camanjs这样的库,它们有一些保存功能

var Caman = require('caman').Caman;

Caman("/path/to/file.png", function () {
 this.brightness(5);
 this.render(function () {
 this.save("/path/to/output.png");
 });
});

但我不知道如何使用它或将我的画布传递给这个函数。所以如何将当前画布保存到磁盘作为图像文件,如pngjpg in node-webkit

【问题讨论】:

    标签: javascript html node.js canvas node-webkit


    【解决方案1】:

    最后我可以解决我的问题并使用FileSaver将不同图像格式的画布保存到磁盘

      canvas.toBlob(function(blob) {
                saveAs(blob, "image.jpg");
            });
    

    【讨论】:

      【解决方案2】:

      在您的 html 页面上,而不是 node.js 端,包括 html2canvas.js

      然后:

      html2canvas($('div#main')).then(function(canvas) {
        var dataurl = canvas.toDataURL();
        dataurl = dataurl.replace(/^data:image\/(png|jpg|jpeg);base64,/, "");
        require("fs").writeFile("C:\\out.png", dataurl, 'base64', function(err) {
          console.log(err);
        });
      }).catch(function(e) {
        console.log(e);
      })
      

      不需要其他包。

      【讨论】:

        猜你喜欢
        • 2021-10-13
        • 2020-09-21
        • 2012-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-21
        • 2016-09-14
        • 2013-01-17
        相关资源
        最近更新 更多