[英]How can I get the pixel color value of an image in javascript?
HTML 规范中有一整节用于图像处理。 https://html.spec.whatwg.org/#pixel-manipulation
但具体来说,一种可能性是使用getImageData
function: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Pixel_manipulation_with_canvas#getting_the_pixel_data_for_a_context
这篇文章对我这种事情帮助很大: https://hacks.mozilla.org/2011/12/faster-canvas-pixel-manipulation-with-typed-arrays/
这是我的基本实现示例(我使用 Typescript 来简化它):
function GetColorPixel(
context: CanvasRenderingContext2D,
x: number,
y: number
): Array<number> {
const imageData: Uint8ClampedArray = context.getImageData(x, y, 1, 1).data
const rgbColor: Array<number> = [imageData[0], imageData[1], imageData[2]]
return rgbColor
}
在这个function中,需要传递canvas的上下文例如
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.