透明度的颜色变化 [英] a color change in transparency

查看:145
本文介绍了透明度的颜色变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript HTML5画布中生成了一个图像。
我现在想说某种颜色的所有px(例如红色)都变得透明

I have an image generated in the javascript HTML5 canvas. I would now like to say that all the px of a certain color (red for example) have all become transparent

推荐答案

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);

我没有测试代码但它应该有效(如果没有,你有全局的想法)

I did not test the code but it should work (you have the global idea if it does not)

这篇关于透明度的颜色变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆