score:2
for the first approach you need to invert the zoom prior to inverting the xy coordinates into long lat coordinates:
var transform = d3.zoomtransform(this);
var xy = transform.invert(d3.mouse(this));
var longlat = projection.invert(xy);
we get the mouse position in pixel coordinates, invert it to zoom coordinates, and then invert that to geographic coordinates.
this should demonstrate the above:
var width = 960;
var height = 500;
var canvas = d3.select("canvas");
var context = canvas.node().getcontext("2d")
var projection = d3.geomercator();
var path = d3.geopath(projection,context);
d3.json("https://unpkg.com/world-atlas@1/world/110m.json", function(error, world) {
if (error) throw error;
renderfeature();
var zoom = d3.zoom()
.scaleextent([1, infinity])
.on("zoom", zoombycontext);
canvas.call(zoom);
function zoombycontext() {
var transform = d3.event.transform;
context.clearrect(0, 0, width, height);
context.save();
context.translate(transform.x, transform.y);
context.linewidth = 0.5 / transform.k;
context.scale(transform.k, transform.k);
renderfeature();
context.restore();
}
function renderfeature() {
context.beginpath();
path(topojson.mesh(world));
context.stroke();
}
canvas.on("click", function() {
var transform = d3.zoomtransform(this);
var xy = transform.invert(d3.mouse(this));
var longlat = projection.invert(xy);
console.log(longlat);
})
});
<canvas width="960" height="500"></canvas>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
the second approach is a little trickier, if the projection's translate is [0,0]
the approach you have will work, however this is rarely the case. the default value is [480,250]
(assumes a canvas that is 960x500), and fitsize
and fitextent
don't position the feature by modifying rotate and center, but rather translate. so you need to account for the initial translate when modifying the projection (just as you have done with scale):
var transform = d3.event.transform;
projection.translate([transform.x+translate[0]*transform.k, transform.y+translate[1]*transform.k]);
projection.scale(scale * transform.k);
here translate
is an array holding the initial translate values
and here's a demonstration that should demonstrate the above:
var width = 960;
var height = 500;
var canvas = d3.select("canvas");
var context = canvas.node().getcontext("2d")
var projection = d3.geomercator().center([105,3]).scale(1200).translate([2000,0]);
var path = d3.geopath(projection,context);
var scale = projection.scale();
var translate = projection.translate();
d3.json("https://unpkg.com/world-atlas@1/world/110m.json", function(error, world) {
if (error) throw error;
renderfeature();
var zoom = d3.zoom()
.scaleextent([0.1, infinity])
.on("zoom", zoombyprojection);
canvas.call(zoom);
function zoombyprojection() {
context.clearrect(0, 0, width, height);
var transform = d3.event.transform;
projection.translate([transform.x+translate[0]*transform.k, transform.y+translate[1]*transform.k]);
projection.scale(scale * transform.k);
renderfeature();
}
function renderfeature() {
context.beginpath();
path(topojson.mesh(world));
context.stroke();
}
});
<canvas width="960" height="500"></canvas>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/topojson-client@3"></script>
Source: stackoverflow.com
Related Query
- D3 Geo map zoom is not working correctly (Canvas)
- d3 world map with country click and zoom almost working not quite
- Angular 4 + D3v4 : dragging circle not working correctly in conjunction with zooming
- d3 zoom not working as in example
- D3 Zoom functionality is not working in Google chrome
- D3: Zooming/Panning Line Graph in SVG is not working in Canvas
- D3.js zoom with Ordinal axis not working - Issue with setup of scale in zoom behavior?
- Initial zoom not working in d3 v4
- D3 forced layout zoom and pan not working
- Why is hover on my d3 map not working properly?
- Hexbins are not showing on geo tile map D3.JS
- Select SVG elements using rectangle select box not working during zoom : d3.js
- Svg semantic zoom not working on SVG in HTML
- d3.js Date vs Half hour heat map not working
- d3 click handler not working on leaflet map layer
- Tooltip in d3/topojson choropleth map not working
- d3 world map -- calculate geo bounds on zoom
- SVG text path not working correctly in IE
- D3.js zoom is not working with mousewheel in safari
- Bubbles in Map not getting zoomed on Map zoom on mouse wheel in d3
- D3 force directed graph drag and zoom functions not working
- useInteractiveGuideline not working correctly in Angular nvd3
- svg filter not working in mozilla firefox after zoom
- d3.js - Zoom and Center on Click - Map scales, points do not
- Scattered plot zooming not working correctly in d3.js
- d3 map - After using blur filter, zoom does not work properly
- D3 Map data label not correctly showing
- D3 Data updates not working correctly
- D3 mapping with geoJson US Map working but not World
- D3.js semantic zoom and pan example not working
More Query from same tag
- Not able to generate area in D3
- D3 chart doesn't show up
- D3.js v4 elements are going back to initial position after dragging
- Converting string time into milliseconds
- how to draw nvd3 simple barchart with x and y axis in angularjs
- Remove comma to separate groups of thousands of axis labels in D3
- How do you translate HTML elements along with SVG elements when zooming and panning with D3
- How can I make a SVG group follow a gradient in a circle
- Updating Pie Chart with Specific colors not working as expected
- How would I parse a large TSV file in node.js?
- Remove current element based on data availability in D3.js
- patternTransform in svg not working for entire pattern
- Tooltip in linegraph doesn't show using d3-tip
- D3 force directed graph, apply force to a "g" element
- How does DOM Node cleanup work in d3?
- Enter/exit not properly cleaning up popped nodes/links in force layout
- Can't append text to d3.js force layout nodes
- Mongo Query: flatten field and split into a new document
- ClientWidth and ClientHeight are undefined
- Beeswarm plot, split "swarm" y value based on boolean
- Export or Download d3.js sunburst / collapsible tree charts as pdf file
- Convert CSV to JSON
- Stacked area chart data with NaN
- d3js v7: add node to forceSimulation
- Custom legend based on color of bars in a dc.js composite chart
- Joining GeoJSON to another GeoJSON
- Responsive D3 js chart
- NVD3 chart fails to calculate legend text length in Chrome, since Window.getComputedStyle does not return font-size correctly
- Programmatically adjusting d3 brush with animation
- legend not visible in d3.js pie chart