3

I have a network topology view that looks like this. It is made up of nodes and links. I have a button that creates a line under g.newLinks, which one coordinate is set to a node while the other coordinate is updated to be the mouse coordinates on 'mousemove'. This all works correctly until I do a transform and then the mouse point and the line x2 and y2 no longer line up. How do I get the correct mouse coordinates after a transform?

enter image description here

 addNewLineToNode(node: Node) {
    this.drawingNewLine = true;

    this.newLinkDatum = {
        source: node,
        target: { x: node.x, y: node.y }
    };

    this.svg.select('.newLinks').append('svg:line').attr('class', 'link');
}


 function moveNewLine(d: any, event: MouseEvent) {
        if (me.drawingNewLine) {
            var mouse = d3.mouse(this);

            if(isNaN(mouse[0])){
                //Do nothing we've already set target x and y
            } else {
                var mCoords = me.getMouseCoords({x: mouse[0], y:mouse[1]});
                me.newLinkDatum.target.x = mCoords.x;
                me.newLinkDatum.target.y = mCoords.y;
            }

            const newLink = me.svg
                .select('.newLinks')
                .selectAll('.link')
                .datum(me.newLinkDatum)
                .attr('x1', function(d: any) {return d['source'].x;})
                .attr('y1', function(d: any) {return d['source'].y;})
                .attr('x2', function(d: any) {return d['target'].x;})
                .attr('y2', function(d: any) {return d['target'].y;});
        }
    }


 getMouseCoords(point: any){
    var pt = this.svg.node().createSVGPoint();
    pt.x = point.x;
    pt.y = point.y;
    pt = pt.matrixTransform(this.svg.node().getCTM());
    return { x: pt.x, y: pt.y };
}

I have tried using this solution (which is what I'm showing above): d3.js - After translate wrong mouse coordinates being reported. Why?

and this solution: D3 click coordinates after pan and zoom

iamcootis
  • 2,203
  • 3
  • 18
  • 22

1 Answers1

1

The mouse coordinates were relative to the whole svg instead of the network-zoomable-area that had the transform. The moveNewLine function needed to look like this:

 function moveNewLine(d: any, event: MouseEvent) {
        if (me.drawingNewLine) {

            var mouse = d3.mouse(me.svg.select('#network-zoomable-area').node());

            if(isNaN(mouse[0])){
                //Do nothing we've already set target x and y
            } else {
             //   var mCoords = me.getMouseCoords({x: mouse[0], y:mouse[1]});
                me.newLinkDatum.target.x = mouse[0];
                me.newLinkDatum.target.y = mouse[1];
            }

            const newLink = me.svg
                .select('.newLinks')
                .selectAll('.link')
                .datum(me.newLinkDatum)
                .attr('x1', function(d: any) {return d['source'].x;})
                .attr('y1', function(d: any) {return d['source'].y;})
                .attr('x2', function(d: any) {return d['target'].x;})
                .attr('y2', function(d: any) {return d['target'].y;});
        }
    } 
iamcootis
  • 2,203
  • 3
  • 18
  • 22