【问题标题】:HTML5 Canvas line draw between two clicks, live两次点击之间的 HTML5 Canvas 线条绘制,实时
【发布时间】:2019-07-26 01:51:20
【问题描述】:

您可能会看到这是我的第一篇文章,所以请原谅任何初学者使用 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 中指定的。

【问题讨论】:

    标签: javascript html canvas


    【解决方案1】:

    我创建了一个非常基本的示例,说明您可能想要实现的目标:

    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();
      }
    }
    &lt;canvas id="myCanvas" height="500" width="500"&gt;&lt;/canvas&gt;

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

    【讨论】:

    • 经过一些调整,这几乎就是我想要的。感谢您的帮助并简化了我的代码。
    • 很高兴能帮上忙!
    猜你喜欢
    • 2011-07-23
    • 2016-01-23
    • 2017-12-21
    • 1970-01-01
    • 2018-12-19
    • 1970-01-01
    • 2019-02-28
    • 2014-03-20
    • 1970-01-01
    相关资源
    最近更新 更多