Skip to content

Instantly share code, notes, and snippets.

@d3noob
Last active May 21, 2021 01:37
Show Gist options
  • Save d3noob/e549dc220052ac8214b9db6ce47d2a61 to your computer and use it in GitHub Desktop.
Save d3noob/e549dc220052ac8214b9db6ce47d2a61 to your computer and use it in GitHub Desktop.
Map Pan and Zoom v5
license: mit

This is a simple map example with pan and zoom centered on the Pacific.

This graph is part of the code samples for the update to the book D3 Tips and Tricks to version 5 of d3.js.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
stroke: white;
stroke-width: 0.25px;
fill: grey;
}
</style>
<body>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://unpkg.com/topojson@3"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geoMercator()
.center([0, 5 ])
.scale(150)
.rotate([-180,0]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var path = d3.geoPath()
.projection(projection);
var g = svg.append("g");
// load and display the World
d3.json("world-110m2.json").then(function(topology) {
g.selectAll("path")
.data(topojson.feature(topology, topology.objects.countries)
.features)
.enter().append("path")
.attr("d", path);
});
var zoom = d3.zoom()
.scaleExtent([1, 8])
.on('zoom', function() {
g.selectAll('path')
.attr('transform', d3.event.transform);
});
svg.call(zoom);
</script>
</body>
</html>
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