2012-05-16 244 views
0

我在JavaScript HTML5画布中生成了一个图像。 我现在想说的是一个特定的颜色(红色为例)的所有像素都变成透明的透明度中的颜色变化

+0

你有什么试过?这是一个非常简单的任务:循环遍历像素数组,并更改Alpha通道。请参阅https://developer.mozilla.org/En/HTML/Canvas/Pixel_manipulation_with_canvas –

回答

1
var imgd = context.getImageData(0,0, canvas.widht, canvas.height); 
var pix = imgd.data; 

// Loop over each pixel and set alpha channel to 255 for every red pixel 
for (var i = 0; n = pix.length, i < n; i += 4) { 
    if (pix[i ] > 240 && pix[i+1 ] < 15 && pix[i+2] < 15) // it is kind of red 
     pix[i+3] = 255; // alpha channel 
} 

// Draw the ImageData object at the given (x,y) coordinates. 
context.putImageData(imgd, 0,0); 

我没有测试的代码,但它应该工作(你有全局观念,如果它不)

+0

peeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeerfect! –

+1

将其标记为您的答案 – jazzytomato