Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting point information from SVG. #23

Open
popara opened this issue Oct 21, 2017 · 3 comments
Open

Getting point information from SVG. #23

popara opened this issue Oct 21, 2017 · 3 comments

Comments

@popara
Copy link

popara commented Oct 21, 2017

Hi,

So there is one quircky thing about SVG when you are trying to get precise information about the position of pointer inside the SVG itself. Namely when you bind event handler to e.g. mousemove you would get for clientX, and clientY with values relative to the document, not the node I have bound my handler to.

This is somewhat normal because SVG uses different units potentially for internal drawing, and you need to transform those units into pixels relative to the SVG it self. Here is how you do it in dirty JS world:

function handler(e){
  var svg = document.getElementsByTagName("svg")[0]
  svg = document.querySelector('svg');
  var pt = svg.createSVGPoint();
  pt.x = e.clientX; 
  pt.y = e.clientY;
  return pt.matrixTransform(svg.getScreenCTM().inverse());
 };

I just wanted to leave a note about this. I have no idea how to fix this nicely.
My quick and dirty solution was to make native module, and stick similar code into it.

Better systematic solution requires much forethought, which I believe you can allocate to the issue. In mean time I will continue thinking about neat solution also :)

Further reading:
https://www.sitepoint.com/how-to-translate-from-dom-to-svg-coordinates-and-back-again/

@process-bot
Copy link

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@ondrejsevcik
Copy link

One possible solution - custom event https://discourse.elm-lang.org/t/custom-elements-extend-svg-real-coordinates-on-mouse-events/1762/4

@popara
Copy link
Author

popara commented Jan 14, 2019

One super neat solution I have started using:

Overlay one transparent rect on top your svg, (aka put is last in the list, with transparent fill) and attach all events to it instead of parent svg.

Works like beauty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants