Skip to content

Instantly share code, notes, and snippets.

@radiodario
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radiodario/87bc43cb590e6c906280 to your computer and use it in GitHub Desktop.
Save radiodario/87bc43cb590e6c906280 to your computer and use it in GitHub Desktop.
Hexagon watch grid

An Watch-like hexagon grid in d3

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.hexagon {
fill: none;
stroke: #000;
stroke-width: .5px;
}
svg {
background-color: #222;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/d3.hexbin.v0.min.js"></script>
<script>
var width = 427,
height = 548,
i = -1,
theta = 0,
dtheta = 0.03,
n = 2000,
k = 52; // samples to replace per frame
var randomX = d3.random.normal(width / 2, 80),
randomY = d3.random.normal(height / 2, 80),
points = d3.range(n).map(function() { return [randomX(), randomY()]; });
var color = d3.scale.linear()
.domain([0, 40])
.range(["black", "yellow"])
.interpolate(d3.interpolateLab);
var size = d3.scale.linear()
.domain([0, 40])
.range([0, 8]);
var hexbin = d3.hexbin()
.size([width, height])
.radius(40);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var hexagon = svg.append("g")
.attr("class", "apps")
.selectAll("circle")
.data(hexbin(points))
.enter()
.append('circle')
.attr("r", function(d) { return size(d.length) })
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.style("fill", function(d) { return color(d.length); });
d3.timer(function() {
theta += dtheta;
randomX = d3.random.normal(width / 2 + 80 * Math.cos(theta), 80),
randomY = d3.random.normal(height / 2 + 80 * Math.sin(theta), 80);
for (var j = 0; j < k; ++j) {
i = (i + 1) % n;
points[i][0] = randomX();
points[i][1] = randomY();
}
hexagon = hexagon
.data(hexbin(points), function(d) { return d.i + "," + d.j; });
hexagon.exit().remove();
hexagon.enter()
.append('circle')
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
hexagon
.attr("r", function(d) { return size(d.length) })
.style("fill", function(d) { return color(d.length); });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment