[ Launch: Weather stations (recreated with SVG) ] 5786314 by poezn
[ Launch: Tributary inlet ] 5786233 by poezn
-
-
Save poezn/5786314 to your computer and use it in GitHub Desktop.
Weather stations: Voronoi animation
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
{"description":"Weather stations: Voronoi animation","endpoint":"","display":"svg","public":true,"require":[{"name":"TopoJSON","url":"http://d3js.org/topojson.v1.min.js"}],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"countries_simplified.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"stations.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"mask.svg":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":true,"play":true,"loop":true,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":10000,"ease":"linear","dt":0.01} |
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
var r = 5, | |
month = Math.floor(37) % 12 + 1; // 1 = January, 12 = December | |
width = 1000,//tributary.sw - 20, | |
height = width / 640 * 360; | |
var projection = d3.geo.equirectangular() | |
.center([0,0]) | |
.scale(145) | |
var path = d3.geo.path() | |
.projection(projection); | |
var world = tb['countries_simplified']; | |
var countries = topojson.feature(world, world.objects.world); | |
//var values = _.map(stations.features | |
var colorScale = d3.scale.linear() | |
.domain([-2000, 1500, 4000]) | |
.range(['#21C4F7', '#F0F8B3', '#D52323']) | |
.interpolate(d3.interpolateLab) | |
var color = function(d, i) { | |
var value = data[i].properties.val1; | |
return colorScale(value); | |
// var color = value >= -2000 && value < 4000 ? colorScale(value) : "#515268"; | |
// return color; | |
} | |
var scaleOpacity = d3.scale.pow() | |
.domain([0, 0.3, 1]) | |
.range(["#000000", "#000000", "#FFFFFF"]) | |
var stations = tb['stations']; | |
var delta = 9500; | |
var lowerLimit = -4000; | |
var upperLimit = lowerLimit + delta; | |
var data = _.chain(stations.features) | |
.map(function(d, i) { | |
var temps = _.chain(d3.range(1, 13)) | |
.map(function(month, i) { | |
return d.properties["val" + month]; | |
}) | |
.filter(function(d, i) { | |
return !_.isNull(d) && d > -9999; | |
}) | |
.value(); | |
d.properties.temps = temps; | |
return d; | |
}) | |
.filter(function(d, i) { | |
return d.properties.temps.length == 12; | |
}) | |
.value(); // filter out unknown results | |
var i = 0; | |
var render = function(i) { | |
var visibleData = data.slice(0, i); | |
var points = _.map(visibleData, function(d) { return projection(d.geometry.coordinates); }); | |
points = d3.geom.voronoi(points); | |
// clipping voronoi (this is not important for rendering, but is necessary to export SVG) | |
var padding = 0; | |
var bounds = d3.geom.polygon([ | |
[padding, padding], | |
[padding, height - padding], | |
[width - padding, height - padding], | |
[width - padding, padding] | |
]); | |
g = d3.select("#voronoi"); | |
var shapes = g.selectAll("path") | |
.data(points); | |
shapes.exit().remove(); | |
shapes.enter().append("path") | |
.attr({ | |
"d": function(d, i) { | |
return "M" + bounds.clip(d).join("L") + "Z"; | |
} | |
}) | |
.style({ | |
"fill": color, | |
"stroke": color | |
}); | |
} | |
tributary.init = function(g) { | |
g.append("g") | |
.attr({"id": "voronoi"}); | |
gClip = g.select("#continent-clip"); | |
gClip.selectAll("path.continent") | |
.data(countries.features) | |
.enter().append("path") | |
.attr({ | |
"class": "continent", | |
"d": function(d, i) { | |
return path(d) | |
} | |
}) | |
g.append("rect") | |
.attr({ | |
"width": width, | |
"height": height | |
}) | |
.style({ | |
"mask": "url(#continent-clip)", | |
"fill": "#181730" | |
}) | |
} | |
tributary.run = function(g, t) { | |
g.selectAll("#continent-clip rect") | |
.attr("fill", function(d, i) { return scaleOpacity(t); }) | |
var i = Math.pow((Math.exp(t) - 1), 1) * data.length / Math.exp(1); | |
render(Math.ceil(i)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment