liuganglove2018 发表于 2024-3-19 20:27

html5javascriptcanvas照着抄都找不出错在哪,求助

html:
<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
                #myCanvas{border:1px solid red;}
        </style>
</head>
<body>
        <canvas id="myCanvas" width="500" height="300"></canvas>
</body>
<script src="mycanvas.js">       
</script>
</html>
---------------------------------------------------------
js

var canvas=document.getElementById('myCanvas');
var pen=canvas.getContext('2d');

pen.lineWidth=1;
pen.strokeStyle="red";

var mousePress=false;
var last=null;

function pos(event){
        var ex,ey;
        ex=event.clientX;
        ey=event.clientY;
        return(x:ex,y:ey)
}

function start(event){
        mousePress=true;

}

function draw(event){
        if(!mousePress) return;
        var xy=pos(event);
        if(last != null){
                pen.beginPath();
                pen.moveTo(last.x,last.y);
                pen.lineto(xy.x,xy.y);
                pen.stroke();
        }
        last=xy;
}

function finish(event){
        mousePress=false;
        last=null;
}
canvas.onmousedown=start;
canvas.onmousemove=draw;
canvas.onmouseup=finish;


lengong 发表于 2024-3-19 20:46

function pos(event){
      var ex,ey;
      ex=event.clientX;
      ey=event.clientY;
      return(x:ex,y:ey)
}
改 return {x:ex,y:ey}

function draw(event){
      if(!mousePress) return;
      var xy=pos(event);
      if(last != null){
                pen.beginPath();
                pen.moveTo(last.x,last.y);
                pen.lineto(xy.x,xy.y);
                pen.stroke();
      }
      last=xy;
}

pen.lineto(xy.x,xy.y); 改成
pen.lineTo(xy.x,xy.y);

liuganglove2018 发表于 2024-3-19 21:02

lengong 发表于 2024-3-19 20:46
function pos(event){
      var ex,ey;
      ex=event.clientX;


大佬是怎么这么厉害的,人工看出来的吗,现在我怀疑自己是不是脑袋不好使,呜呜

lengong 发表于 2024-3-19 21:10

新手?没调试过页面?浏览器上有报错提示

steven026 发表于 2024-3-20 12:13


把代码随便丢进一个代码编辑器都能报错
这里应该是{x:ex,y:ey}
页: [1]
查看完整版本: html5javascriptcanvas照着抄都找不出错在哪,求助