I'm trying to setup a canvas where the user can load a custom image. As far as I can tell everything should be set but for some reason it is not working.
HTML:
<canvas id="image-canvas" width="500" height="500" style="border:1px solid grey"></canvas>
<input type='file' id='fileInput' />
Javascript:
var canvas = document.getElementById('image-canvas');
ctx = canvas.getContext('2d');
// Trigger the imageLoader function when a file has been selected
var fileInput = document.getElementById('fileInput');
fileInput.addEventListener('change', imageLoader(), false);
function imageLoader() {
var reader = new FileReader();
reader.onload = function(event) {
img = new Image();
img.onload = function(){
ctx.drawImage(img,0,0);
}
img.src = reader.result;
}
reader.readAsDataURL(fileInput.files[0]);
}
You are calling imageLoader, not passing as a function to call.
Replace
fileInput.addEventListener('change', imageLoader(), false);
by
fileInput.addEventListener('change', imageLoader, false);
One line is wrong. Here is the fixed line:
fileInput.addEventListener('change', imageLoader, false);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.