I managed to find the length of the paths in svg, but now i want to find the length for the line, rect, polygon and circle tags from SVG, I am really lost right now, and clues ? or are there already some functions like there is for path?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- How to fix IE ClearType + jQuery opacity problem i
- void before promise syntax
I tried to use the answer specified by ZetCoby for polygons, but in testing I found that the path length it returns is wrong.
Example:
The the above polygon should have a length of
56
, but thegetPolygonLength(el)
function returns a value of61.79898987322332
.I wrote an algorithm to correctly calculate the path length of an SVG polygon, so I thought I should contribute it back since this is the first hit on google when searching for this problem.
Here is my function. Enjoy...
I think you are looking at the problem incorrectly :
length of
rectangle = 2 * (width + height)
length of line ( use pythagorean theorem for any non vertical line c^2 = a^2 + b^2 ) or use ( x1 to x2 ) for horizontal , ( y1 to y2 ) for vertical
length of circle = 2 × π × radius ... etc
In case anyone else wants to find the length of these tags I made some functions for each of them, tested them and I say they work pretty ok, this was what i needed.
In SVG 2 all geometry elements will have a pathLength property but as of May 2017 this is still to be implemented in most browsers.
See https://developer.mozilla.org/en-US/docs/Web/API/SVGGeometryElement for more info.
We can future proof @zetcoby 's answer with: