How To Know The Current Zoom Level In D3.js
Solution 1:
In the draw function the current event will have the zoom level (d3.event.scale as you mentioned). Also if you keep the behaviour around like:
var zm = d3.behavior.zoom().x(x).scaleExtent([1,10]).on("zoom", draw);
Then you can find the current zoom level by calling:
zm.scale();
Solution 2:
As of D3 v4 there are two documented ways of retrieving the zoom state. To quote d3/d3-zoom README (under Zoom Transforms):
To retrieve the zoom state, use event.transform on a current zoom event within a zoom event listener (see zoom.on), or use d3.zoomTransform for a given node.
https://github.com/d3/d3-zoom/blob/master/README.md#zoom-transforms
Solution 3:
rect.call(zm=d3.behavior.zoom().x(x).scaleExtent([1,10]).on("zoom", draw));
After a new test i have the answer :
var currentZoom = d3.event.scale;
But only available/readable in the draw() function called by .on("zoom", draw)
rect.call( zm = d3.behavior.zoom().x(x).scaleExtent([1,10]).on("zoom", draw));
function draw() {
// trace l'axe X
svg.select("g.x.axis").call(xAxis);
// trace l'axe Y
svg.select("g.y.axis").call(yAxis);
// trace la courbe
svg.select("path.line").attr("d", line);
console.log(zm.scale(), zm.translate()); // , zm.translate[1] = Y
console.log(d3.event.scale, d3.event.translate[0]); // , d3.event.translate[1] = Y
}
Solution 4:
As of today (d3.v5), the way to check the zoom level within the closure is by looking at this attribute:
d3.event.transform.k
Post a Comment for "How To Know The Current Zoom Level In D3.js"