Is it possible to query a HTML Canvas object to get the color at a specific location?
10 Answers
There's a section about pixel manipulation in the W3C documentation.
Here's an example on how to invert an image:
var context = document.getElementById('myCanvas').getContext('2d');
// Get the CanvasPixelArray from the given coordinates and dimensions.
var imgd = context.getImageData(x, y, width, height);
var pix = imgd.data;
// Loop over each pixel and invert the color.
for (var i = 0, n = pix.length; i < n; i += 4) {
pix[i ] = 255 - pix[i ]; // red
pix[i+1] = 255 - pix[i+1]; // green
pix[i+2] = 255 - pix[i+2]; // blue
// i+3 is alpha (the fourth element)
}
// Draw the ImageData at the given (x,y) coordinates.
context.putImageData(imgd, x, y);

- 1,754
- 3
- 19
- 34

- 124,188
- 49
- 220
- 267
-
Pixel manipulation section is now available here : https://www.w3.org/TR/2dcontext2/#pixel-manipulation – jtraulle Mar 13 '19 at 11:01
Try the getImageData
method:
var data = context.getImageData(x, y, 1, 1).data;
var rgb = [ data[0], data[1], data[2] ];

- 1,737
- 10
- 25

- 8,905
- 3
- 24
- 38
-
2shouldn't this be context.getImageData() and not canvas.getImageData()? – Crashalot Apr 05 '12 at 07:04
-
5@Crashalot depends on what the var "canvas" contains, it could simply be the context of a canvas with a crappy var name. – tbleckert May 03 '12 at 09:33
-
1Wow, very elegant! I thought about searching for the point in the entire context, but this is much smarter. – TheOne Jan 08 '13 at 21:24
-
5This is clever, but if you're going to be calling getPixel a lot, it is *much* faster to cache the ImageData object for the whole image (0,0,width,height), and then compute the index using `idx = (y * width + x) * 4` like Georg's answer. However, don't forget to refresh that cached object every time the image changes. – noio Oct 28 '13 at 12:30
-
That's true @Noio, however my answer is specific to the question above (i.e. the color of a pixel). – Theo.T Nov 20 '13 at 16:50
-
2What's that `Color()` constructor? That doesn't seem to exist anywhere – fregante Jun 30 '15 at 12:34
-
-
@SalientBrain indeed, but it's convenient if you only need the color at specific location (which was the question) – Theo.T May 11 '19 at 21:25
-
@Theo.T just wanted to point it out. Qwerty's approach is fast and convenient – SalientBrain May 15 '19 at 10:57
Yes sure, provided you have its context. (See how to get canvas context here.)
var imgData = context.getImageData(0,0,canvas.width,canvas.height)
// { data: [r,g,b,a,r,g,b,a,r,g,..], ... }
function getPixel(imgData, index) {
var i = index*4, d = imgData.data
return [d[i],d[i+1],d[i+2],d[i+3]] // Returns array [R,G,B,A]
}
// AND/OR
function getPixelXY(imgData, x, y) {
return getPixel(imgData, y*imgData.width+x)
}
PS: If you plan to mutate the data and draw them back on the canvas, you can use subarray
var
idt = imgData, // See previous code snippet
a = getPixel(idt, 188411), // Array(4) [0, 251, 0, 255]
b = idt.data.subarray(188411*4, 188411*4 + 4) // Uint8ClampedArray(4) [0, 251, 0, 255]
a[0] = 255 // Does nothing
getPixel(idt, 188411) // Array(4) [0, 251, 0, 255]
b[0] = 255 // Mutates the original imgData.data
getPixel(idt, 188411) // Array(4) [255, 251, 0, 255]
// Or use it in the function
function getPixel(imgData, index) {
var i = index*4, d = imgData.data
return imgData.data.subarray(i, i+4) // Returns subarray [R,G,B,A]
}
You can experiment with this on http://qry.me/xyscope/, the code for this is in the source, just copy/paste it in the console.

- 29,062
- 22
- 108
- 136
-
2Yay! Thanks it works very well and it is waay faster than calling `context.getImageData(x, y, 1, 1);` – adelriosantiago Aug 25 '16 at 03:59
-
-
That's ok if there is no credits for my (very) small contribution, but thanks for willing to. ^^ Your answer is already a pro move and deserved another upvote by who you know, so my joy is complete. :-) – Amessihel Mar 30 '21 at 16:23
-
Just as a side note, your answer helped me to find a way to write a flood fill of a closed area. Even if my code is still not good (really a _stack overflow_, too much recursion for a given amount of pixel to fill) your answer is a cobblestone of the way leading to my goal. So big thanks! – Amessihel Mar 30 '21 at 16:26
-
@Amessihel Nice, thanks for the heads-up and praise . Anyway, sometimes it is better to use `while` with a storage to avoid stack overflow with too much recursion. – Qwerty Mar 30 '21 at 16:39
-
@Qwerty, the algo I wrote is quite naive, it checks the current pixel color and fill it if it matches the color of the first hit pixel, then performs a call for each pixel in the immediate neighborhood (N,E,S,W). The recursion is deep and not trivial to convert to an iteration "as is". – Amessihel Mar 30 '21 at 16:44
-
@Amessihel I would love to see it, would you mind posting that to a gist? – Qwerty Mar 30 '21 at 16:52
-
@Qwerty, basically it is the same approach than the first sample of this SO answer: https://stackoverflow.com/a/56221940. – Amessihel Mar 30 '21 at 20:13
-
@Amessihel I hoped you create a gist so that I can comment better, but there is everything written already on that answer.. How to write it using a stack and even in a worker, wow! – Qwerty Mar 31 '21 at 01:37
-
@Qwerty yes, I noticed that. I remember also the behavior of command `FILL` provided with the Locomotive Basic of the Amstrad CPC which floods fill same-colour area, My goal is to get an iterative algorithm if possible. :) – Amessihel Apr 01 '21 at 22:32
-
function GetPixel(context, x, y)
{
var p = context.getImageData(x, y, 1, 1).data;
var hex = "#" + ("000000" + rgbToHex(p[0], p[1], p[2])).slice(-6);
return hex;
}
function rgbToHex(r, g, b) {
if (r > 255 || g > 255 || b > 255)
throw "Invalid color component";
return ((r << 16) | (g << 8) | b).toString(16);
}

- 1,028
- 8
- 20
Yup, check out getImageData()
. Here's an example of breaking CAPTCHA with JavaScript using canvas:

- 1,737
- 10
- 25

- 13,937
- 6
- 46
- 52
Note that getImageData
returns a snapshot. Implications are:
- Changes will not take effect until subsequent
putImageData
getImageData
andputImageData
calls are relatively slow

- 1,737
- 10
- 25

- 379
- 1
- 5
- 3
//Get pixel data
var imageData = context.getImageData(x, y, width, height);
//Color at (x,y) position
var color = [];
color['red'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 0];
color['green'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 1];
color['blue'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 2];
color['alpha'] = imageData.data[((y*(imageData.width*4)) + (x*4)) + 3];

- 1,737
- 10
- 25

- 7,099
- 8
- 53
- 55
You can use i << 2
.
const data = context.getImageData(x, y, width, height).data;
const pixels = [];
for (let i = 0, dx = 0; dx < data.length; i++, dx = i << 2) {
pixels.push({
r: data[dx ],
g: data[dx+1],
b: data[dx+2],
a: data[dx+3]
});
}

- 12,494
- 5
- 50
- 73
Fast and handy
Use following class which implement fast method described in this article and contains all you need: readPixel
, putPixel
, get width/height
. Class update canvas after calling refresh()
method. Example solve simple case of 2d wave equation
class Screen{
constructor(canvasSelector) {
this.canvas = document.querySelector(canvasSelector);
this.width = this.canvas.width;
this.height = this.canvas.height;
this.ctx = this.canvas.getContext('2d');
this.imageData = this.ctx.getImageData(0, 0, this.width, this.height);
this.buf = new ArrayBuffer(this.imageData.data.length);
this.buf8 = new Uint8ClampedArray(this.buf);
this.data = new Uint32Array(this.buf);
}
// r,g,b,a - red, gren, blue, alpha components in range 0-255
putPixel(x,y,r,g,b,a=255) {
this.data[y * this.width + x] = (a << 24) | (b << 16) | (g << 8) | r;
}
readPixel(x,y) {
let p= this.data[y * this.width + x]
return [p&0xff, p>>8&0xff, p>>16&0xff, p>>>24];
}
refresh() {
this.imageData.data.set(this.buf8);
this.ctx.putImageData(this.imageData, 0, 0);
}
}
// --------
// TEST
// --------
let s=new Screen('#canvas');
function draw() {
for (var y = 1; y < s.height-1; ++y) {
for (var x = 1; x < s.width-1; ++x) {
let a = [[1,0],[-1,0],[0,1],[0,-1]].reduce((a,[xp,yp])=>
a+= s.readPixel(x+xp,y+yp)[0]
,0);
let v=a/2-tmp[x][y];
tmp[x][y]=v<0 ? 0:v;
}
}
for (var y = 1; y < s.height-1; ++y) {
for (var x = 1; x < s.width-1; ++x) {
let v=tmp[x][y];
tmp[x][y]= s.readPixel(x,y)[0];
s.putPixel(x,y, v,v,v);
}
}
s.refresh();
window.requestAnimationFrame(draw)
}
// temporary 2d buffer ()for solving wave equation)
let tmp = [...Array(s.width)].map(x => Array(s.height).fill(0));
function move(e) { s.putPixel(e.x-10, e.y-10, 255,255,255);}
draw();
<canvas id="canvas" height="150" width="512" onmousemove="move(event)"></canvas>
<div>Move mouse on black box</div>

- 85,173
- 29
- 368
- 345
If you want to extract a particular color of pixel by passing the coordinates of pixel into the function, this will come in handy:
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
function detectColor(x, y){
data=ctx.getImageData(x, y, 1, 1).data;
col={
r:data[0],
g:data[1],
b:data[2]
};
return col;
}
x
, y
is the coordinate you want to filter out color.
var color = detectColor(x, y)
The color is the object, you will get the RGB value by color.r
, color.g
, color.b
.

- 30,738
- 21
- 105
- 131

- 471
- 6
- 15