【问题标题】:How i can draw new lines with convas js我如何用canvas js画新线
【发布时间】:2022-11-16 14:04:35
【问题描述】:

我想用 convas 画线,但是当我尝试创建一条新线时,我以前的线被删除了。我第一次与 convas 打交道,如果你能告诉我我的错误和问题的解决方案,我会很高兴

        const convas = document.querySelector(".v");
        const ctx = convas.getContext("2d");
    
        let startPositionLine = { x: 0, y: 0 };
        let endPositionLine = { x: 0, y: 0 };
        let { xStart, yStart } = startPositionLine;
        let { xEnd, yEnd } = endPositionLine;
    
        function moveMouseEvent(e) {
        xEnd = e.offsetX;
        yEnd = e.offsetY;
        ctx.beginPath();
        ctx.clearRect(0, 0, convas.width, convas.height);
        ctx.moveTo(xStart, yStart);
        ctx.lineTo(xEnd, yEnd);
        ctx.stroke();
      }
    
        convas.onmousedown = (e) => {
        ctx.beginPath();
        xStart = e.offsetX;
        yStart = e.offsetY;
        ctx.stroke();
        convas.onmousemove = (e) => moveMouseEvent(e);
      };
       convas.onmouseup = () => {
       convas.onmousemove = null;
      };

【问题讨论】:

  • 您正在绘制 function moveMouseEvent 那里有 clearRect 将清除所有内容
  • 由于每次移动鼠标时都必须清除画布,因此您需要在绘制时将绘制的每条线的开始和结束 x 和 y 坐标保存到数组中。然后在ctx.stroke(); 之后循环数组以重绘保存在数组中的每一行。因此,在数组中,每次单击画布时都会保存 Xstart、Ystart、xEnd、Yend。您还需要设置一个变量开关,以了解是否为每个单击/鼠标按下事件保存开始坐标或结束坐标。

标签: javascript canvas


【解决方案1】:

我在上面对你的帖子的评论中提到了这一点,但我想我只是举一个你可以在这里运行和测试的例子。所以你所做的是正确的,但你只需要在绘制它们时将每条绘制的线保存到一个数组中。您将在此处的 mousedown 事件处理程序中看到相关代码。然后,每次在 mousemove 事件处理程序中清除画布后,您都需要重新绘制这些保存的线条。

const canvas = document.getElementById('canvas');
var canvasrect = canvas.getBoundingClientRect();
var Xstart = 0;
var Ystart = 0;
var Xend = 0
var Yend = 0
var isDrawing = false;
var linesArray = [{}];


// MOUSE CLICK / BUTTON DOWN LISTENER
canvas.addEventListener('mousedown', e => {
  e.preventDefault();
  e.stopPropagation();
  canvasrect = canvas.getBoundingClientRect();

  Xstart = e.clientX - canvasrect.left;
  Ystart = e.clientY - canvasrect.top;


  // We need to know if the user is actuallydrawing or not.
  // Each time the user clicks their canvas we toggle whether to start or stop drawing.
  if (isDrawing) {
    // If this is the end of a line, save the end coordinates to the latest array element.
    linesArray[linesArray.length - 1].xe = Xend;
    linesArray[linesArray.length - 1].ye = Yend;
    isDrawing = false;
  } else {
    // If this is the start of a new line, save the start coordinates in a new array element along with end coordinate placeholders.
    linesArray.push({
      xs: Xstart,
      ys: Ystart,
      xe: 0,
      ye: 0
    })
    isDrawing = true;
  }
});


// MOUSE MOVE
canvas.addEventListener('mousemove', e => {
  e.preventDefault();
  e.stopPropagation();

  // get the current mouse x & y locations along with any scrolling and window resizing offsets.
  Xend = e.clientX - canvasrect.left;
  Yend = e.clientY - canvasrect.top;

  if (isDrawing === true) {

    var ctx = canvas.getContext('2d');
    //clear the canvas
    ctx.clearRect(0, 0, canvasrect.width, canvasrect.height);

    // Draw a line from the initial click coordinates to the current mouse pointer coordinates.
    ctx.strokeStyle = '#fe0101';
    ctx.lineWidth = 3;
    ctx.beginPath();
    ctx.moveTo(Xstart, Ystart);
    ctx.lineTo(Xend, Yend);
    ctx.stroke();

    // now redraw the previous lines saved in the linesArray
    if (linesArray.length >= 1) {
      for (let i = 0; i < linesArray.length; i++) {
        if (linesArray[i].xe != 0) {
          ctx.strokeStyle = '#fe0101';
          ctx.lineWidth = 3;
          ctx.beginPath();
          ctx.moveTo(linesArray[i].xs, linesArray[i].ys);
          ctx.lineTo(linesArray[i].xe, linesArray[i].ye);
          ctx.stroke();
        }
      }
    }
  }
});
&lt;canvas id="canvas" width="500" height="500" style="background-color:lightGray"&gt;&lt;/canvas&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多