Last active
April 25, 2024 13:48
-
-
Save d3noob/5189284 to your computer and use it in GitHub Desktop.
World Map with zoom and pan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
World map with pan and zoom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
path { | |
stroke: white; | |
stroke-width: 0.25px; | |
fill: grey; | |
} | |
</style> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script> | |
<script src="http://d3js.org/topojson.v0.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var projection = d3.geo.mercator() | |
.center([0, 5 ]) | |
.scale(150) | |
.rotate([-180,0]); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var path = d3.geo.path() | |
.projection(projection); | |
var g = svg.append("g"); | |
// load and display the World | |
d3.json("world-110m2.json", function(error, topology) { | |
g.selectAll("path") | |
.data(topojson.object(topology, topology.objects.countries) | |
.geometries) | |
.enter() | |
.append("path") | |
.attr("d", path) | |
}); | |
// zoom and pan | |
var zoom = d3.behavior.zoom() | |
.on("zoom",function() { | |
g.attr("transform","translate("+ | |
d3.event.translate.join(",")+")scale("+d3.event.scale+")"); | |
g.selectAll("path") | |
.attr("d", path.projection(projection)); | |
}); | |
svg.call(zoom) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment