How to calculate length of a link


How can I get the x & y coordinates of a link between two nodes?

Scenario: I am trying to position a link label. I need to compute the segmentfraction for based on a specific horizontal offset from the from-node. For example, say I want the topleft of the label to be exactly 20 pixels from the right edge of the node (the links always come from the right of the node in this case), then because the link can be at an angle I have to calculate the fraction of the link length required to have the label positioned exactly 20 horizontally pixels away from the node etc.

If I can get the co-ordinates of the from- and to- ends of the link I can carry out the calculations.

So - how to get the co-ordinates of the ends of a link please ?

PS. I am aware that if I use a bezier link style then this will make the calculation much more difficult. My links are default straight lines, although I do use the toShortLength attribute and I have yet to decide what impact this will have.

I have a feeling that this should be easy but at the moment I can’t think of the way forward.

Thanks in advance.

Link.points is a List of Points in document coordinates.

Perfect - I can use a dragcomputation on the nodes to call a calculation function such as below, using the first() and last() funcs to access the points that I need.

function dragCalc(part, pt, gridpt){

    part.findLinksInto().each(function (link) {
        console.log('Found link - x points are ' + link.points.first().x + ' - ' + link.points.last().x)

        var labelPos = ...// do the calculation

        var label = link.findObject('linkLabel')
        label.segmentFraction = labelPos 


    return pt // give back the original pt to the draggingtool.



That might work for your app, but in general it would be wiser to override Link.computePoints to call the base method and if it returns true to reset the label’s segment fraction and/or offset.

That way the labels can be adjusted even if nodes are moved around not by the user’s dragging, but by a layout or other programmatic movement of nodes.

Ah - thanks for that wise suggestion, You have just saved me some time.