代码之家  ›  专栏  ›  技术社区  ›  bafix2203

操作RGBA Javascript

  •  0
  • bafix2203  · 技术社区  · 7 年前

    在Javascript中,我有一个图像的Uint8Array()RGBA,这里是控制台。日志:

    enter image description here

    以下是HTML中rgba数组为字符串的图像:

    enter image description here

    有没有可能操纵这个数组,比如改变颜色? 谢谢你的帮助!

    1 回复  |  直到 7 年前
        1
  •  1
  •   nikitpad    7 年前

    下面是一个JS算法,用于将像素绘制到这种数组中:

    function changeColor(x, y, c)
    {
       colorArray[(x * 4) + (y * (imageWidth * 4))] = c.r;
       colorArray[(x * 4) + (y * (imageWidth * 4)) + 1] = c.g;
       colorArray[(x * 4) + (y * (imageWidth * 4)) + 2] = c.b;
       colorArray[(x * 4) + (y * (imageWidth * 4)) + 3] = c.a;
    }
    

    其中,x和y是要更改的像素的坐标,imageWidth是该数组生成的图像的宽度,c是要将像素更改为的颜色,colorArray是数组本身。