我想将图像上传到画布,然后使用node webkit运行程序,所以这是我的代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Slicer</title>
<script>
var imageLoader = document.getElementById('imageLoader');
imageLoader.addEventListener('change', handleImage, false);
var canvas = document.getElementById('imageCanvas');
var ctx = canvas.getContext('2d');
function handleImage(e){
var reader = new FileReader();
reader.onload = function(event){
var img = new Image();
img.onload = function(){
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img,0,0);
}
img.src = event.target.result;
}
reader.readAsDataURL(e.target.files[0]);
}
</script>
</head>
<body>
<h1>Image Slicer</h1>
<label>Image File:</label><br/>
<input type="file" id="imageLoader" name="imageLoader"/>
<canvas id="imageCanvas"></canvas>
</body>
</html>
但运行节点webkit时出现此错误:
Uncaught node.js Error
TypeError: Cannot read property 'addEventListener' of null
at file:///F:/Project/Slicer/html/index.html:8:16
**更新**
我忘了等待加载事件
window.addEventListerner('load', app, false);