Skip to content

Instantly share code, notes, and snippets.

@frantic1048
Last active December 7, 2015 12:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frantic1048/c031892a994e749f1e6c to your computer and use it in GitHub Desktop.
Save frantic1048/c031892a994e749f1e6c to your computer and use it in GitHub Desktop.
get ImageData(pixel information) directly from HTML Input Element
/* use on <input> like this, multiple is optional.
* <input
* type="file"
* multiple
* accept="image/*"
* onchange={handleFile}
* />
*/
function handleFile() {
function extractDataAndDoSomething(f) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
const fr = new FileReader();
img.onload = () => {
let _imageData;
ctx.drawImage(img, 0, 0);
_imageData = ctx.getImageData(0, 0, img.width, img.height);
// do anyting you want here with parsed data
doSometingWith(_imageData);
};
fr.onload = () => { img.src = fr.result; };
fr.readAsDataURL(f);
}
for (const eachFile of new Array(...this.refs.fileInput.files)) {
extractDataAndDoSomething(eachFile);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment