Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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 使用画布html5创建图像像素的2d数组_Javascript_Html_Canvas_Pixels - Fatal编程技术网

Javascript 使用画布html5创建图像像素的2d数组

Javascript 使用画布html5创建图像像素的2d数组,javascript,html,canvas,pixels,Javascript,Html,Canvas,Pixels,大家好,我试着把一个图像像素复制到一个矩阵上,这样我以后就可以使用它了。我想知道我是否正确使用了js中的矩阵,因为我是一个乞丐。谢谢 <canvas id="Canvas" > Navigator doesnt support canvas </canvas> <script type="text/javascript"> var img = new Image(); img.src = "jj.JPG"; //

大家好,我试着把一个图像像素复制到一个矩阵上,这样我以后就可以使用它了。我想知道我是否正确使用了js中的矩阵,因为我是一个乞丐。谢谢

<canvas id="Canvas" >  
     Navigator doesnt support canvas 
  </canvas>
      <script type="text/javascript">


  var img = new Image();

  img.src = "jj.JPG"; // Set source path

 img.onload = function() {
var matrix = [];
context.putImageData(idata, 0, 0);

for(var y = 0; y < canvas.height; y++) {

 matrix[y] = [];
for(var x = 0; x < canvas.width; x++){


var imgd = context.getImageData(x, y, canvas.width, canvas.height);
var pix = imgd.data;

var pos = (y * canvas.width + x) * 4; 
    matrix[y][pos]= pix[pos];;     //red      
    matrix[y][pos+1] =pix[pos+1];;    //bleu      
    matrix[y][pos+2]= pix[pos+2];;   //green       
    matrix[y][pos+3]= 255;          //alpha
}
  }
 </script>

 </body>
 </html>

导航器不支持画布
var img=新图像();
img.src=“jj.JPG”;//设置源路径
img.onload=函数(){
var矩阵=[];
putImageData(idata,0,0);
对于(变量y=0;y
用于(变量y=0;y
用于(变量y=0;y
用于(变量y=0;y
用于(变量y=0;y
在我看来,
var pos=(y*canvas.width+x)*4
来自另一段代码,它使用一维数组来表示图像数据。然而,你的矩阵是二维的。只需删除所有的[y]部分,就可以将矩阵展平。le_m我已经删除了所有的[y],这样正确吗?感谢您的帮助在我看来,
var pos=(y*canvas.width+x)*4
来自另一段代码,它使用一维数组来表示图像数据。然而,你的矩阵是二维的。只需删除所有的[y]部分,就可以将矩阵展平。le_m我已经删除了所有的[y],这样正确吗?感谢您帮助mematrix[y]=[]似乎不正确,您可能需要删除它。运行代码时是否遇到任何错误?使用console.log()或类似工具了解代码的功能,以及它是否符合您的预期。矩阵[y]=[]似乎不正确,您可能需要删除它。运行代码时是否遇到任何错误?使用console.log()或类似工具了解代码的作用以及它是否达到了预期效果。
for(var y = 0; y < canvas.height; y++) {//ligne
 matrix[y] = [];
for(var x = 0; x < canvas.width; x++){
//colonne

var imgd = context.getImageData(x, y, canvas.width, canvas.height);
var pix = imgd.data;


    var pos = (y * canvas.width + x) * 4; // position de pixel
    matrix[pos]= pix[pos];;           // some R value [0, 255]
    matrix[pos+1] =pix[pos+1];;           // some G value
    matrix[pos+2]= pix[pos+2];;           // some B value
    matrix[pos+3]= 255;           // set alpha channel
    }
 }
   return matrix;

}
for(var y = 0; y < canvas.height; y++) {//ligne

for(var x = 0; x < canvas.width; x++){
//colonne

var imgd = context.getImageData(x, y, canvas.width, canvas.height);

   var pix = imgd.data;


    var pos = (y * canvas.width + x) * 4; // position de pixel
    matrix[pos]= pix[pos];;           // some R value [0, 255]
    matrix[pos+1] =pix[pos+1];;           // some G value
    matrix[pos+2]= pix[pos+2];;           // some B value
    matrix[pos+3]= 255;           // set alpha channel
    }
  }
   return matrix;

 }