HTML5 File API で開いた画像をキャンバスに描画したいと考えています。
handleFiles(e)
メソッドでは、 File にアクセスできますが、をe.target.files[0]
使用してその画像を直接描画することはできませんdrawImage
。HTML5 キャンバスでファイル API から画像を描画するにはどうすればよいですか?
これが私が使用したコードです:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<script>
window.onload = function() {
var input = document.getElementById('input');
input.addEventListener('change', handleFiles);
}
function handleFiles(e) {
var ctx = document.getElementById('canvas').getContext('2d');
ctx.drawImage(e.target.files[0], 20,20);
alert('the image is drawn');
}
</script>
</head>
<body>
<h1>Test</h1>
<input type="file" id="input"/>
<canvas width="400" height="300" id="canvas"/>
</body>
</html>