-
-
Save jmgimeno/2935262 to your computer and use it in GitHub Desktop.
Squares ↔ Hexagons
This file contains hidden or 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 { | |
| fill: none; | |
| stroke: #000; | |
| stroke-width: .6px; | |
| } | |
| </style> | |
| <body> | |
| <script src="http://d3js.org/d3.v2.min.js?2.9.3"></script> | |
| <script> | |
| var width = 960, | |
| height = 500, | |
| size = 100, | |
| offset = 0; | |
| var svg = d3.select("body").append("svg") | |
| .attr("width", width) | |
| .attr("height", height); | |
| var g = svg.selectAll("g") | |
| .data(d3.geom.voronoi(vertices())) | |
| .enter().append("g"); | |
| var path = g.append("path") | |
| .attr("d", function(d) { return "M" + d.join("L") + "Z"; }); | |
| var circle = g.append("circle") | |
| .attr("transform", function(d) { return "translate(" + d + ")"; }) | |
| .attr("r", 2); | |
| d3.timer(function() { | |
| ++offset; if (offset > size) offset -= size; | |
| var data = vertices(); | |
| path.data(d3.geom.voronoi(data)).attr("d", function(d) { return "M" + d.join("L") + "Z"; }); | |
| circle.data(data).attr("transform", function(d) { return "translate(" + d + ")"; }); | |
| }); | |
| function vertices() { | |
| return d3.merge(d3.range(-size, width + size, size).map(function(x, i) { | |
| return d3.range(-size, height + size, size).map(function(y, j) { | |
| return [x + (j & 1 ? -offset : offset), y]; | |
| }); | |
| })); | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment