Zoom to Bounding Box
This interactive map of the United States uses D3.js (v3) with the Albers USA projection to render state boundaries as SVG paths. Clicking a state triggers a smooth 750-millisecond transition that computes the clicked feature's bounding box and applies a translate and scale transform to zoom into it. The zoom level and translation are calculated using the `d3.geo.path` bounds method, with stroke widths adjusted inversely to the scale. Clicking the active state or the background resets the viewport to its original full-country extent.
AI-generated descriptionThis example demonstrates how to compute a suitable translate and scale to zoom to the bounding box of a particular feature. Click on any state to zoom in; click on the focused state or the background to zoom out.
This variant uses transform transitions to change the viewport. A slightly better approach, and one that can also allow free panning and zooming with the mouse if desired, is to use zoom transitions.
forked from <a href='http://bl.ocks.org/mbostock/'>mbostock</a>'s block: <a href='http://bl.ocks.org/mbostock/4699541'>Zoom to Bounding Box</a>