JavaScript How to get the color of a pixel from image? color pixel from,pixel color from image,color pixel in

I want to get the color of a pixel from image with using pure JavaScript.

I wrote this script, but it did not work:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Get Pixel</title>

<script type='text/javascript'>
window.onload = function() {
var canvas = document.createElement("canvas");

var pic = new Image(); 
pic.src = 'http://i.imgur.com/hvGAPwJ.png'; 
pic.onload = function() {

canvas.width = pic.width;
canvas.height = pic.height;
var ctx = canvas.getContext("2d");

ctx.drawImage(pic, 0, 0);}

var c = canvas.getContext('2d');
var p = c.getImageData(7, 7, 1, 1).data;
var hex = "RGB = " + p[0]+", "+p[1]+", "+p[2];

document.getElementById("output").innerHTML = hex; 
}
</script>

</head>
<body>
<div id="output"></div>
</body>
</html>

How to change the code, what would he worked correctly?

For example for the picture "http://i.imgur.com/hvGAPwJ.png", the result should be RGB = 255, 255, 255.

Answer:1

If you indent your script properly, the error becomes more apparent:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Get Pixel</title>

<script type='text/javascript'>
window.onload = function() {
    var canvas = document.createElement("canvas");

    var pic = new Image(); 
    pic.src = 'http://i.imgur.com/hvGAPwJ.png'; 
    pic.onload = function() {

        canvas.width = pic.width;
        canvas.height = pic.height;
        var ctx = canvas.getContext("2d");

        ctx.drawImage(pic, 0, 0);
    }

    var c = canvas.getContext('2d');
    var p = c.getImageData(7, 7, 1, 1).data;
    var hex = "RGB = " + p[0]+", "+p[1]+", "+p[2];

    document.getElementById("output").innerHTML = hex; 
}
</script>

</head>
<body>
<div id="output"></div>
</body>
</html>

You are setting a function on the image's load event, but not waiting for it to happen before you update the contents of the div.

You should move everything inside the pic.onload function, apart from setting the pic.src:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Get Pixel</title>

<script type='text/javascript'>
window.onload = function() {
    var canvas = document.createElement("canvas");

    var pic = new Image(); 
    pic.onload = function() {

        canvas.width = pic.width;
        canvas.height = pic.height;
        var ctx = canvas.getContext("2d");

        ctx.drawImage(pic, 0, 0);

        var c = canvas.getContext('2d');
        var p = c.getImageData(7, 7, 1, 1).data;
        var hex = "RGB = " + p[0]+", "+p[1]+", "+p[2];

        document.getElementById("output").innerHTML = hex; 
    }
    pic.src = 'http://i.imgur.com/hvGAPwJ.png'; 
}
</script>

</head>
<body>
<div id="output"></div>
</body>
</html>

Now the problem is just cross origin restrictions.

Answer:2

Your code is almost (see below) correct in its self BUT unfortunately when you use images from other origins than the page itself certain criteria must be there for it to work due to CORS (security feature).

You cannot use images from other origins out of the box. The server they are on need to have accept-* header set to allow this.

If not your getImageData and toDataURL will be blank (throws a security error that you can see in the console).

If you don't have access to modify the server the only way to get around this is to use your own server as a image proxy (http://myServer/getImage.cgi|php|aspx...?http/otherServer/img)

As pointed on in comments always set src after onload so you are sure onload gets initialized.

var pic = new Image(); 
pic.onload = function() { /* funky stuff here */ };
pic.src = 'http://i.imgur.com/hvGAPwJ.png'; //Last
Answer:3

www.bchomescondos.ca/properties/?city=Burnaby&id=261618646 www.bchomescondos.ca/properties/?city=Burnaby&id=261711367 These are just the few of the sample pages where the problem is arising. ...

www.bchomescondos.ca/properties/?city=Burnaby&id=261618646 www.bchomescondos.ca/properties/?city=Burnaby&id=261711367 These are just the few of the sample pages where the problem is arising. ...

  1. google street view car
  2. google street view map
  3. google street view search
  4. google street view instant
  5. google street view funny
  6. google street view game
  7. google street view online
  8. google street view jobs
  9. google street view history
  10. google street view camera
  11. google street view locations
  12. google street view api
  13. google street view schedule
  14. google street view mobile
  15. google street view android
  16. google street view ready
  17. google street view not working
  18. google street view vr
  19. google street view iphone
  20. google street view germany

so I am trying to get the validate from my model to actually disable the save button but to re-validate when new input is included. Anyone know the best way to attempt this. Thanks! The problem I have ...

so I am trying to get the validate from my model to actually disable the save button but to re-validate when new input is included. Anyone know the best way to attempt this. Thanks! The problem I have ...

  1. backbone model isvalid
  2. backbone.js model isvalid

Guys actually i want to write this code shortly, instead of rewriting the same code for all my 4 elements individually i would like to write a function that will be called for each element and execute....

Guys actually i want to write this code shortly, instead of rewriting the same code for all my 4 elements individually i would like to write a function that will be called for each element and execute....

  1. write function jquery
  2. write jquery function in js file
  3. write jquery function in html
  4. write jquery function in typescript
  5. write jquery function with parameters
  6. write jquery function and call
  7. write jquery function in angularjs controller
  8. write jquery function inside php
  9. creating function jquery
  10. write function in jquery w3schools
  11. write function using jquery
  12. write a jquery function on page load
  13. write on click function jquery
  14. write simple jquery function
  15. how to write jquery function in external js file
  16. how to write jquery function in jsp page
  17. write onclick function in jquery
  18. how to write jquery function in angular 6
  19. how to write jquery function in react js
  20. how to write jquery function in javascript

I just found that I can use $('form').prop(name) to get input with that name within the form. Then I experimented on other tags, and this won't work for div and body. Now I don't have a way to tell if ...

I just found that I can use $('form').prop(name) to get input with that name within the form. Then I experimented on other tags, and this won't work for div and body. Now I don't have a way to tell if ...

  1. form form method property