Created
May 7, 2020 08:14
-
-
Save jyhsu2000/3fc20b5bada3426aa4315222f5feb749 to your computer and use it in GitHub Desktop.
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> | |
<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