Created
May 30, 2016 08:54
-
-
Save woss/41bbf574c6d28f88923661a7964ac062 to your computer and use it in GitHub Desktop.
crates canvas with onclick event to get value of a clicked pixel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
</head> | |
<body> | |
<img style="display:none" src="image.png" alt=""> | |
<canvas id="canvas" onClick="selectPixel(this,event)"></canvas> | |
<script> | |
window.onload = function() { | |
var myImg = document.querySelector("img"); | |
var canvas = document.getElementById("canvas"); | |
var context = canvas.getContext("2d"); | |
context.drawImage(myImg, 0, 0); | |
} | |
var selectPixel = function ($this,event){ | |
console.log(event.offsetX,event.offsetY); | |
getImageData(event.offsetX,event.offsetY); | |
} | |
var getImageData = function (x,y) { | |
var canvas = document.getElementById("canvas"); | |
var context = canvas.getContext("2d"); | |
data = context.getImageData(x, y, 1, 1); | |
//returns rgba() | |
console.log(data); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment