I have a bunch of HTML elements that I want to link to lines through Canvas. Here is a layout of what I'm trying to achieve:

Currently I just have strings, no text. I want to place the text halfway between each line, but, seeing that these are diagonals, I am not sure how to do this.
Current code:
// '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 }
What is the best way to achieve this? Potentially draw half a line, write text, and then draw the rest of the line?
EDIT . Perhaps the best heading / question would be: How do I find the midpoint between two arbitrary points in an HTML Canvas? I want to draw text there.
source share