How to get coordinates of an svg element?

前端 未结 7 2000

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.

提交回复
热议问题