Last active
July 3, 2016 04:27
-
-
Save veltman/8bfb6be8ba724551b009 to your computer and use it in GitHub Desktop.
Rainbow states
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> | |
path { | |
stroke-linejoin: round; | |
stroke: #444; | |
stroke-width: 1px; | |
fill: url(#rainbow); | |
} | |
</style> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/topojson/1.6.20/topojson.min.js"></script> | |
<script src="//d3js.org/d3.v4.0.0-alpha.18.min.js"></script> | |
<script> | |
var rainbow = d3.scaleRainbow(); // lazy | |
</script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.14/d3.min.js"></script> | |
<script> | |
var width = 960, | |
height = 500; | |
var svg = d3.select("body").append("svg") | |
.attr("width", width) | |
.attr("height", height); | |
var gradient = svg.append("defs") | |
.append("radialGradient") | |
.attr("id","rainbow"); | |
gradient | |
.selectAll("stop") | |
.data(d3.range(0,1,0.01)) | |
.enter() | |
.append("stop") | |
.attr("offset",function(d){ | |
return d; | |
}) | |
.attr("stop-color",rainbow); | |
d3.json("stateplane.topo.json",function(err,stateplane){ | |
var zones = topojson.feature(stateplane,stateplane.objects.stateplane).features; | |
outer = topojson.merge(stateplane,stateplane.objects.stateplane.geometries); | |
zones.forEach(function(zone){ | |
var fn = d3.geo[zone.properties.type]().rotate(zone.properties.rotate); | |
if (zone.properties.parallels) { | |
fn.parallels(zone.properties.parallels); | |
} | |
zone.properties.path = scaledPath(fn); | |
}); | |
outer.type = "Polygon"; | |
outer.coordinates = [outer.coordinates[0][0]]; | |
d3.shuffle(zones); | |
var us = svg.append("path") | |
.attr("class","outer") | |
.datum(outer) | |
.attr("d",zones[0].properties.path) | |
.each(morph); | |
d3.selectAll("stop") | |
.each(cycle); | |
function morph() { | |
var current = zones[0], | |
next = zones.pop(); | |
zones.unshift(next); | |
d3.select(this).transition() | |
.ease("linear") | |
.duration(1250) | |
.attr("d",next.properties.path) | |
.each("end",morph); | |
} | |
function scaledPath(projection) { | |
var p = d3.geo.path().projection(projection); | |
// Auto-scale a la http://bl.ocks.org/mbostock/4707858 | |
projection | |
.scale(1) | |
.translate([0, 0]); | |
var b = p.bounds(outer), | |
s = .95 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height), | |
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2]; | |
projection | |
.scale(s) | |
.translate(t); | |
return p; | |
} | |
}); | |
function cycle(d) { | |
d3.select(this).transition() | |
.duration(2000) | |
.ease("linear") | |
.attrTween("stop-color",function(d){ | |
return function(t) { | |
return rainbow((d + t) % 1); | |
}; | |
}) | |
.each("end",cycle); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment