Skip to content

Instantly share code, notes, and snippets.

Forked from mbostock/.block
Last active August 5, 2019 04:43
Show Gist options
  • Save iamkevinv/0a24e9126cd2fa6b283c6f2d774b69a2 to your computer and use it in GitHub Desktop.
Save iamkevinv/0a24e9126cd2fa6b283c6f2d774b69a2 to your computer and use it in GitHub Desktop.
Zoom to Bounding Box II - Updated for d3 v4
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
.background {
fill: none;
pointer-events: all;
.feature {
fill: #ccc;
cursor: pointer;
} {
fill: orange;
.mesh {
fill: none;
stroke: #fff;
stroke-linecap: round;
stroke-linejoin: round;
<script src=""></script> <!-- Inclusion of d3 v4 -->
<script src=""></script>
var width = 960,
height = 500,
active =;
var projection = d3.geoAlbersUsa() // updated for d3 v4
.translate([width / 2, height / 2]);
var zoom = d3.zoom()
// no longer in d3 v4 - zoom initialises with zoomIdentity, so it's already at origin
// .translate([0, 0])
// .scale(1)
.scaleExtent([1, 8])
.on("zoom", zoomed);
var path = d3.geoPath() // updated for d3 v4
var svg ="body").append("svg")
.attr("width", width)
.attr("height", height)
.on("click", stopped, true);
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.on("click", reset);
var g = svg.append("g");
.call(zoom); // delete this line to disable free zooming
// .call(zoom.event); // not in d3 v4
d3.json("/mbostock/raw/4090846/us.json", function(error, us) {
if (error) throw error;
.data(topojson.feature(us, us.objects.states).features)
.attr("d", path)
.attr("class", "feature")
.on("click", clicked);
.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 ="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];
// .call(zoom.translate(translate).scale(scale).event); // not in d3 v4
.call( zoom.transform, d3.zoomIdentity.translate(translate[0],translate[1]).scale(scale) ); // updated for d3 v4
function reset() {
active.classed("active", false);
active =;
// .call( zoom.transform, d3.zoomIdentity.translate(0, 0).scale(1) ); // not in d3 v4
.call( zoom.transform, d3.zoomIdentity ); // updated for d3 v4
function zoomed() {"stroke-width", 1.5 / d3.event.transform.k + "px");
// g.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")"); // not in d3 v4
g.attr("transform", d3.event.transform); // updated for d3 v4
// 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();
Copy link

kbtz commented Dec 12, 2016

Thanks for sharing!

Copy link

Thanks for this really helpful. This example is using GEOJSON, what edits should be made to load in TopoJSON?. Also it maybe worth commenting this in somewhere. Im new to this and struggling to work out when people are using GEO and TOPO.


Copy link

kylem038 commented Jul 3, 2019

Just wanted to say thank you for sharing this. The majority of examples out there for this type of functionality are in v3, and it's not easily translatable. This just flat out works, and I appreciate the comments showing what changed between v3 and v4.

Copy link


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment