问答详情
源自:2-1 回顾——从线条开始

求大神看看代码哪里错了 一直画不出线条

<!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>

提问者:嗜甜怪 2016-11-15 23:46

个回答

  • 草莓summer
    2016-11-18 14:07:31
    已采纳

    <!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>

  • 草莓summer
    2016-11-16 00:18:05

    <canvas id="canvas" style="border:1px solid #aaaaaa;display:block;margin:50px auto;">
    </canvas>
    你的id错了下面的js找不到id=canvas,所以画不出来