关于一个HTML5CANVAS的问题!!!!!
<script>varcanvas;varcontext;window.onload=function(){canvas=document.getElementById(...
<script>
var canvas;
var context;
window.onload = function() {
canvas = document.getElementById("myCanvas");
context = canvas.getContext("2d");
}
context.rect(20,20,150,100);
context.stroke();
</script>
为什么上面这串代码不正确
但是下面这样又可以运行了??
<script>
var canvas;
var context;
canvas = document.getElementById("myCanvas");
context = canvas.getContext("2d");
context.rect(20,20,150,100);
context.stroke();
</script> 展开
var canvas;
var context;
window.onload = function() {
canvas = document.getElementById("myCanvas");
context = canvas.getContext("2d");
}
context.rect(20,20,150,100);
context.stroke();
</script>
为什么上面这串代码不正确
但是下面这样又可以运行了??
<script>
var canvas;
var context;
canvas = document.getElementById("myCanvas");
context = canvas.getContext("2d");
context.rect(20,20,150,100);
context.stroke();
</script> 展开
1个回答
推荐于2016-02-04
展开全部
window.onload = function() {//页面加载完成才执行的代码
//
}
上面的不行,是因为这2个对象没有初始化,就调用方法,报错,可以这样写
window.onload = function() {
canvas = document.getElementById("myCanvas");
context = canvas.getContext("2d");
context.rect(20,20,150,100);
context.stroke();
}
//
}
上面的不行,是因为这2个对象没有初始化,就调用方法,报错,可以这样写
window.onload = function() {
canvas = document.getElementById("myCanvas");
context = canvas.getContext("2d");
context.rect(20,20,150,100);
context.stroke();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询