Using D3 v4, SVG, and the zoom behaviour, on mousemove, I want to display the mouse co-ordinates in SVG's co-ordinate system.
The mousemove event only appears to provide client/screen co-ordinates.
How do I convert these co-ordinates into SVG co-ordinates that reflect the current zooom/pan/etc transform?
I can see examples of this using axis/scales/etc but I'm not creating a graph and am not using axis/etc- I'm using D3.js for an interactive diagram.
I've tried the crateSVGPoint/getScreenCTM approach but (a) don't really understand it and am therefore unsure how to apply it to my code, and (b) it doesn't appear to work when I zoom/pan - I just get the client/screen co-ordinates. EG: here's the code in my mousemove event to convert to SVG co-ords:
var theSvg = document.getElementById('svgItem');
var pt = theSvg.createSVGPoint();
var cursorPoint = function(evt){
pt.x = evt.clientX; pt.y = evt.clientY;
return pt.matrixTransform(theSvg.getScreenCTM().inverse());
}
var loc = cursorPoint(d3.event);
//the pair of co-ordinates should be different on zoom, but aren't
d3.select(".statusBarText").text("move (" + d3.event.x + "," + d3.event.y + ") (" + loc.x + "," + loc.y + ")");
For what it's worth, the zoom behaviour is applied to an SVG group element of the SVG element. Zooming works fine; I can see the translate/scale being applied to the group.
I've tried to modify the above to call createSVGPoint() on the group element which has been transformed but get errors about createSVGPoint() not being a function; I guess this only works with the SVG element from the DOM...
There's also a viewBox set on the SVG element I'm using, if that makes a difference.
Surely there's got to be an easy way to do the conversion?