1、增加鼠标移动事件

$('#canvas').mousemove(function (e) {
            draw(event);
        });

 

2、获取鼠标在canvas上的坐标

 function getCanvasPos(canvas, e) {//获取鼠标在canvas上的坐标
        var rect = canvas.getBoundingClientRect();
        return {
            x: e.clientX - rect.left * (canvas.width / rect.width),
            y: e.clientY - rect.top * (canvas.height / rect.height)
        };
    }

3、获取鼠标在整个页面上的坐标

    function mousePos(e) {//获取鼠标所在位置的坐标,相对于整个页面
        var x, y;
        var e = e || window.event;
        return {
            x: e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
            y: e.clientY + document.body.scrollTop + document.documentElement.scrollTop
        };
    }

 

 

4、画圆

  function draw(e) {
        var c = document.getElementById("can_header");
        var cxt = c.getContext("2d");
        cxt.clearRect(0, 0, c.width, c.height);
        cxt.fillStyle = "#FF0000";
        cxt.beginPath();
        //cxt.arc(mousePos(e).x,mousePos(e).y,15,0,Math.PI*2,true);
        cxt.arc(getCanvasPos(c, e).x, getCanvasPos(c, e).y, 15, 0, Math.PI * 2, true);
        cxt.closePath();
        cxt.fill();
    }

打个广告,有需要微信投票或点赞的朋友可以找我。wx:18963948278

相关文章:

  • 2021-04-19
  • 2021-07-20
  • 2021-12-31
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-15
  • 2022-12-23
  • 2021-12-10
相关资源
相似解决方案