Skip to content

Instantly share code, notes, and snippets.

@a-malahova
Forked from jasondavies/README.md
Last active November 12, 2017 20:26
Show Gist options
  • Select an option

  • Save a-malahova/ca89dc880d3e53b00b2c76c33e113848 to your computer and use it in GitHub Desktop.

Select an option

Save a-malahova/ca89dc880d3e53b00b2c76c33e113848 to your computer and use it in GitHub Desktop.
Map with pan+zoom and floating buttons for zoom in/out and reset.

Uses geographic projection and d3.zoom working with d3.v4.js

Based on an example by Jason Davies.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3.v4 geographic projection and zoom</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div id="svg"></div>
<div class="controls">
<button id="zoom_in">+</button>
<button id="zoom_out">-</button>
<button id="reset">.</button>
</div>
<script type="text/javascript" src="http://d3js.org/d3.v4.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
var mapZoom = d3.zoom().on("zoom", freeZoom);
function freeZoom() {
map.attr("transform", d3.event.transform);
}
d3.select("#reset").on("click", function() {
svg.transition().duration(500).call(mapZoom.transform, d3.zoomIdentity);
});
d3.select("#zoom_in").on("click", function() {
mapZoom.scaleBy(svg.transition().duration(500), 1.1);
});
d3.select("#zoom_out").on("click", function() {
mapZoom.scaleBy(svg.transition().duration(500), 0.9);
});
var width = "400",
height = "250",
projection = d3.geoMercator();
projection.translate([200, 700]);
projection.scale(500);
var path = d3.geoPath().projection(projection);
var svg = d3.select("#svg")
.append("svg")
.attr("width", width)
.attr("height", height)
.call(mapZoom);
var map = svg.append("g")
.attr("width", width)
.attr("height", height);
d3.json("uk.json", function(error, uk_json) {
map.selectAll("path")
.data(uk_json.features)
.enter().append("path")
.attr("d", path);
});
var t = projection.translate()
var axis = map.append("line")
.attr("id", "axis")
.attr("y2", height)
.attr("x1", t[0])
.attr("x2", t[0]);
body {
margin: 10;
background-color: #E0E0E0;
}
.controls {
position: absolute;
left: 15px;
top: 15px;
display: grid;
grid-gap: 5px;
}
button {
width: 30px;
height: 30px;
font-size: 18px;
display:grid;
border-radius:50% !important;
background: rgba(240, 240, 240, 0.8);
color: darkgrey;
border: 1px solid #aaaaaa;
user-select: none;
cursor: pointer;
box-sizing: border-box;
}
button:active {
background: rgba(200, 200, 200, 0.8);
}
button:focus {
outline: 0;
}
#svg {
position: fixed;
border: 1px solid brown;
}
svg {
background-color: lightblue;
display: block;
}
path {
fill: green;
stroke: darkgreen;
stroke-width: 0.15;
}
#axis {
stroke: brown;
stroke-width: 0.25;
}
Display the source blob
Display the rendered blob
Raw
Loading
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