javascript - 从 Canvas 数组中提取像素

标签 javascript arrays html canvas html5-canvas

我目前正在开发一个项目,需要使用 Canvas 的像素。我使用 canvascontext.getImageData(0,0,width,height).data() 提取像素。这段代码运行良好并返回一个像素数组。在这个数组中,像素的位置如下:[r1,g1,b1,a1,r2,g2,b2,a2...]。现在我在 Java 中使用了类似的数组,但这里返回的像素如下所示: [r1,r2,g1,g2,b1,b2,a1,a2] 这使得可以使用掩码来获取值。 由于在 JS 中有所不同,我使用以下函数从数组中提取值并在编辑后设置它们:

 ImageClass.prototype.getRed = function(temp){
   return imageData.pixels[temp];
 }

 ImageClass.prototype.setRed = function(r, temp){
   imageData.pixels[temp] = r;
 }

 ImageClass.prototype.getGreen = function(temp){
   return imageData.pixels[Number(temp)+1];
 }

 ImageClass.prototype.setGreen = function(g, temp){
   imageData.pixels[Number(temp)+1] = g;
 }

 ImageClass.prototype.getBlue = function(temp){
   return imageData.pixels[Number(temp)+2];
 }

 ImageClass.prototype.setBlue = function(b, temp){
   imageData.pixels[Number(temp)+2] = b;
 }

 ImageClass.prototype.getAlpha = function(temp){
   return imageData.pixels[Number(temp)+3];
 }

 ImageClass.prototype.setAlpha = function(a, temp){
   imageData.pixels[Number(temp)+3] = a;
 }

temp 是一个整数值,表示索引。现在我的问题是:虽然以下函数(红色)有效,但下一个函数(绿色)不起作用。我不知道为什么以及如何开始调试。

ImageClass.prototype.red = function(){
  this.getPixels();
  for (index = 0; index < imageData.pixelsLength; index += 4) {
    var g = this.getGreen(index);
    var b = this.getBlue(index);

    g = 0;
    b = 0;

    this.setGreen(g, index);
    this.setBlue(b, index);

  }
  this.draw();
}

ImageClass.prototype.green = function(){
  this.getPixels();
  for (index = 0; index < imageData.pixelsLength; index += 4) {
    var r = this.getRed(index);
    var b = this.getBlue(index);

    r = 0;
    b = 0;

    this.setRed(r, index);
    this.setBlue(b, index); 
  }
  this.draw();
}

getPixels() 函数只是使像素数组全局可用(在命名空间中)。绘制函数的作用正如其名称所示。

如果有人知道从数组中提取像素的更简单方法,以便我可以访问所有红色、所有绿色等。我愿意接受建议。

提前致谢。

最佳答案

您可以删除对 getGreen 和 getBlue 的调用,因为您只是覆盖它们。

稍作修改

ImageClass.prototype.red = function(){
  this.getPixels();
  for (index = 0; index < imageData.pixelsLength; index += 4) {
    var g = 0, b = 0;

    this.setGreen(g, index);
    this.setBlue(b, index);

  }
  this.draw();
}

避免ImageClass.prototype.getRed

如果你想要一个非常简单的方法,你可以这样做:

offsets {r: 0, g: 1, b: 2, a: 3}

ImageClass.prototype.red = function(){
  this.getPixels();
  for (index = 0; index < imageData.pixelsLength; index += 4) {
      imageData.pixels[index+offsets.g] = 0;
      imageData.pixels[index+offsets.b] = 0;
  }
  this.draw();
}

简单而完整的替代方案

您可以通过生成这些函数来避免重复代码。

offsets {r: 0, g: 1, b: 2, a: 3}

function MakeColorFunction(omit1, omit2){
  return (function(){
    this.getPixels();
    for (index = 0; index < imageData.pixelsLength; index += 4) {
      imageData.pixels[index+offsets[omit1]] = 0;
      imageData.pixels[index+offsets[omit2]] = 0;
    }
    this.draw();
  });
}

ImageClass.prototype.red = MakeColorFunction("g", "b");
ImageClass.prototype.green = MakeColorFunction("b", "r");
ImageClass.prototype.blue = MakeColorFunction("r", "g");

关于javascript - 从 Canvas 数组中提取像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18076357/

相关文章:

c - 在 pthread_create 中使用函数指针数组 - 接近初始化

Javascript:链接运行功能不起作用

html - 媒体查询断点不准确

javascript - 如何清除div内的跨度

javascript - VueJS 中的计算属性

javascript - 事件不会在内容脚本 Chrome 扩展程序上触发

javascript - 循环数组并输出 HTML

javascript - ElementHandle 与 DOM 元素有何不同?

python - 将切片组合成多维切片以进行 numpy 数组切片

html - 表格中的 iframe - 无法覆盖