Skip to content

Instantly share code, notes, and snippets.

@loraxx753
Last active December 31, 2018 18:00
Show Gist options
  • Save loraxx753/b43bd453488a05f3de5e74de85d16dd3 to your computer and use it in GitHub Desktop.
Save loraxx753/b43bd453488a05f3de5e74de85d16dd3 to your computer and use it in GitHub Desktop.
Grabs the color of an individual pixel in the image with the source '_assets/data/example.png'
(async () => {
const img = document.createElement('img')
img.src = "_assets/data/example.png"
img.onload = () => {
document.body.appendChild(img)
const canvas = document.createElement('canvas')
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
document.body.removeChild(img)
document.body.appendChild(canvas)
}
document.addEventListener('keypress', (e) => {
if(e.key.toLowerCase() == 'enter') {
let [x, y] = Array(2).fill(0)
var [red, blue, green, alpha] = document.querySelector('canvas').getContext('2d').getImageData(x, y, 1, 1).data;
console.log({red, green, blue, alpha})
}
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment