Skip to content

Instantly share code, notes, and snippets.

@veltman
Last active July 3, 2016 04:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veltman/627c4c9b4ccaefe6e0d8 to your computer and use it in GitHub Desktop.
Save veltman/627c4c9b4ccaefe6e0d8 to your computer and use it in GitHub Desktop.
Hypnostates
<!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");
var modded = function(d){
return rainbow((4 * d) % 1);
};
gradient
.selectAll("stop")
.data(d3.range(0,1,0.01))
.enter()
.append("stop")
.attr("offset",function(d){
return d;
})
.attr("stop-color",modded);
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 modded((d + t) % 1);
};
})
.each("end",cycle);
}
</script>
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment