开发者

how to get the RGB value of an image in a page using javascript?

I am trying to make a bookmark-let. But I'm stuck at a point. I need to extract the RGB or the color value of an image of very x pixel usi开发者_运维知识库ng the "For Next" statement. But i don't know what function can help me to extract the RGB value. Can someone please help me out with the function that tells the value of RCG of a given point in a picture?


This is a tough question. There's no method I'm aware of in the standard DOM that would allow you to extract the RGB value of pixels in an <img>.

However, if you're willing to dig in to HTML5, you can take advantage of the <canvas> element. You can load an image into the canvas (.drawImage()) and then get the RGB values you're looking for (.getImageData(); see the link in SLaks’ answer). Of course, this won't work on IE8 since it doesn't support <canvas>.

An alternative option might be to make an AJAX call to a web service to get pixel data. The server-side script can load the image, get RGB values, and return it as JSON to your bookmarklet. This obviously adds the latency of a roundtrip to a server, but is more compatible. (Also consider cross-domain issues, however.)


You can use a canvas.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜