Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Zoom/Pan Map Example

Demonstrates using d3.behavior.zoom with a geographic projection. Based on an example by Iain Dillingham.

<!DOCTYPE html>
<meta charset="utf-8">
path {
fill: #E5F5F9;
stroke: #2CA25F;
stroke-width: 0.5;
#axes {
stroke: #BDBDBD;
stroke-width: 0.5;
<script src=""></script>
var width = 960,
height = 500,
projection = d3.geo.mercator(),
path = d3.geo.path().projection(projection);
var svg ="body").append("svg")
.attr("width", width)
.attr("height", height)
.on("zoom", redraw));
var axes = svg.append("g").attr("id", "axes"),
xAxis = axes.append("line").attr("y2", height),
yAxis = axes.append("line").attr("x2", width);
d3.json("uk.json", function(error, json) {
function redraw() {
if (d3.event) {
svg.selectAll("path").attr("d", path);
var t = projection.translate();
xAxis.attr("x1", t[0]).attr("x2", t[0]);
yAxis.attr("y1", t[1]).attr("y2", t[1]);
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment