Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the x/y coordinates of an SVG element(path etc) that does not have the X/Y attributes?

I am trying to get the position of SVG elements using Javascript. I have managed to get the position on elements that have X/Y attribute set. But elements, such as paths does not have this attribute.. At least not in my docuements.

Is there a way to calculate the position other than taking the first number in the "path"?

Thank you, Morten

like image 936
Morten Avatar asked May 02 '10 17:05

Morten


People also ask

Is it possible to draw any path in SVG If yes explain how it is possible?

The <path> element is the most powerful element in the SVG library of basic shapes. It can be used to create lines, curves, arcs, and more. Paths create complex shapes by combining multiple straight lines or curved lines. Complex shapes composed only of straight lines can be created as <polyline> s.

Is it possible to draw any path in SVG?

It can draw anything! I've heard that under the hood all the other drawing elements ultimately use path anyway. The path element takes a single attribute to describe what it draws: the d attribute.

What tag is used to define a path using SVG?

SVG Path - <path> The <path> element is used to define a path. The following commands are available for path data: M = moveto. L = lineto.


1 Answers

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.

Link: getBBox() in the SVG v1.1 standard.

like image 129
Jonas Pegerfalk Avatar answered Oct 23 '22 07:10

Jonas Pegerfalk