mardi 16 mars 2021

Return which segment of a line is clicked on in an SVG with Javascript

I have an svg with a bunch of different paths. An example of one looks as follows:

<path d="M2943648.169104896,472020.4539518825L2943637.730875478,472048.6771215173" />

I'm attempting to find a way to get which section of the line has been clicked. I could add an onclick function which returns d, but this will only give me the full string as listed above, and I need just the relevant portion. As in, if a user clicks the first part of the line it'll return the correct coordinates (2943648.169104896,472020.4539518825) or if they click on the second segment, it will show the other coordinates that are in the value for d. Some of these paths have many different line segments.

I found an answer on here with a deprecated method of doing this and I was wondering what the latest way to do this is. Thank you.




Aucun commentaire:

Enregistrer un commentaire