白水向前冲
2014-12-01 16:44
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Canvas 特效</title>
</head>
<body>
<canvas id="canvas" width="1024" height="768" style="border:1px solid #aaa; display:block; margin:50px auto;" > </canvas>
<script>
window.onload = function(){
var canvas = document.getElementById("canvas");
var cantext = canvas.getContext("2d")
context.moveTo(100,100)
context.lineTo(700,700)
context.stroke()
}
</script>
</body>
</html>
为什么啥也没有
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>1</title> </head> <body> <canvas id="canvas" style="border:1px sliod #aaa;display: block;margin: 50px auto;"> 请更换浏览器后再试~~~ </canvas> <script> var tangram=[ {p:[{x:0,y:0},{x:800,y:0},{x:400,y:400}],color:"#caff87"}, {p:[{x:0,y:0},{x:400,y:400},{x:0,y:800}],color:"#ffffff"}, {p:[{x:800,y:0},{x:800,y:400},{x:600,y:600},{x:600,y:200}],color:"#ef3d61"}, {p:[{x:600,y:200},{x:600,y:600},{x:400,y:400}],color:"#f9f51a"}, {p:[{x:400,y:400},{x:600,y:600},{x:400,y:800},{x:200,y:600}],color:"#a594c0"}, {p:[{x:200,y:600},{x:400,y:800},{x:0,y:800}],color:"#fa8ecc"}, {p:[{x:800,y:400},{x:800,y:800},{x:400,y:800}],color:"#f6ca29"} ] window.onload=function(){ var canvas=document.getElementById("canvas"); canvas.width=800; canvas.height=800; var context=canvas.getContext("2d"); for(var i=0;i<tangram.length;i++) draw(tangram[i],context) } function draw(piece,cxt){ cxt.beginPath(); cxt,moveTo(piece.p[0].x,piece.p[0].y); for(var i=0;i<piece.length;i++) cxt.lineTo(piece.p[i].x,piece.p[i].y); cxt.closePath(); cxt.fillStyle=piece.color; cxt.fill(); cxt.strokeStyle="black" cxt.lineWidth=3 cxt.stroke(); } </script> </body> </html>
终于找到原因了,原来是 var canvas = document.getElementById("canvas"); 出现了中文的括号。肉眼真的很难分辨啊
<html>
<head>
<meta charset="utf-8">
<title>Canvas 特效</title>
</head>
<body>
<canvas id="canvas" width="1024" height="768" style="border:1px solid #aaa; display:block; margin:50px auto;" > </canvas>
<script>
window.onload = function(){
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.moveTo(100,100)
context.lineTo(700,700)
context.stroke()
}
</script>
</body>
</html>
修改了 context,但是还是画不出来,并且我发现把宽度和高度代码放到<script>里面后没有生效,感觉现在整个<script>都没有生效,但是没找到原因,请高人指点
cantext和context
炫丽的倒计时效果Canvas绘图与动画基础
96746 学习 · 1000 问题
相似问题