I have put an image in canvas and I want to get the RGB value of the pixels of that image when the user moves the mouse over the image. Here is the code which I have written:
<canvas id="myCanvas" width="200" height="200" style="border: red;border-style: dotted">
Your browser does not support the canvas element.
</canvas>
<script>
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var destX = 0;
var destY = 0;
var imageObj = new Image();
imageObj.onload = function()
{
context.drawImage(imageObj, destX, destY);
};
imageObj.src = "zain.jpg";
canvas.onclick = function(e) {
var x = e.pageX;
var y = e.pageY;
var canvasColor = context.getImageData(x, y, 1,1); // rgba e [0,255]
var pixels = canvasColor.data;
var r = pixels[0];
var g = pixels[1];
var b = pixels[2];
document.body.style.backgroundColor = "rgb("+r+','+g+','+b+")";
}
I have put an image in canvas and I want to get the RGB value of the pixels of that image when the user moves the mouse over the image. Here is the code which I have written:
<canvas id="myCanvas" width="200" height="200" style="border: red;border-style: dotted">
Your browser does not support the canvas element.
</canvas>
<script>
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var destX = 0;
var destY = 0;
var imageObj = new Image();
imageObj.onload = function()
{
context.drawImage(imageObj, destX, destY);
};
imageObj.src = "zain.jpg";
canvas.onclick = function(e) {
var x = e.pageX;
var y = e.pageY;
var canvasColor = context.getImageData(x, y, 1,1); // rgba e [0,255]
var pixels = canvasColor.data;
var r = pixels[0];
var g = pixels[1];
var b = pixels[2];
document.body.style.backgroundColor = "rgb("+r+','+g+','+b+")";
}
Share
Improve this question
edited Jun 13, 2014 at 11:32
Matt Ellen
11.6k4 gold badges72 silver badges93 bronze badges
asked Sep 10, 2011 at 18:48
ZainZain
5,57111 gold badges36 silver badges35 bronze badges
6
- 1 Two quick ments about your current code. 1. Make sure getImageData w and h are set to 1 instead (getImageData(x, y, 1, 1). 2. Set CSS like this "rgb("+r+","+g+","+b+")"; . You'll need to format the output like this to match the spec. – Juho Vepsäläinen Commented Sep 11, 2011 at 7:09
- @bebraw i have updated the code, but still nothing happening, my code is not executing right after the getImageData() functional call. Can you please do me a favor, that you copy my code and just run it at your end. Shall be very thankful to you! – Zain Commented Sep 11, 2011 at 8:11
- Your code appears to work here. There's one important thing to keep in mind, though! You will need to run the script on a local server. Otherwise getImageData will give you SECURITY_ERR (inspect your console for this). – Juho Vepsäläinen Commented Sep 11, 2011 at 8:20
- i am running it on my local machine, but i dont know why my code is not executing right after the getImageData() call. :( – Zain Commented Sep 11, 2011 at 8:33
- yes you are right it is giving the Security error right after the getImageData() function call, can you suggest me, how to handle with it? – Zain Commented Sep 11, 2011 at 8:35
2 Answers
Reset to default 9Try something along this:
var color = document.getElementById("color");
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
var imageObj = new Image();
imageObj.onload = function(){
context.drawImage(imageObj, destX, destY);
};
imageObj.src = "zain.jpg";
canvas.onmousemove = function(e) {
// not so sure about these... might need to offset them or so
var x = e.x;
var y = e.y;
// set color now
var canvasColor = context.getImageData(x, y, 1, 1).data; // rgba e [0,255]
var r = canvasColor[0];
var g = canvasColor[1];
var b = canvasColor[2];
color.style.backgroundColor = 'rgb(' + r + ',' + g + ',' + b + ')';
}
Note that the snippet expects you have a div with id "color" somewhere. It sets the pixel color there.
What you're looking for here is the getImageData() call.
So, your solution would look something like this:
function getColor(canvas, x, y) {
var context = canvas.getContext("2d");
var pixel = context.getImageData(x, y, 1, 1);
// Red = rgb[0], green = rgb[1], blue = rgb[2]
// All colors are within range [0, 255]
var rgb = pixel.data;
return rgb;
}
function canvasMouseMove(e) {
var x = e.layerX, y = e.layerY;
var rgb = getColor(canvas, x, y);
var rgb_string = "rgb(" + rgb[0] + "," + rgb[1] + "," + rgb[2] + ")";
document.body.style.backgroundColor = rgb_string;
}
canvas.onmousemove = canvasMouseMove;
As @bebraw pointed out, you may need to handle the mouse location differently depending on the browser being used. For that, you might consider using jQuery or another JS library for simplicity.