嗜甜怪
2016-11-15 23:46
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="canvas-warp" style="border:1px solid #aaaaaa;display:block;margin:50px auto;">
</canvas>
</div>
<script>
window.onload=function(){
var canvas=document.getElementById("canvas");
canvas.width=800;
canvas.height=800;
var context=canvas.getContext("2d");
context.moveTo(100,100);
context.lineTo(300,300);
context.lineWidth=5;
context.strokeStyle="#AA394C";
context.stroke();
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
//你的错误在于canvas标签,canvas不是一个div,它是html5的一个特定的标签,不能用<div id="canvas">这样来创建一个canvas
<canvas id="canvas" style="border:1px solid #aaaaaa;display:block;margin:50px auto;">
</canvas>
<script>
window.onload=function(){
var canvas=document.getElementById("canvas");
canvas.width=800;
canvas.height=800;
var context=canvas.getContext("2d");
context.moveTo(100,100);
context.lineTo(300,300);
context.lineWidth=5;
context.strokeStyle="#AA394C";
context.stroke();
}
</script>
</body>
</html>
<canvas id="canvas" style="border:1px solid #aaaaaa;display:block;margin:50px auto;">
</canvas>
你的id错了下面的js找不到id=canvas,所以画不出来
Canvas绘图详解
72881 学习 · 422 问题
相似问题
回答 1
回答 1