带光标跟随器的画布

时间:2019-01-06 19:02:31

标签: javascript jquery html css canvas

$(function(){
     var mouseX = 0;
     var mouseY = 0;

     $('body,html').mousemove(function(e){
       var gap = parseInt($('#stalker').css("width")) / 2;
       mouseX = e.pageX - gap;
       mouseY = e.pageY - gap;
       $('#stalker').css('left', mouseX);
       $('#stalker').css('top', mouseY);
     });

     var canvas = document.getElementById('mycanvas');
     if(!canvas || !canvas.getContext) return false;
     var ctx = canvas.getContext('2d');

     ctx.lineWidth = 2;
     ctx.lineJoin = ctx.lineCap = 'round';

     var startX,
         startY,
         x,
         y,
         borderWidth = 5,
         isDrawing = false;

     $('#mycanvas,#stalker').mousedown(function(e){
        startX = e.pageX - $('#mycanvas').offset().left - borderWidth;
        startY = e.pageY - $('#mycanvas').offset().top - borderWidth;
     })
     .mouseup(function(e){
       if(!isDrawing) return;
       x = e.pageX - $('#mycanvas').offset().left - borderWidth;
       y = e.pageY - $('#mycanvas').offset().top - borderWidth;

       ctx.beginPath();
       ctx.moveTo(startX, startY);
       ctx.lineTo(x,y);
       ctx.stroke();
     })


     $('#mycanvas').mouseenter(function(e){
        startX = e.pageX - $('#mycanvas').offset().left - borderWidth;
        startY = e.pageY - $('#mycanvas').offset().top - borderWidth;
      });

     $('body,html').mousedown(function(e){
       isDrawing = true;
     })
     .mouseup(function(e){
       isDrawing = false;
     });

     $('#mycanvas,#stalker').mousemove(function(e){
       if(!isDrawing) return;
       x = e.pageX - $('#mycanvas').offset().left - borderWidth;
       y = e.pageY - $('#mycanvas').offset().top - borderWidth;

       ctx.beginPath();
       ctx.moveTo(startX, startY);
       ctx.lineTo(x,y);
       ctx.stroke();

       startX = x;
       startY = y;
     });

   });
#mycanvas{
  border:5px solid #999;
}
#stalker{
  position:absolute;
  width:80px;
  height:80px;
  border:solid 1px gray;
  border-radius:50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="stalker"></div>
<canvas width="550px" height="500px" id="mycanvas">
</canvas>

我正在尝试使用 canvas 制作绘图应用, 而且我需要一个在绘制时始终跟随光标的圆圈。 所以我写了上面的代码, 但这并没有真正起作用:如果我慢慢画一条线看起来不错,但是如果我移动光标更快,那条线就不会连接。 即使我没有释放鼠标单击,该行也将像两三行一样。 我认为这可能是因为 #stalker 不能赶上光标的速度,所以我也将 #mycanvas 上,但仍然不起作用。

有人知道为什么吗?

1 个答案:

答案 0 :(得分:0)

您可以将鼠标位置保存在数组中,然后绘制

一个简单的例子:

$(function(){
 var mouseX = 0;
 var mouseY = 0;

 $('body,html').mousemove(function(e){
   var gap = parseInt($('#stalker').css("width")) / 2;
   mouseX = e.pageX - gap;
   mouseY = e.pageY - gap;
   $('#stalker').css('left', mouseX);
   $('#stalker').css('top', mouseY);
 });

 var canvas = document.getElementById('mycanvas');
 if(!canvas || !canvas.getContext) return false;
 var ctx = canvas.getContext('2d');

 ctx.lineWidth = 2;
 ctx.lineJoin = ctx.lineCap = 'round';

 var startX,
     startY,
     x,
     y,
     borderWidth = 5,
     isDrawing = false,
     lines = [];

 $('body,html').mousedown(function(e){
   isDrawing = true;
   lines.push([]);
 })
 .mouseup(function(e){
   isDrawing = false;
 });

 $('#mycanvas,#stalker').mousemove(function(e){
   if(!isDrawing) return;
   x = e.pageX - $('#mycanvas').offset().left - borderWidth;
   y = e.pageY - $('#mycanvas').offset().top - borderWidth;

   lines[lines.length-1].push([x, y]);

 });

 function render() {
    ctx.clearRect(0, 0, 550, 500);

    for (const line of lines) {
        ctx.beginPath();
        for (const [i, pos] of Object.entries(line)) {
            if (!+i) {
                ctx.moveTo(pos[0], pos[1]);
            } else {
                ctx.lineTo(pos[0], pos[1]);
            }
        }
        ctx.stroke();
    }
 }

 (function loop() {
    render();
    requestAnimationFrame(loop);
 })();

});