Javascript 如何使用画布在顶点之间绘制平行边(箭头)?

Javascript 如何使用画布在顶点之间绘制平行边(箭头)?,javascript,math,canvas,edges,arrows,Javascript,Math,Canvas,Edges,Arrows,我正在使用Javascript进行流网络可视化。 顶点表示为圆,边表示为箭头 这是我的边缘课程: function Edge(u, v) { this.u = u; // start vertex this.v = v; // end vertex this.draw = function() { var x1 = u.x; var y1 = u.y; var x2 = v.x; var y2 = v.y; context.beginPat

我正在使用Javascript进行流网络可视化。 顶点表示为圆,边表示为箭头

这是我的边缘课程:

function Edge(u, v) {
  this.u = u; // start vertex
  this.v = v; // end vertex

  this.draw = function() {
    var x1 = u.x;
    var y1 = u.y;
    var x2 = v.x;
    var y2 = v.y;

    context.beginPath();
    context.moveTo(x1, y1);
    context.lineTo(x2, y2);
    context.stroke();

    var dx = x1 - x2;
    var dy = y1 - y2;
    var length = Math.sqrt(dx * dx + dy * dy);

    x1 = x1 - Math.round(dx / ((length / (radius))));
    y1 = y1 - Math.round(dy / ((length / (radius))));
    x2 = x2 + Math.round(dx / ((length / (radius))));
    y2 = y2 + Math.round(dy / ((length / (radius))));

    // calculate the angle of the edge
    var deg = (Math.atan(dy / dx)) * 180.0 / Math.PI;
    if (dx < 0) {
      deg += 180.0;
    }
    if (deg < 0) {
      deg += 360.0;
    }
    // calculate the angle for the two triangle points
    var deg1 = ((deg + 25 + 90) % 360) * Math.PI * 2 / 360.0;
    var deg2 = ((deg + 335 + 90) % 360) * Math.PI * 2 / 360.0;
    // calculate the triangle points
    var arrowx = [];
    var arrowy = [];
    arrowx[0] = x2;
    arrowy[0] = y2;
    arrowx[1] = Math.round(x2 + 12 * Math.sin(deg1));
    arrowy[1] = Math.round(y2 - 12 * Math.cos(deg1));
    arrowx[2] = Math.round(x2 + 12 * Math.sin(deg2));
    arrowy[2] = Math.round(y2 - 12 * Math.cos(deg2));

    context.beginPath();
    context.moveTo(arrowx[0], arrowy[0]);
    context.lineTo(arrowx[1], arrowy[1]);
    context.lineTo(arrowx[2], arrowy[2]);
    context.closePath();
    context.stroke();
    context.fillStyle = "black";
    context.fill();
  };
}
函数的作用是在两个顶点之间绘制一条边,如下所示:

如果我们添加代码

var k = new Edge(v, u);
k.draw();
我们将获得:

但我想画两个方向的边,如下所示: (很抱歉我的绘画技巧不好)

当然,顶点和边方向不是固定的。 JSFIDLE上的一个工作示例(具有绘制顶点功能):

将轴与直线对齐。 如果旋转渲染使其与直线对齐,则可以使一切变得更简单。一旦你这样做了,就可以很容易地在线的上方或下方绘制,因为这只是在y方向,沿着线是x方向

因此,如果你有一条线

const line = { 
   p1 : { x : ? , y : ? },
   p2 : { x : ? , y : ? },
};
将其转换为向量并对该向量进行归一化

// as vector from p1 to p2
var nx = line.p2.x - line.p1.x;    
var ny = line.p2.y - line.p1.y;

// then get length
const len = Math.sqrt(nx * nx + ny * ny);

// use the length to normalise the vector
nx /= len;
ny /= len;
归一化向量表示我们要沿其渲染的新x轴,y轴与该轴成90度角。我们可以使用setTransform在直线的起点设置轴和原点(0,0)

ctx.setTransform(
    nx, ny,   // the x axis
    -ny, nx,  // the y axis at 90 deg to the x axis
    line.p1.x, line.p1.y  // the origin (0,0)
)
现在渲染线和箭头很容易,因为它们是轴对齐的

ctx.beginPath();
ctx.lineTo(0,0); // start of line
ctx.lineTo(len,0); // end of line
ctx.stroke();

// add the arrow head
ctx.beginPath();
ctx.lineTo(len,0); // tip of arrow
ctx.lineTo(len - 10, 10);
ctx.lineTo(len - 10, -10);
ctx.fill();
渲染偏离中心的两条线的步骤

var offset = 10;
ctx.beginPath();
ctx.lineTo(0,offset); // start of line
ctx.lineTo(len,offset); // end of line
ctx.moveTo(0,-offset); // start of second line
ctx.lineTo(len,-offset); // end of second line
ctx.stroke();

// add the arrow head
ctx.beginPath();
ctx.lineTo(len,offset); // tip of arrow
ctx.lineTo(len - 10, offset+10);
ctx.lineTo(len - 10, offset-10);
ctx.fill();

offset = -10;

// add second  arrow head
ctx.beginPath();
ctx.lineTo(0,offset); // tip of arrow
ctx.lineTo(10, offset+10);
ctx.lineTo(10, offset-10);
ctx.fill();
您可以使用

ctx.setTransform(1,0,0,1,0,0);  // restore default transform








 

太神了一个非常优雅和简单的方法来解决这个问题!非常感谢你!这正是我需要的。
ctx.setTransform(1,0,0,1,0,0);  // restore default transform