Created
November 26, 2015 00:00
-
-
Save d3byex/d5b006647a357260baad to your computer and use it in GitHub Desktop.
D3byEX 12.16: Rotate a Globe
This file contains hidden or 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> | |
<html> | |
<head> | |
<meta name="description" content="D3byEX 12.16" /> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
<script src="http://d3js.org/d3.v3.min.js"></script> | |
<script src="http://d3js.org/topojson.v1.min.js"></script> | |
<script> | |
var width = 1024, height = 728; | |
var svg = d3.select('body') | |
.append('svg') | |
.attr({ | |
width: width, | |
height: height | |
}); | |
var url = 'https://gist.githubusercontent.com/d3byex/65a128a9a499f7f0b37d/raw/176771c2f08dbd3431009ae27bef9b2f2fb56e36/world-110m.json'; | |
d3.json(url, function (error, world) { | |
var projection = d3.geo.orthographic() | |
.scale(300) | |
.clipAngle(90) | |
.translate([width / 2, height / 2]); | |
var path = d3.geo.path() | |
.projection(projection); | |
var scaleLongitude = d3.scale.linear() | |
.domain([0, width]) | |
.range([-180, 180]); | |
var scaleLatitude = d3.scale.linear() | |
.domain([0, height]) | |
.range([90, -90]); | |
var countries = topojson.feature(world, world.objects.countries).features; | |
svg.selectAll('path') | |
.data(countries) | |
.enter() | |
.append('path') | |
.attr('d', path) | |
.style({ | |
fill: 'black', | |
stroke: 'white' | |
}); | |
svg.on('mousemove', function() { | |
var p = d3.mouse(this); | |
projection.rotate([scaleLongitude(p[0]), scaleLatitude(p[1])]); | |
svg.selectAll('path').attr('d', path); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment