How to get coordinates of an svg element?

前端 未结 7 1998

I am using d3 to draw a line from a relative svg position and hence want to access the coordinates of the element itself. I tried something like this (where \"this\" refers

相关标签:
7条回答
  • 2020-12-23 12:02

    You can use the function getBBox() to get the bounding box for the path. This will give you the position and size of the tightest rectangle that could contain the rendered path.

    An advantage of using this method over reading the x and y values is that it will work with all graphical objects. There are more objects than paths that do not have x and y, for example circles that have cx and cy instead.

    0 讨论(0)
  • 2020-12-23 12:04

    i can handle it like that ;

    svg.selectAll("rect")
        .data(zones)
        .enter()
        .append("rect")
        .attr("id", function (d) { return "zone" + d.zone; })
        .attr("class", "zone")
        .attr("x", function (d, i) {
            if (parseInt(i / (wcount)) % 2 == 0) {
                this.xcor = (i % wcount) * zoneW;
            }
            else {
                this.xcor = (zoneW * (wcount - 1)) - ((i % wcount) * zoneW);
            }
    
            return this.xcor;
        })
    

    and anymore you can find x coordinate

    svg.select("#zone1").on("click",function(){alert(this.xcor});
    
    0 讨论(0)
  • 2020-12-23 12:07

    The way to determine the coordinates depends on what element you're working with. For circles for example, the cx and cy attributes determine the center position. In addition, you may have a translation applied through the transform attribute which changes the reference point of any coordinates.

    Most of the ways used in general to get screen coordinates won't work for SVGs. In addition, you may not want absolute coordinates if the line you want to draw is in the same container as the elements it connects.

    Edit:

    In your particular code, it's quite difficult to get the position of the node because its determined by a translation of the parent element. So you need to get the transform attribute of the parent node and extract the translation from that.

    d3.transform(d3.select(this.parentNode).attr("transform")).translate
    

    Working jsfiddle here.

    0 讨论(0)
  • 2020-12-23 12:07

    I use the consolidate function, like so:

     element.transform.baseVal.consolidate()
    

    The .e and .f values correspond to the x and y coordinates

    0 讨论(0)
  • 2020-12-23 12:13

    The element.getBoundingClientRect() method will return the proper coordinates of an element relative to the viewport regardless of whether the svg has been scaled and/or translated.

    See this question and answer.

    While getBBox() works for an untransformed space, if scale and translation have been applied to the layout then it will no longer be accurate. The getBoundingClientRect() function has worked well for me in a force layout project when pan and zoom are in effect, where I wanted to attach HTML Div elements as labels to the nodes instead of using SVG Text elements.

    0 讨论(0)
  • 2020-12-23 12:17
    svg.selectAll("rect")
    .attr('x',function(d,i){
        // get x coord
        console.log(this.getBBox().x, 'or', d3.select(this).attr('x'))
    })
    .attr('y',function(d,i){
        // get y coord
        console.log(this.getBBox().y)
    })
    .attr('dx',function(d,i){
        // get dx coord
        console.log(parseInt(d3.select(this).attr('dx')))
    })
    
    0 讨论(0)
提交回复
热议问题