GetPixel from HTML Canvas?

Can I request an HTML Canvas object to get color in a specific place?

+104
javascript html canvas
Mar 20 '09 at 17:00
source share
10 answers

There is a section on pixel manipulation in the W3C documentation.

Here is an example of how to invert an image :

// 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); 
+162
Mar 20 '09 at 17:06
source share

Have you tried the getImageData method?

 var data = context.getImageData(x, y, 1, 1).data; var rgb = [ data[0], data[1], data[2] ]; 
+51
Mar 20 '09 at 17:11
source share

Yes, of course, if you have its context. How to get a canvas context?

 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); } 
+10
Dec 30 '15 at 13:49
source share

Yup, check getImageData (). Here is an example of hacking captcha with JavaScript using canvas:

http://ejohn.org/blog/ocr-and-neural-nets-in-javascript/

+8
Mar 20 '09 at 17:07
source share
 function GetPixel(x, y) { var p = ctx.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); } 
+8
Apr 17 '15 at 13:44 on
source share

Note that getImageData returns a snapshot. Effects:

  • The changes will not take effect until the next putImageData li>
  • calls to getImageData and putImageData are relatively slow
+5
Dec 31 '10 at 7:51
source share
 // 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]; 
+4
Feb 06 '15 at 11:03
source share

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] }); } 
0
Jul 15 '18 at 10:36
source share

If you want to extract a specific pixel color by passing the coordinates of the pixel to a function, this is useful

 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.

 var color=detectColor(x,y) 

Color is an object, you will get the rgb value by color.r, color.g, color.b.

0
Oct 13 '18 at 18:38
source share

Convenient long reader pixel (draw here )

 let rp=((s='.myCanvas',c=document.querySelector(s),ctx=c.getContext('2d')) => (x,y)=>Object.values(ctx.getImageData(x, y, 1, 1).data))(); rp(10,20) // rp(x,y) returns: [r,g,b,a] with values 0-255 

 let rp=((s='.myCanvas',c=document.querySelector(s),ctx=c.getContext('2d')) => (x,y)=>Object.values(ctx.getImageData(x, y, 1, 1).data))(); let pp= ((s='.myCanvas',c=document.querySelector(s),ctx=c.getContext('2d'),id=ctx.createImageData(1,1)) => (x,y,r=0,g=0,b=0,a=255)=>(id.data.set([r,g,b,a]),ctx.putImageData(id, x, y),c))() // draw point pp(50,60, 198,236,247,250) // x,y, r,g,b,a // read color let c = rp(50,60); console.log(c); 
 <canvas class="myCanvas" width=100 height=100 style="background: black"></canvas> 

The first line is the initial part in which you can change the canvas selector s='.myCanvas' . This convenient oneliner is good for testing algorithms or for checking the concept, but for production code it is better to use other more understandable and readable code.

0
Feb 01 '19 at 11:35
source share



All Articles