在鼠标按下事件中选择的两点之间绘制直线

yaj*_*nab 1 javascript jquery html5-canvas

我有代码在单击鼠标(mousedown 事件和 mousemove 事件)时移动鼠标时绘制一条线。

我还希望从点的起点(我第一次单击点,mousedown 事件)到终点(我抬起鼠标事件,mouseup 事件)绘制一条直线。

(function() {
      var canvas = document.querySelector('#paint');
      var ctx = canvas.getContext('2d');

      var sketch = document.querySelector('#sketch');
      var sketch_style = getComputedStyle(sketch);
      canvas.width = parseInt(sketch_style.getPropertyValue('width'));
      canvas.height = parseInt(sketch_style.getPropertyValue('height'));

      var mouse = {
        x: 0,
        y: 0
      };
      
      var last_mouse = {
        x: 0,
        y: 0
      };

      /* Mouse Capturing Work */
      canvas.addEventListener('mousemove', function(e) {
        last_mouse.x = mouse.x;
        last_mouse.y = mouse.y;

        mouse.x = e.pageX - this.offsetLeft;
        mouse.y = e.pageY - this.offsetTop;
      }, false);


      /* Drawing on Paint App */
      ctx.lineWidth = 5;
      ctx.lineJoin = 'round';
      ctx.lineCap = 'round';
      ctx.strokeStyle = 'black';

      canvas.addEventListener('mousedown', function(e) {
        canvas.addEventListener('mousemove', onPaint, false);
      }, false);

      canvas.addEventListener('mouseup', function() {
        canvas.removeEventListener('mousemove', onPaint, false);
      }, false);

      var onPaint = function() {
        ctx.beginPath();
        ctx.moveTo(last_mouse.x, last_mouse.y);
        ctx.lineTo(mouse.x, mouse.y);
        ctx.closePath();
        ctx.stroke();
      };
    }());
Run Code Online (Sandbox Code Playgroud)
#sketch{
  width: 100%;
  height: 200px;
  background-color: #CCCCCC;
}
Run Code Online (Sandbox Code Playgroud)
<div id="sketch">
  <canvas id="paint">
  </canvas>
</div>
Run Code Online (Sandbox Code Playgroud)

Jul*_*ian 5

您想要存储mouseDown事件的坐标,然后使用它们绘制一条到事件坐标的线mouseUp。我修改了您的代码以显示可以完成的方法:

(function() {
      var canvas = document.querySelector('#paint');
      var ctx = canvas.getContext('2d');

      var sketch = document.querySelector('#sketch');
      var sketch_style = getComputedStyle(sketch);
      canvas.width = parseInt(sketch_style.getPropertyValue('width'));
      canvas.height = parseInt(sketch_style.getPropertyValue('height'));

      var mouse = {
        x: 0,
        y: 0
      };
      
      var last_mouse = {
        x: 0,
        y: 0
      };

      /* Mouse Capturing Work */
      canvas.addEventListener('mousemove', function(e) {
        last_mouse.x = mouse.x;
        last_mouse.y = mouse.y;

        mouse.x = e.pageX - this.offsetLeft;
        mouse.y = e.pageY - this.offsetTop;
      }, false);


      /* Drawing on Paint App */
      ctx.lineWidth = 5;
      ctx.lineJoin = 'round';
      ctx.lineCap = 'round';
      ctx.strokeStyle = 'black';
      canvas.addEventListener('mousedown', function(e) {
        initialPoint = {x:mouse.x, y:mouse.y}
        canvas.addEventListener('mousemove', onPaint, false);
      }, false);

      canvas.addEventListener('mouseup', function() {
        drawStraightLine()
        canvas.removeEventListener('mousemove', onPaint, false);
      }, false);

      var onPaint = function() {
        ctx.beginPath();
        ctx.moveTo(last_mouse.x, last_mouse.y);
        ctx.lineTo(mouse.x, mouse.y);
        ctx.strokeStyle = "#000000";
        ctx.closePath();
        ctx.stroke();
      };
      
      let initialPoint
      const drawStraightLine = function() {
        ctx.beginPath();
        ctx.moveTo(initialPoint.x, initialPoint.y);
        ctx.lineTo(mouse.x, mouse.y);
        ctx.strokeStyle = "#FF000077";
        ctx.stroke();
      }
    }());
Run Code Online (Sandbox Code Playgroud)
#sketch{
  width: 100%;
  height: 180px;
  background-color: #DDDDDD;
}
Run Code Online (Sandbox Code Playgroud)
<div id="sketch">
  <canvas id="paint" />
</div>
Run Code Online (Sandbox Code Playgroud)

initialPoint是按下按钮时的鼠标位置,drawStarightLine()是释放按钮时执行的方法。还为线条添加了不同的颜色以使其明显。