Skip to content

Instantly share code, notes, and snippets.

Last active Mar 16, 2020
What would you like to do?
Using d3.behavior.drag with a map
<!DOCTYPE html>
<meta charset="utf-8">
body {
background: #fcfcfa;
height: 500px;
position: relative;
width: 960px;
#projection-menu {
position: absolute;
right: 10px;
top: 10px;
.stroke {
fill: none;
stroke: #000;
stroke-width: 3px;
.fill {
fill: #fff;
.graticule {
fill: none;
stroke: #777;
stroke-width: .5px;
stroke-opacity: .5;
.land {
fill: #222;
<script src=""></script>
<script src=""></script>
var width = 960,
height = 500,
rotate = [0,0],
graticule = d3.geo.graticule();
var projection = d3.geo.azimuthalEqualArea()
.clipAngle(180 - 1e-3)
var path = d3.geo.path()
var drag = d3.behavior.drag()
.origin(function() { return {x: rotate[0], y: -rotate[1]}; })
.on("drag", function() {
rotate[0] = d3.event.x;
rotate[1] = -d3.event.y;
path = d3.geo.path().projection(projection);
d3.selectAll("path").attr("d", path);
var svg ="body").append("svg")
.attr("width", width)
.attr("height", height)
.datum({type: "Sphere"})
.attr("id", "sphere")
.attr("d", path);
.attr("class", "stroke")
.attr("xlink:href", "#sphere");
.attr("class", "fill")
.attr("xlink:href", "#sphere");
.attr("class", "graticule")
.attr("d", path);
d3.json("plate0.json", function(error, plates) {
svg.insert("path", ".graticule")
.datum(topojson.feature(plates, plates.objects.plates0))
.attr("class", "land")
.attr("d", path);
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