ImageData用于检索指定颜色的x,y位置的对象

时间:2012-10-03 04:26:00

标签: html5 canvas

是否可以使用ImageData对象在html5画布上检索“黑色”的x,y像素位置?我对画布相当新,我很难搞清楚这是否可行或如何实现它。

1 个答案:

答案 0 :(得分:3)

确实你可以做到。

您将需要getImageData画布上下文并在4s块中循环显示它,这些块代表RGBA通道,然后分别比较每个通道。

多个像素的ImageData有点棘手。 成像var imgData = ctx.getImageData(0, 0, width, height);

现在imgData.data是一个大数组,其格式如下:

imgData.data[0] // is the Red channel of the first pixel
imgData.data[1] // is the Green channel of the first pixel
imgData.data[2] // is the Blue channel of the first pixel
imgData.data[3] // is the Alpha (transparency) channel of the first pixel

imgData.data[4] // is the Red channel of the second pixel
... etc ...

检查演示中您提出的问题http://jsfiddle.net/GXrd5/