在画布上绘制线条"" round"

时间:2017-08-20 16:43:37

标签: javascript canvas

我有这个代码,允许我在画布上画一条线

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

var x = "black",
y = 10;

function initialize() {
    canvas = document.getElementById('can');

    const bounds = canvas.getBoundingClientRect();
    canvas.width = bounds.width;
    canvas.height = bounds.height;

    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 draw(e) {
    ctx.beginPath();
    ctx.moveTo(prevX, prevY);
    ctx.lineTo(currX, currY);
    ctx.strokeStyle = x;
    ctx.lineWidth = y;
    ctx.stroke();
    ctx.closePath();
}

function getMousePos(canvas, evt) {
    var rect = canvas.getBoundingClientRect();
    return {
      x: evt.clientX - rect.left,
      y: evt.clientY - rect.top
    };
}

function findxy(res, e) {
    var pos = getMousePos(canvas, e);
    if (res == 'down') {
        prevX = currX;
        prevY = currY;

        currX = pos.x;
        currY = pos.y;

        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 = pos.x;
            currY = pos.y;
            draw();
        }
    }
}

对于像{10}这样的高{no}线条被绘制成一条粗水平线,我知道它可以绘制一条圆形线,像autodraw这样的网站可以在那里制作漂亮的草图。

以下是https://www.youtube.com/watch?v=e0SdFLDAb6U&feature=youtu.be

所显示的行类型的屏幕记录

1 个答案:

答案 0 :(得分:1)

在绘制 之前绘制 。这将使绘制的线'#34; round"。



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

var x = "black",
   y = 5;
initialize()

function initialize() {
   canvas = document.getElementById('can');

   const bounds = canvas.getBoundingClientRect();
   canvas.width = bounds.width;
   canvas.height = bounds.height;

   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 draw(e) {
   //arc
   ctx.beginPath();
   ctx.arc(currX, currY, y, 0, Math.PI * 2);
   ctx.fillStyle = x;
   ctx.fill();
   //line
   ctx.beginPath();
   ctx.moveTo(prevX, prevY);
   ctx.lineTo(currX, currY);
   ctx.lineWidth = y * 2;
   ctx.strokeStyle = x;
   ctx.stroke();
}

function getMousePos(canvas, evt) {
   var rect = canvas.getBoundingClientRect();
   return {
      x: evt.clientX - rect.left,
      y: evt.clientY - rect.top
   };
}

function findxy(res, e) {
   var pos = getMousePos(canvas, e);
   if (res == 'down') {
      prevX = currX;
      prevY = currY;

      currX = pos.x;
      currY = pos.y;

      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 = pos.x;
         currY = pos.y;
         draw();
      }
   }
}

canvas { border: 1px solid }

<canvas id="can" width="200" height="200"></canvas>
&#13;
&#13;
&#13;