score:6
in order to convert the coordinate returned by d3.mouse to the coordinate system used by d3.zoom, you need to get the zoom transform in addition to the coordinates returned by d3.mouse. i'll highlight one method here.
you can get the current zoom transform with:
d3.zoomtransform(selection.node());
where selection is the d3 selection the zoom is called on. while d3.mouse() gives us the screen coordinates of the mouse relative to the container, we can use that and the transform to give us the scaled and translated coordinates with transform.invert()
, which takes a point and will return the zoomed coordinates:
var xy = d3.mouse(this); // relative to specified container
var transform = d3.zoomtransform(selection.node());
var xy1 = transform.invert(xy); // relative to zoom
here's a quick example showing extraction of the zoomed coordinates as compared with the mouse coordinates. the axes are just to give a rough idea of what the zoom coordinate system is (they show zoom coordinates), but the functionality remains the same without them:
var svg = d3.select("body")
.append("svg")
.attr("width", 500)
.attr("height", 300)
.attr("fill","#eee");
var g = svg.append("g")
.attr("transform","translate(50,50)");
var rect = g.append("rect")
.attr("width",400)
.attr("height",200)
.attr("fill","#eee");
var x = d3.scalelinear().domain([0,400]).range([0,400]);
var y = d3.scalelinear().domain([0,200]).range([0,200]);
var axisx = d3.axistop().scale(x).ticksize(-200)
var axisy = d3.axisleft().scale(y).ticksize(-400);
var gx = g.append("g").call(axisx)
var gy = g.append("g").call(axisy)
var zoom = d3.zoom()
.scaleextent([1, 8])
.on("zoom",zoomed);
rect.call(zoom);
rect.on("click", function() {
var xy = d3.mouse(this);
var transform = d3.zoomtransform(rect.node());
var xy1 = transform.invert(xy);
console.log("mouse:[", xy[0], xy[1], "] zoomed:[",xy1[0],xy1[1],"]")
})
function zoomed() {
gx.call(axisx.scale(d3.event.transform.rescalex(x)));
gy.call(axisy.scale(d3.event.transform.rescaley(y)));
}
rect {
cursor: pointer;
}
.tick line {
stroke: #ccc;
pointer-events: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.10.0/d3.min.js"></script>
of course you must ensure that d3.mouse and d3.zoom are referencing the same thing, in this case the rectangle.
Source: stackoverflow.com
Related Query
- Get mouse position in SVG co-ordinates after zoom
- Leaflet: How to get pixel position of a lat lng after zoom, before zoom begins
- Get width of d3.js SVG text element after it's created
- Get coordinates of mouse click on svg using d3
- D3 - How to get correct scale and translate origin after manual zoom and pan to country path
- D3.zoom jumps when using mouse wheel after programmatically zoom
- d3.js pan and zoom jumps when using mouse after programatic zoom
- get point coordination using path.getPointAtLength after rotation in d3 and svg
- How do I get the d3 svg points to stay in their correct position on the leaflet map when zooming?
- What is the fastest way to get SVG element given a position (x,y)?
- Get pixel width of d3.js SVG element after it's created with width as percentage
- Get X and Y axes values from the mouse position in d3.js
- How to get the svg position within document?
- SVG returns to its default scale and position after initializing it with specific scale and position
- Get svg image real size after resizing
- 3d.js (Force) - get current mouse position on the link for the tooltip
- NVD3.js Get Data Point's Screen Position Within SVG Chart
- How to get position of empty SVG group?
- get the position details of a div inside an svg
- Open div on mouse position on click of svg text in d3
- How to blink nodes after successfully get position of nodes?
- d3 zoom not centering on mouse position with radial tree
- prevent d3 zoom moving text in svg to different position to the rest of the svg
- svg filter not working in mozilla firefox after zoom
- How to get a circle position in a g element in SVG using D3.js
- How can I get a SVG path shape to scale on zoom in D3?
- How to get element on mouse position within d3.xhr and async queue?
- How to get SVG child element position relative to SVG position on page, D3.js?
- Mouse events stop working after d3 graph, zoom loads
- SVG Gradient Bug: gradients change after zoom
More Query from same tag
- Can I append a literal SVG element with d3?
- Invalid value for <circle> attribute cx="NaN" using NVD3.js
- Understanding the call method in D3
- Line and Rect intersection not calculated properly on one axis
- Change style on points, d3
- d3 time format x axis showing same value
- setTimeout in D3.js
- D3.js path transition not working
- Line chart - how to display data points whilst line path is being drawn
- math to svg coordinate translation in javascript
- "marker-mid" is not working with path d3.js force layout
- NVD3 Line Chart Uncaught TypeError: Cannot read property 'x' of undefined
- Reading text file with special characters using d3.request
- d3: when a line move to specific positions call function
- How to get the zoom level of an svg image using d3
- Newline characters disappear after uploading a txt to a server
- Animating arc to horizontal line with d3.js
- D3 multi-line chart line path not displaying: d attribute missing
- Wrapping labels in StreamGraph
- D3.js Sankey diagram: Add title (text) above each column of nodes
- Passing json objects to a bottle template correctly
- Parsing error for nested object?
- d3 js transition not working
- How to trigger an angular change from a non-angular context e.g. from a D3 event?
- How to modify the shading of an arc in d3.js?
- D3.js mouseover fails to trigger with an overlapping object
- Stacked Bar Chart with Time Series Data
- d3 selections in event handler: select vs selectAll
- d3 circles shifting position on click-to-zoom
- In d3, how to get the interpolated line data from a SVG line?