如何在画布上绘制流程图文档形状?

时间:2018-06-08 11:23:15

标签: javascript

我试图在画布上绘制流程图文档形状(https://i.imgur.com/ZfWDDfs.png)我已经部分地完成了它但是在下面的曲线上有困难,因为它显示确定一个位置但是当我改变xy坐标时曲线受到影响所以我想知道如何正确地绘制曲线,以便在坐标变化后保持相同。

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

// var x = 150, y = 150, w = 350, h = 350;  // display ok
var x = 50, y = 50, w = 350, h = 350;  // display not ok

 ctx.lineWidth = 10;
 ctx.beginPath();
 ctx.lineJoin="round";
// draw one horizontal and two vertical lines
 ctx.moveTo(x, y);
 ctx.lineTo((x + w)+(w/4), y);
 ctx.lineTo((x + w)+(w/4) , y + h/1.3);

 ctx.moveTo(x, y);
 ctx.lineTo(x, y + h);

// bottom curve .. problem start here
ctx.quadraticCurveTo(x,h + (h/1.2), (x + w/2) ,h + h/4);
ctx.lineTo((x + w)+(w/4), y+h/1.3);
ctx.stroke();

此代码使用x = 50和y = 50:

let drawChart = function(ctx, points) {
  ctx.moveTo((points[0].x), points[0].y);

  for (var i = 0; i < points.length - 1; i++) {
    // Draw point
    //   ctx.arc(points[i].x, points[i].y, 2, 0, Math.PI * 2, false);

    var x_mid = (points[i].x + points[i + 1].x) / 2;
    var y_mid = (points[i].y + points[i + 1].y) / 2;
    var cp_x1 = (x_mid + points[i].x) / 2;
    var cp_y1 = (y_mid + points[i].y) / 2;
    var cp_x2 = (x_mid + points[i + 1].x) / 2;
    var cp_y2 = (y_mid + points[i + 1].y) / 2;

    ctx.quadraticCurveTo(cp_x1, points[i].y, x_mid, y_mid);
    ctx.quadraticCurveTo(cp_x2, points[i + 1].y, points[i + 1].x, points[i + 1].y);
    ctx.stroke();
  }
  //   ctx.arc(points[points.length - 1].x, points[points.length - 1].y, 2, 0, Math.PI * 2, false)
  //   ctx.stroke();
}

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

//var x = 150, y = 150, w = 350, h = 350;   // ok

var x = 50,
  y = 50,
  w = 350,
  h = 350; // not ok

ctx.lineWidth = 10;

ctx.beginPath();
ctx.lineJoin = "round";
ctx.moveTo(x, y);
ctx.lineTo((x + w) + (w / 4), y);
ctx.lineTo((x + w) + (w / 4), y + h / 1.3);
ctx.moveTo(x, y);
ctx.lineTo(x, y + h);
// var points = [{x:x,y:y + h},{x:(x + w/2),y:h + h/5},{x:(x + w)+(w/4),y:h/1.3 }] 
// var points = [{x:x,y:y + h},{x:(x + w/3),y:h + h/2},{x:(x + w/1.2),y:h + h/4},{x:(x + w)+(w/4),y:h + h/5}] 
//drawChart(ctx,points);
ctx.quadraticCurveTo(x, h + (h / 1.2), (x + w / 2), h + h / 4);
// ctx.quadraticCurveTo((x + w/2),h ,  (x + w)+(w/4) , h + h/5 );
ctx.lineTo((x + w) + (w / 4), y + h / 1.3);
//  ctx.closePath();

ctx.stroke();

//	ctx.fill();
<canvas id="myCanvas" width="800" height="600" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

http://jsfiddle.net/0da2e869/

1 个答案:

答案 0 :(得分:1)

你必须使用:

ctx.quadraticCurveTo(x, y + h * 1.6, x + w / 2, y + h);

第一个和第二个arg是你网格上的位置。

ctx.quadraticCurveTo(cpx, cpy, x, y);

注意:要提高您的平局使用率ctx.lineCap = "round";。 ;)

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

drawIt(150, 150, 350, 350); // ok
ctx.strokeStyle = "red";
drawIt(50, 50, 350, 350); // not ok

function drawIt(x, y, w, h) {
  ctx.lineWidth = 10;

  ctx.beginPath();
  ctx.lineJoin = "round";
  ctx.lineCap = "round"; //<========== lineCap
  ctx.moveTo(x, y);
  ctx.lineTo((x + w) + (w / 4), y);
  ctx.lineTo((x + w) + (w / 4), y + h / 1.3);
  ctx.moveTo(x, y);
  ctx.lineTo(x, y + h);
  ctx.quadraticCurveTo(x, y + h * 1.6, x + w / 2, y + h);
  ctx.lineTo((x + w) + (w / 4), y + h / 1.3);

  ctx.stroke();
}
canvas {
  /* Focus on the "not ok" */
  transform-origin: top left;
  transform: scale(.25);
  transition:1s;
}

body:hover canvas {
  /* Focus on the "not ok" */
  transform-origin: top left;
  transform: none;
}
<canvas id="myCanvas" width="800" height="800" style="border:1px solid #d3d3d3;"></canvas>