Created
August 30, 2017 13:01
-
-
Save eberlitz/4b6cd607ed6dff3030250c48e4492aa4 to your computer and use it in GitHub Desktop.
Load pixels from image in the browser
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>Image</title> | |
</head> | |
<body> | |
<script> | |
function loadImage(canvas, src, x, y, width, height, opt_callback) { | |
var img = new window.Image(); | |
img.crossOrigin = '*'; | |
img.onload = function () { | |
var context = canvas.getContext('2d'); | |
canvas.width = width; | |
canvas.height = height; | |
context.drawImage(img, x, y, width, height); | |
opt_callback && opt_callback(); | |
img = null; | |
}; | |
img.src = src; | |
}; | |
var canvas = document.createElement('canvas'); | |
var width = 512; | |
var height = 512; | |
canvas.width = width; | |
canvas.height = height; | |
loadImage( | |
canvas, | |
'https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png', | |
0, 0, | |
width, height, | |
() => { | |
var context = canvas.getContext('2d'); | |
var imageData = context.getImageData(0, 0, width, height); | |
console.log(imageData); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment