Skip to content

Instantly share code, notes, and snippets.

@jyhsu2000
Created May 7, 2020 08:14
Show Gist options
  • Save jyhsu2000/3fc20b5bada3426aa4315222f5feb749 to your computer and use it in GitHub Desktop.
Save jyhsu2000/3fc20b5bada3426aa4315222f5feb749 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Image drawing with canvas</title>
</head>
<body>
<input type="file" name="fileChooser" id="fileChooser" accept="image/jpeg"><br />
<canvas width="400" height="300" id="canvas" />
<script type="text/javascript">
var fileChooser = document.getElementById('fileChooser');
fileChooser.addEventListener('change', handleFiles);
function handleFiles(e) {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext('2d');
var img = new Image;
img.src = URL.createObjectURL(e.target.files[0]);
img.onload = function () {
canvas.width = this.width;
canvas.height = this.height;
ctx.drawImage(img, 0, 0);
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment