在向下滚动网页后,在鼠标点上绘制画布表现得很奇怪

时间:2017-08-22 10:39:11

标签: javascript canvas

在鼠标移动过程中,我在画布上使用了以下代码。

  <script type="text/javascript">
         var canvas, ctx, flag = false,
             prevX = 0,
             currX = 0,
             prevY = 0,
             currY = 0,
             dot_flag = false;

         var x = "black",
             y = 2;

         function init() {
             canvas = document.getElementById('can');
             ctx = canvas.getContext("2d");
             w = canvas.width;
             h = canvas.height;

             canvas.addEventListener("mousemove", function (e) {
                 findxy('move', e)
             }, false);
             canvas.addEventListener("mousedown", function (e) {
                 findxy('down', e)
             }, false);
             canvas.addEventListener("mouseup", function (e) {
                 findxy('up', e)
             }, false);
             canvas.addEventListener("mouseout", function (e) {
                 findxy('out', e)
             }, false);
         }

         function color(obj) {
             switch (obj.id) {
                 case "green":
                     x = "green";
                     break;
                 case "blue":
                     x = "blue";
                     break;
                 case "red":
                     x = "red";
                     break;
                 case "yellow":
                     x = "yellow";
                     break;
                 case "orange":
                     x = "orange";
                     break;
                 case "black":
                     x = "black";
                     break;
                 case "white":
                     x = "white";
                     break;
             }
             if (x == "white") y = 14;
             else y = 2;

         }

         function draw() {
             ctx.beginPath();
             ctx.moveTo(prevX, prevY);
             ctx.lineTo(currX, currY);
             ctx.strokeStyle = x;
             ctx.lineWidth = y;
             ctx.stroke();
             ctx.closePath();
         }

         function erase() {
             var m = confirm("Want to clear");
             if (m) {
                 ctx.clearRect(0, 0, w, h);
                 document.getElementById("canvasimg").style.display = 
           "none";
             }
         }

         function save() {
             document.getElementById("canvasimg").style.border = "2px solid";
             var dataURL = canvas.toDataURL();
             document.getElementById("canvasimg").src = dataURL;
             document.getElementById("canvasimg").style.display = "inline";
         }

         function findxy(res, e) {
             if (res == 'down') {
                 prevX = currX;
                 prevY = currY;
                 currX = e.clientX - canvas.offsetLeft;
                 currY = e.clientY - canvas.offsetTop;

                 flag = true;
                 dot_flag = true;
                 if (dot_flag) {
                     ctx.beginPath();
                     ctx.fillStyle = x;
                     ctx.fillRect(currX, currY, 2, 2);
                     ctx.closePath();
                     dot_flag = false;
                 }
             }
             if (res == 'up' || res == "out") {
                 flag = false;
             }
             if (res == 'move') {
                 if (flag) {
                     prevX = currX;
                     prevY = currY;
                     currX = e.clientX - canvas.offsetLeft;
                     currY = e.clientY - canvas.offsetTop;
                     draw();
                 }
             }
         }
</script>
    <div class="content-wrapper"  onmousemove="init()" style="width: 50%; 
float: left;">

       <canvas id="can" width="400" height="200" style="border:2px solid;">
   </canvas>
 </div>

当我向下滚动网页时,它不会精确地绘制鼠标点。这就是它绘制的东西,但是如果我向下滚动浏览网页则不是鼠标点。 我想避免这种情况,即使在我向下滚动之后它也应该在鼠标点上绘制。 有人能弄明白问题是什么吗?

1 个答案:

答案 0 :(得分:1)

您应该分别使用MouseEvent的offsetXoffsetY属性,而不是 e.clientX - canvas.offsetLeft e.clientY - canvas.offsetTop ,即使在滚动网页后,也能在鼠标点上精确绘制线条。

var canvas, ctx, flag = false,
   prevX = 0,
   currX = 0,
   prevY = 0,
   currY = 0,
   dot_flag = false;

var x = "black",
   y = 2;

function init() {
   canvas = document.getElementById('can');
   ctx = canvas.getContext("2d");
   w = canvas.width;
   h = canvas.height;

   canvas.addEventListener("mousemove", function(e) {
      findxy('move', e)
   }, false);
   canvas.addEventListener("mousedown", function(e) {
      findxy('down', e)
   }, false);
   canvas.addEventListener("mouseup", function(e) {
      findxy('up', e)
   }, false);
   canvas.addEventListener("mouseout", function(e) {
      findxy('out', e)
   }, false);
}

function color(obj) {
   switch (obj.id) {
      case "green":
         x = "green";
         break;
      case "blue":
         x = "blue";
         break;
      case "red":
         x = "red";
         break;
      case "yellow":
         x = "yellow";
         break;
      case "orange":
         x = "orange";
         break;
      case "black":
         x = "black";
         break;
      case "white":
         x = "white";
         break;
   }
   if (x == "white") y = 14;
   else y = 2;

}

function draw() {
   ctx.beginPath();
   ctx.moveTo(prevX, prevY);
   ctx.lineTo(currX, currY);
   ctx.strokeStyle = x;
   ctx.lineWidth = y;
   ctx.stroke();
   ctx.closePath();
}

function erase() {
   var m = confirm("Want to clear");
   if (m) {
      ctx.clearRect(0, 0, w, h);
      document.getElementById("canvasimg").style.display =
         "none";
   }
}

function save() {
   document.getElementById("canvasimg").style.border = "2px solid";
   var dataURL = canvas.toDataURL();
   document.getElementById("canvasimg").src = dataURL;
   document.getElementById("canvasimg").style.display = "inline";
}

function findxy(res, e) {
   if (res == 'down') {
      prevX = currX;
      prevY = currY;
      currX = e.offsetX;
      currY = e.offsetY;

      flag = true;
      dot_flag = true;
      if (dot_flag) {
         ctx.beginPath();
         ctx.fillStyle = x;
         ctx.fillRect(currX, currY, 2, 2);
         ctx.closePath();
         dot_flag = false;
      }
   }
   if (res == 'up' || res == "out") {
      flag = false;
   }
   if (res == 'move') {
      if (flag) {
         prevX = currX;
         prevY = currY;
         currX = e.offsetX;
         currY = e.offsetY;
         draw();
      }
   }
}
<div class="content-wrapper" onmousemove="init()" style="width: 50%; 
float: left;">
   <canvas id="can" width="400" height="500" style="border:2px solid;"></canvas>
</div>