Skip to content

Instantly share code, notes, and snippets.

@btipling
Forked from jasondavies/README.md
Created December 24, 2012 06:46
Show Gist options
  • Save btipling/4368114 to your computer and use it in GitHub Desktop.
Save btipling/4368114 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
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;
}
path {
fill: none;
stroke: #000;
stroke-width: 1px;
}
.background {
fill: none;
display: none;
}
.foreground {
fill: none;
stroke: #333;
stroke-width: 1.5px;
display: none;
}
.graticule {
fill: none;
stroke: #333;
stroke-width: .5px;
}
.graticule:nth-child(2n) {
stroke-dasharray: 2,2;
}
.land {
fill: #d7c7ad;
stroke: #766951;
}
.boundary {
fill: none;
stroke: #a5967e;
}
</style>
<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()},
{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 initial = [-71, 42, 0],
rotate = initial.slice(),
transitioning = false,
velocity = [.010, -.002],
t0 = Date.now();
var interval = setInterval(loop, 5000),
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();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.call(d3.behavior.drag()
.origin(function() { return {x: rotate[0], y: -rotate[1]}; })
.on("dragstart", function() { transitioning = true; })
.on("drag", function(d) {
rotate[0] = d3.event.x;
rotate[1] = -d3.event.y;
projection.rotate(rotate);
svg.selectAll(".geometry, .graticule").attr("d", path);
})
.on("dragend", function() {
transitioning = false;
t0 = Date.now();
initial = rotate.slice();
}));
svg.append("path")
.datum(graticule.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(graticule.outline)
.attr("class", "foreground")
.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() {
if (transitioning) return;
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) {
projection.precision(Infinity);
svg.selectAll(".geometry, .graticule").attr("d", path);
path.projection(projection = option.projection);
projection.rotate(rotate).precision(Infinity);
transitioning = true;
initial = rotate.slice();
svg.selectAll(".geometry, .graticule").transition()
.duration(750)
.attr("d", path)
.each("end", function() {
transitioning = false;
t0 = Date.now();
});
d3.timer.flush();
projection.rotate([0, 0, 0]);
svg.selectAll(".background, .foreground").transition()
.duration(750)
.attr("d", path);
d3.timer.flush();
projection.precision(10);
}
d3.timer(function() {
if (transitioning) return;
var t = Date.now() - t0;
rotate[0] = initial[0] + velocity[0] * t;
rotate[1] = initial[1] + velocity[1] * t;
projection.rotate(rotate);
svg.selectAll(".geometry, .graticule").attr("d", path);
});
d3.json("readme.json", function(err, geometry) {
var feature = svg.insert("path", ".foreground,.graticule")
.attr("class", "geometry")
.datum(geometry);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment