Skip to content

Instantly share code, notes, and snippets.

@jasondavies
Created September 17, 2012 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasondavies/3736910 to your computer and use it in GitHub Desktop.
Save jasondavies/3736910 to your computer and use it in GitHub Desktop.
Projection Transitions (Atlantis)
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
height: 500px;
position: relative;
width: 960px;
}
#projection-menu {
position: absolute;
right: 10px;
top: 10px;
}
.background {
fill: #a4bac7;
}
.foreground {
fill: none;
stroke: #333;
stroke-width: 1.5px;
}
.graticule {
fill: none;
stroke: #fff;
stroke-width: .5px;
}
.graticule:nth-child(2n) {
stroke-dasharray: 2,2;
}
.land {
fill: #d7c7ad;
xstroke: #766951;
}
.boundary {
fill: none;
stroke: #a5967e;
}
</style>
<body>
<select id="projection-menu"></select>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://raw.github.com/d3/d3-plugins/master/geo/projection/projection.js"></script>
<script>
var width = 960,
height = 500;
var options = [
{name: "Aitoff", projection: d3.geo.aitoff()},
{name: "Albers", projection: d3.geo.albers().scale(145).parallels([20, 50]).center([0, 0]).translate([width * .500, height * .750])},
{name: "Bonne", projection: d3.geo.bonne().scale(120).translate([width * .500, height * .425])},
{name: "Collignon", projection: d3.geo.collignon().scale(93)},
{name: "Eckert I", projection: d3.geo.eckert1().scale(165)},
{name: "Eckert II", projection: d3.geo.eckert2().scale(165)},
{name: "Eckert III", projection: d3.geo.eckert3().scale(180)},
{name: "Eckert IV", projection: d3.geo.eckert4().scale(180)},
{name: "Eckert V", projection: d3.geo.eckert5().scale(170)},
{name: "Eckert VI", projection: d3.geo.eckert6().scale(170)},
{name: "Equirectangular (Plate Carrée)", projection: d3.geo.equirectangular().scale(950)},
{name: "Hammer", projection: d3.geo.hammer().scale(165)},
{name: "Goode Homolosine", projection: d3.geo.homolosine()},
{name: "Kavrayskiy VII", projection: d3.geo.kavrayskiy7()},
{name: "Lambert cylindrical equal-area", projection: d3.geo.cylindricalEqualArea()},
{name: "Larrivée", projection: d3.geo.larrivee().scale(95)},
{name: "Mercator", projection: d3.geo.mercator().scale(490)},
{name: "Miller", projection: d3.geo.miller().scale(100)},
{name: "Mollweide", projection: d3.geo.mollweide().scale(165)},
{name: "Nell–Hammer", projection: d3.geo.nellHammer()},
{name: "Polyconic", projection: d3.geo.polyconic().scale(100)},
{name: "Robinson", projection: d3.geo.robinson()},
{name: "Sinusoidal", projection: d3.geo.sinusoidal()},
{name: "van der Grinten", projection: d3.geo.vanDerGrinten().scale(75)},
{name: "Wagner VI", projection: d3.geo.wagner6()},
{name: "Winkel Tripel", projection: d3.geo.winkel3()}
];
var interval = setInterval(loop, 1500),
i = 0,
n = options.length - 1;
var projection = options[i].projection
.translate([width / 2 - .5, height / 2 - .5]);
var path = d3.geo.path().projection(projection);
var graticule = d3.geo.graticule()
.extent([[-180, -80], [180, 80]]).step([10, 10]),
outline = d3.geo.graticule();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("path")
.datum(outline.outline)
.attr("class", "background")
.attr("d", path);
svg.selectAll(".graticule")
.data(graticule.lines)
.enter().append("path")
.attr("class", "graticule")
.attr("d", path);
svg.append("path")
.datum(outline.outline)
.attr("class", "foreground")
.attr("d", path);
d3.json("readme-boundaries.json", function(err, collection) {
svg.insert("g", ".graticule")
.attr("class", "boundary")
.selectAll("path")
.data(collection)
.enter().append("path")
.attr("d", path);
});
d3.json("readme-world-countries.json", function(err, collection) {
svg.insert("g", ".graticule,.boundary")
.attr("class", "land")
.selectAll("path")
.data(collection)
.enter().append("path")
.attr("d", path);
});
var menu = d3.select("#projection-menu")
.on("change", change);
menu.selectAll("option")
.data(options)
.enter().append("option")
.text(function(d) { return d.name; });
function loop() {
var j = Math.floor(Math.random() * n);
menu.property("selectedIndex", i = j + (j >= i));
update(options[i]);
}
function change() {
clearInterval(interval);
update(options[this.selectedIndex]);
}
function update(option) {
path.projection(projection = option.projection);
projection.rotate([30, -45, 90]);
svg.selectAll(".land path, .boundary path, .graticule path").transition()
.duration(750)
.attr("d", path);
d3.timer.flush();
projection.rotate([0, 0, 0]);
svg.selectAll(".background, .foreground").transition()
.duration(750)
.attr("d", path);
d3.timer.flush();
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment