Skip to content

Instantly share code, notes, and snippets.

@mbostock mbostock/.block
Last active Aug 5, 2019

Embed
What would you like to do?
Zoom to Bounding Box II
license: gpl-3.0
redirect: https://observablehq.com/@d3/zoom-to-bounding-box

A variant of the Zoom to Bounding Box example that uses zoom transitions to smoothly interpolate between different views. This example also allows you to freely pan and zoom with the mouse (or touch).

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.background {
fill: none;
pointer-events: all;
}
.feature {
fill: #ccc;
cursor: pointer;
}
.feature.active {
fill: orange;
}
.mesh {
fill: none;
stroke: #fff;
stroke-linecap: round;
stroke-linejoin: round;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script src="//d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 500,
active = d3.select(null);
var projection = d3.geo.albersUsa()
.scale(1000)
.translate([width / 2, height / 2]);
var zoom = d3.behavior.zoom()
.translate([0, 0])
.scale(1)
.scaleExtent([1, 8])
.on("zoom", zoomed);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.on("click", stopped, true);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", reset);
var g = svg.append("g");
svg
.call(zoom) // delete this line to disable free zooming
.call(zoom.event);
d3.json("/mbostock/raw/4090846/us.json", function(error, us) {
if (error) throw error;
g.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter().append("path")
.attr("d", path)
.attr("class", "feature")
.on("click", clicked);
g.append("path")
.datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
.attr("class", "mesh")
.attr("d", path);
});
function clicked(d) {
if (active.node() === this) return reset();
active.classed("active", false);
active = d3.select(this).classed("active", true);
var bounds = path.bounds(d),
dx = bounds[1][0] - bounds[0][0],
dy = bounds[1][1] - bounds[0][1],
x = (bounds[0][0] + bounds[1][0]) / 2,
y = (bounds[0][1] + bounds[1][1]) / 2,
scale = Math.max(1, Math.min(8, 0.9 / Math.max(dx / width, dy / height))),
translate = [width / 2 - scale * x, height / 2 - scale * y];
svg.transition()
.duration(750)
.call(zoom.translate(translate).scale(scale).event);
}
function reset() {
active.classed("active", false);
active = d3.select(null);
svg.transition()
.duration(750)
.call(zoom.translate([0, 0]).scale(1).event);
}
function zoomed() {
g.style("stroke-width", 1.5 / d3.event.scale + "px");
g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
// If the drag behavior prevents the default click,
// also stop propagation so we don’t click-to-zoom.
function stopped() {
if (d3.event.defaultPrevented) d3.event.stopPropagation();
}
</script>
@mishawagon

This comment has been minimized.

Copy link

mishawagon commented Dec 11, 2015

Hello, thank you for your great work. There seems to be one small problem: clicking New England states sends the map flying off screen. This doesn't happen for your Bounding Box 1 example: http://bl.ocks.org/mbostock/4699541

@bhrutledge

This comment has been minimized.

Copy link

bhrutledge commented Jul 7, 2016

As a newbie to D3, I'm loving this work. Any chance this will be updated with the new Zoom behavior in D3 V4?

@iamkevinv

This comment has been minimized.

Copy link

iamkevinv commented Oct 5, 2016

Also a d3 newcomer, and trying to digest what i might be doing wrong when trying to use the same sort of zoom transition to a bounding box under the v4 codebase.

@iamkevinv

This comment has been minimized.

Copy link

iamkevinv commented Oct 7, 2016

@bhrutledge / @mbostock - I've gone ahead and made a working fork to using d3 v4 here instead of the v3 example. https://bl.ocks.org/iamkevinv/0a24e9126cd2fa6b283c6f2d774b69a2
I'm still new to this, so i don't claim to definitively know this to be perfect, but it appears to be 100% ported over. Revisions can be reviewed here: https://gist.github.com/iamkevinv/0a24e9126cd2fa6b283c6f2d774b69a2/revisions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.