为什么我只画出了画布,没画出线啊!?反复对了几遍也没看出和老师写的有啥区别?求高人指点

来源:2-2 绘制直线、多边形和七巧板

白水向前冲

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>


写回答 关注

5回答

  • 酷宁
    2015-01-22 16:53:59
    为什么啥也没有


  • 酷宁
    2015-01-22 16:52:37
    <!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>


  • 白水向前冲
    2014-12-02 11:41:10

    终于找到原因了,原来是  var canvas = document.getElementById("canvas"); 出现了中文的括号。肉眼真的很难分辨啊

  • 白水向前冲
    2014-12-02 11:16:08

    <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>都没有生效,但是没找到原因,请高人指点

    HeyLuc...

    我的代码和你一样 也是显示不出来咋回事呢

    2015-06-21 20:18:46

    共 1 条回复 >

  • l1uyub0b0b0
    2014-12-01 17:50:05

    cantext和context

    白水向前冲

    谢谢,但是我改了以后还是没有画出直线

    2014-12-02 11:16:46

    共 1 条回复 >

炫丽的倒计时效果Canvas绘图与动画基础

学习HTML5中最激动人心的技术Canvas,彻底释放自己的创造力

96746 学习 · 1000 问题

查看课程

相似问题