简体   繁体   English

像素操作和画布

[英]Pixel manipulation and canvas

Is there a way in javascript to change the alpha channels of each pixel into being fully transparent (a=0) while coding for pixel manipulation (meaning that you can still change the transparency in some of the alpha channels as desired)? 有没有一种方法在javascript中将每个像素的alpha通道更改为完全透明(a = 0),同时编码像素操作(意味着你仍然可以根据需要改变某些alpha通道的透明度)?

Basically, what I'm doing is: given some data for a specific image, I manipulate the pixel array using an algorithm so that some pixels become fully transparent unless they satisfy some certain condition. 基本上,我正在做的是:给定特定图像的一些数据,我使用算法操纵像素阵列,使得一些像素变得完全透明,除非它们满足某些条件。 In the case of them satisfying the condition I want them to be fully opaque, aka alpha=1. 在满足条件的情况下,我希望它们完全不透明,即alpha = 1。 However, because of a complication with the way the algorithm works, I need to have my data "reset"; 但是,由于算法运行方式的复杂化,我需要将数据“重置”; meaning I want the pixel array to start off as having every alpha = 0. I can provide code if that helps in better understanding the scope of my question. 意思是我希望像素数组从每个alpha = 0开始。我可以提供代码,如果这有助于更好地理解我的问题的范围。

Thanks so much. 非常感谢。

EDIT: I'm looking more for a method/one-line code. 编辑:我正在寻找一个方法/单行代码。 Would context.globalAlpha = 0 serve the purposes? context.globalAlpha = 0可以达到目的吗? Is there any pitfall I should be careful about? 有什么陷阱我应该小心吗?

EDIT2: This is my code. EDIT2:这是我的代码。 Does globalAlpha where I've put it do what I'm expecting it to do? globalAlpha我把它放在我期待它做的事情上吗? I'm not sure how to use it... 我不确定如何使用它...

function getBoundary(imagedata){ var imageData = new Array(imagedata.data.length); imageData = imagedata.data; var w = imagedata.width; var h = imagedata.height;
var color1 = []; var colorRight = []; var colorDown = []; context.globalAlpha = 0; for (var i = 0; i < 4*w*h; i +=4) { color1 = [imageData[i],imageData[i+1],imageData[i+2]];
colorRight = [imageData[i+4],imageData[i+5],imageData[i+6]]; colorDown = [imageData[4*w+i],imageData[4*w+i+1],imageData[4*w+i+2]];
if(colorRight = [255,255,255]){ //if right is white if(color1 = [0,0,0]){ imageData[i+3] = 255; } else{ if(colorDown = [0,0,0]){ imageData[4*w+i+3] = 255; } } } else{ //colorRight = black if(color1 = [0,0,0]){ if(colorDown = [255,255,255]){ imageData[i+3] = 255; } } else if(color1 = [255,255,255]){ imageData[i+7] = 255; if(colorDown = [0,0,0]){ imageData[4*w+i+3] = 255; } else{ } } } } console.log("done"); imagedata.data = imageData; return imagedata; }

You can use getImageData and flip all the alpha elements to zero: 您可以使用getImageData并将所有alpha元素翻转为零:

You can create a function that zeros the alpha of all pixels on the canvas like this: 您可以创建一个将画布上所有像素的alpha归零的函数,如下所示:

function zeroAllAlpha(){

    var imageData=context.getImageData(0,0,canvas.width,canvas.height);

    var data=imageData.data;

    // set all alpha elements to zero (fully transparent);

    for(var i=3;i<data.length;i+=4){

        data[i]=0;

    }

    context.putImageData(imagedata,0,0);

}

And you can call the function with one line like this: 你可以用这样的一行来调用函数:

zeroAllAlpha();

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

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