[cocos2d-js] How to get pixel color of an image

What is the right way to determine pixel color of a sprite. Is it possible only through the RenderTexture as described here? I do not need to render my sprite thus wonder maybe there is a more efficient way.

I just wonder how can I get pixel color from an image that is not rendered?

For web platform, you can draw it onto an canvas then use getImageData to find the color for a pixel.
For JSB, I think you should use RenderTexture

Thanks for your reply, @pandamicro,

But what should I do if I do not need to render my image?

If I use something like this:

renderTexture.begin();
sprite.visit();
gl.readPixels(point.x, point.y, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixel);
cc.log("RGBA: "+pixel[0]+", "+pixel[1]+", "+pixel[2]+", "+pixel[3]);
renderTexture.end();

it gives me color of only visible area. But I do not want to render the sprite. I just want to get some info about its pixels.

P.S. in c++ version I used Image object and operate with its data. But here I can’t find something similar. How can I get access to sprite data?

So there is really no way of getting pixel info from just a sprite?

1 Like