Uncaught TypeError: Cannot read property 'getContext' of null才接触js,网上搜了很久没找到有用的,为什么我这个图片加载不出来 <!DOCTYPE html>
<html dir="ltr">
<head>
<meta charset="utf-8">
<title>哈哈哈哈哈</title>
</head>
<body>
<canvas id="'myCanvas" width="200" height="400"style="border:1px solid #c3c3c3;">
</canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var img = new Image();
img.onload = function(){
context.drawImage(img,0,0);
};
img.src='uu.png';
</script>
</body>
</html>
2 回答
眼眸繁星
TA贡献1873条经验 获得超9个赞
你把img.src='uu.png';放到img.onload上面看下。如果还不可以检查下你的图片路径。
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var img = new Image();
img.src='uu.png';
img.onload = function(){
context.drawImage(img,0,0);
};
添加回答
举报
0/150
提交
取消