实时两次点击之间的HTML5 Canvas线条绘制

时间:2019-03-04 17:29:38

标签: javascript html5 canvas

您可能会看到这是我的第一篇文章,所以请原谅使用stackoverflow的任何初学者错误。

我目前正在开发一个平面图网络应用程序,您可以在其中绘制线条来创建房屋的平面图。

所需的效果是: 单击一次后,用户将绘制一条临时线,其中起点X是单击的点,目标点Z是用户可以移动的鼠标。 我目前正在使用画布来实现此效果,但是以某种方式看不见该行或该行不存在。我尝试了一些调试,这些调试使我来到这里。

这是当前代码:

    function drawLineXY(fromXY, toXY) {
    if(!lineElem) {
        lineElem = document.createElement('canvas');
        lineElem.style.position = "absolute";
        lineElem.style.zIndex = 100;
        document.body.appendChild(lineElem);
        console.log("Added line element");

    }
    var leftpoint, rightpoint;
    if(fromXY.x < toXY.x) {
      leftpoint = fromXY;
      rightpoint = toXY;
    } else {
      leftpoint = toXY;
      rightpoint = fromXY;
    }

    var lineWidthPix = 4;
    var gutterPix = 0;
    var origin = {x:leftpoint.x-gutterPix, 
                  y:Math.min(fromXY.y, toXY.y)-gutterPix};
    lineElem.width = "1000px";
    lineElem.height = "1000px";
    lineElem.style.left = "0px";
    lineElem.style.top = "0px";
    var ctx = lineElem.getContext('2d');
    // Use the identity matrix while clearing the canvas
    ctx.save();
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.clearRect(0, 0, lineElem.width, lineElem.height);
    ctx.restore();
    ctx.lineWidth = 4;
    ctx.strokeStyle = '#09f';
    ctx.beginPath();
    ctx.moveTo(fromXY.x - origin.x, fromXY.y - origin.y);
    ctx.lineTo(toXY.x - origin.x, toXY.y - origin.y);
    ctx.stroke();
    console.log("drawing line..");
}

    function moveHandler(evt) {
    var startCentre, startBounds;
    var targets = [];

    if(clicked.length === 2) {
      targets = clicked;
    } else if(clicked.length === 1) {
      targets.push(clicked[0]);
      if(typeof hoverElement !== 'undefined') {
        targets.push(hoverElement);
      }
    }

    if(targets.length == 2) {
        var start = {x:targets[0], y:targets[0]};
        var end = {x:targets[1], y:targets[1]};
        drawLineXY(start, end);

    } else if(targets.length == 1) {
        var start = {x:targets[0], y:targets[0]};

        drawLineXY(start, {x:evt.clientX, y:evt.clientY});
    }  
};

    function clickHandler(e) {
    if(clicked.length == 2) {
        clicked = [];
    }
    clicked.push(e.target);
};

document.onclick = clickHandler;   
document.onmousemove = moveHandler;

正如您在drawLineXY的最后一行中看到的那样,我已将调试控制台日志“绘图线” 这在我四处移动鼠标时起作用。喜欢它应该。 但是没有电话,有人可以帮忙吗?

PS:#canvas是在style.css中指定的。

1 个答案:

答案 0 :(得分:0)

我为可能要实现的目标创建了一个非常基本的示例:

let c, ctx, fromXY, toXY;

window.onload = function(){
  document.onclick = clickHandler;   
  document.onmousemove = moveHandler;
  c = document.getElementById("myCanvas");
  ctx = c.getContext("2d");
  reset();
}

function draw(){
  clear();
  ctx.beginPath();
  ctx.moveTo(fromXY.x, fromXY.y);
  ctx.lineTo(toXY.x, toXY.y);
  ctx.stroke();
  ctx.closePath();
}

function clear(){
  ctx.clearRect(0, 0, c.width, c.height);
}

function reset() {
  fromXY = {};
  toXY = {};
}

function moveHandler(e) {
  if(typeof fromXY.x !== "undefined"){
    toXY.x = e.clientX;
    toXY.y = e.clientY;
    draw();
  }
}

function clickHandler(e) {
  if(typeof fromXY.x === "undefined"){
    fromXY.x = e.clientX;
    fromXY.y = e.clientY;
  }else{
    reset();
  }
}
<canvas id="myCanvas" height="500" width="500"></canvas>

您可以在draw()函数中设置行选项,如果您希望行保持不变,可以将它们的fromXYtoXY保存在数组中,并重新绘制它们。