Created
May 18, 2015 11:34
-
-
Save jawdatls/465d82f2158e1c4ce161 to your computer and use it in GitHub Desktop.
Get pixel color of Base64 PNG using javascript
This file contains 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
var image = new Image(); | |
image.onload = function() { | |
var canvas = document.createElement('canvas'); | |
canvas.width = image.width; | |
canvas.height = image.height; | |
var context = canvas.getContext('2d'); | |
context.drawImage(image, 0, 0); | |
var imageData = context.getImageData(0, 0, canvas.width, canvas.height); | |
// Now you can access pixel data from imageData.data. | |
// It's a one-dimensional array of RGBA values. | |
// Here's an example of how to get a pixel's color at (x,y) | |
var index = (y*imageData.width + x) * 4; | |
var red = imageData.data[index]; | |
var green = imageData.data[index + 1]; | |
var blue = imageData.data[index + 2]; | |
var alpha = imageData.data[index + 3]; | |
}; | |
image.src = base64EncodedImage; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!