Created
May 8, 2016 08:28
-
-
Save Libbum/ec6a8df2049c6084106512e962788aa5 to your computer and use it in GitHub Desktop.
World tour of Tiny Countries
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> | |
body { | |
background: #fcfcfa; | |
} | |
.fill { | |
fill: #fff; | |
} | |
.graticule { | |
fill: none; | |
stroke: #777; | |
stroke-width: .5px; | |
stroke-opacity: .5; | |
} | |
.land { | |
fill: #222; | |
} | |
.cselect { | |
fill: #81a2be; | |
} | |
</style> | |
<body> | |
<h1></h1> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script src="//d3js.org/topojson.v1.min.js"></script> | |
<script> | |
var width = 500, | |
height = 500; | |
var title = d3.select("h1"); | |
var projection = d3.geo.orthographic() | |
.translate([width / 2, height / 2]) | |
.scale(width / 2 - 20) | |
.clipAngle(90) | |
.precision(0.6) | |
.rotate([-40, -30]); | |
var path = d3.geo.path() | |
.projection(projection); | |
var graticule = d3.geo.graticule(); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
svg.append("defs").append("path") | |
.datum({type: "Sphere"}) | |
.attr("id", "sphere") | |
.attr("d", path); | |
svg.append("use") | |
.attr("class", "stroke") | |
.attr("xlink:href", "#sphere"); | |
svg.append("use") | |
.attr("class", "fill") | |
.attr("xlink:href", "#sphere"); | |
svg.append("path") | |
.datum(graticule) | |
.attr("class", "graticule") | |
.attr("d", path); | |
d3.json("world.json", function(error, world) { | |
if (error) throw error; | |
var land = topojson.feature(world, world.objects.countries).features, | |
small = land.filter(function(d) { return d.geometry.type === 'Point'; }) | |
i = 0, | |
n = small.length; | |
svg.insert("g", ".graticule").attr("id", "countries"); | |
d3.selectAll("#countries").selectAll("path").data(land.filter(function(d) { return d.geometry.type !== 'Point'; })) | |
.enter().append("path").attr("class", "land").attr("id", function(d, i) { return d.id; }).attr("d", path); | |
(function transition() { | |
d3.transition() | |
.duration(1250) | |
.each("start", function() { | |
d3.selectAll(".cselect").remove(); | |
title.text(small[i = (i + 1) % n].properties.name); | |
}) | |
.tween("rotate", function() { | |
var p = d3.geo.centroid(small[i]), | |
r = d3.interpolate(projection.rotate(), [-p[0], -p[1]]); | |
return function(t) { | |
svg.insert("path", ".graticule").datum({ type: "Point", coordinates: [p[0], p[1]] }) | |
.attr("class", "cselect").attr("d", path); | |
projection.rotate(r(t)); | |
d3.select("body").selectAll("path").attr("d", d3.geo.path().projection(projection)); | |
}; | |
}) | |
.transition() | |
.each("end", transition); | |
})(); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment