Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用webos将html5画布元素保存到文件_Javascript_Html_Node.js_Webos - Fatal编程技术网

Javascript 使用webos将html5画布元素保存到文件

Javascript 使用webos将html5画布元素保存到文件,javascript,html,node.js,webos,Javascript,Html,Node.js,Webos,这里使用开源Png生成代码提取img变量。 这是canvas.toDataURL()的替代方法;webOS不支持toDataURL,所以我不得不使用这个库 在这里,我使用了这个库,并在画布图像数据像素阵列上进行了操作 EditorAssistant.prototype.getDataURL = function(width,height,data){ var p = new PNGlib(height, width, 256); // construcor takes hei

这里使用开源Png生成代码提取img变量。
这是canvas.toDataURL()的替代方法;webOS不支持toDataURL,所以我不得不使用这个库

在这里,我使用了这个库,并在画布图像数据像素阵列上进行了操作

  EditorAssistant.prototype.getDataURL = function(width,height,data){   
     var p = new PNGlib(height, width, 256); // construcor takes height, weight and color-depth
 var background = p.color(0, 0, 0, 0); // set the background transparent

    for (var i = 0, n = data.length; i < n; i += 4) {
        var x = i * 10;
        var y = Math.sin(i) * Math.sin(i) * 50 + 50;
        // use a color triad of Microsofts million dollar color
          p.buffer[p.index(Math.floor(x), Math.floor(y))] = p.color(data[i], data[i+1], data[i+2]);
    }


return 'data:image/png;base64,'+p.getBase64() ;
}

提前感谢

数据
是一个以
数据开头的字符串:image/png;base64,
剩下的是base64中的数据

  • 删除
    数据:图像/png;base64,
    来自
    数据
  • 将其从base64转换为二进制
  • 将二进制缓冲区保存到文件
  • 代码


    我以前没有尝试在webOS中写入文件,但您确定您有权限在尝试写入文件的地方写入文件吗?您可能没有收到错误,但当您尝试在不应该写的地方写东西时,webOS可能会悄无声息地失败,我不知道。好了,现在它正在工作,我刚刚将路径更改为/media/internal/wallpers/。。。它创建了一个png,但它是空的。。现在的问题是。。。如何将画布数据加载到图像文件是webOS?这与正确的编码有关吗?png图像应该如何编码?thnks mak…我现在至少可以保存图像上面的代码在模拟器中运行良好,但不适用于palm设备
      var fs = IMPORTS.require('fs');
    var path = IMPORTS.require('path');
    
    
    var buff = new Buffer(data,'binary').toString('base64'); 
    
     path.exists('images/', function(exists     ){
     if (exists) {
    
         fs.open('images/icon.png', 'w', 666, function( e, id ) {
    
              fs.write( id,  buff, null, 'binary', function(err,written){
                if(err)
                    callback({
                        error: false,
                        reply: err
                    });
                if(written){
                        callback({
                        error: false,
                        reply: buff.toString()
                    });
                }   
                fs.close(id, function(){
                    callback({
                        error: false,
                        reply: 'closed'
                    });
                });
              });
            });
    
        }
        else {
            callback({
                error: true,
                reply: 'File did not exist.'
            });
        }
           }
       })
    
    var buff = new Buffer(data.substr('data:image/png;base64,'.length), 'base64');
    ...
    fs.write(id, buff, 0, buff.length, 0, function(...