我有一堆 HTML 元素,想要通过 Canvas 用线连接它们。以下是我试图实现的示意图:
什么是实现这个的最佳方法?可能是画一半的线,写文本,然后再画剩下的线吗?
编辑:也许更好的标题/问题应该是:如何在HTML Canvas中找到两个任意点之间的中点?我想在那里绘制文本。
目前,我只有线条,没有文字。我想在每条线的中间放置文字,但由于它们是对角线,我不确定该怎么做。
当前代码:
// 'connectors' is an array of points corresponding to
// the middle of each big blue buttons' x-value
ctx.clearRect(0,0,canvas.width,canvas.height);
for(var i=0;i<connectors.length;i++){
var wpoint = connectors[i];
var pos1 = {w: wpoint, h: 0};
var pos2 = {w: canvas.width / 2, h: canvas.height};
ctx.beginPath();
ctx.moveTo(pos1.w,pos1.h);
ctx.lineTo(pos2.w,pos2.h);
ctx.stroke();
// Write Text Halfway
ctx.fillStyle = "blue";
ctx.font = "bold 16px Arial";
ctx.fillText("2702", 100, canvas.height / 2);
// No idea what to put as the x value here
}
什么是实现这个的最佳方法?可能是画一半的线,写文本,然后再画剩下的线吗?
编辑:也许更好的标题/问题应该是:如何在HTML Canvas中找到两个任意点之间的中点?我想在那里绘制文本。
x-value
,并将y-value
绘制到画布高度的一半。 - JVGcanvas.height
计算高度。 - JVG