Skip to content

Instantly share code, notes, and snippets.

@learningjs
Forked from mbostock/.block
Created December 6, 2015 14:51
Show Gist options
  • Save learningjs/510f0a52dad54df74df4 to your computer and use it in GitHub Desktop.
Save learningjs/510f0a52dad54df74df4 to your computer and use it in GitHub Desktop.
Wave Motion
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.orbit {
fill: none;
stroke: #999;
stroke-width: 1px;
}
</style>
<svg width="960" height="500"></svg>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var radius = 24,
separation = 1.1 * radius;
var index = cross(
d3.range(0, (width + radius) / separation),
d3.range(0, (height + radius) / separation)
);
svg.append("g")
.attr("class", "orbit")
.selectAll("g")
.data(index)
.enter().append("circle")
.attr("transform", function(d) { return "translate(" + d[0] * separation + "," + d[1] * separation + ")"; })
.attr("r", radius);
var particle = svg.append("g")
.attr("class", "particle")
.selectAll("g")
.data(index)
.enter().append("circle")
.attr("cx", radius)
.attr("r", 3.5);
d3.timer(function(elapsed) {
particle.attr("transform", function(d) {
return "translate(" + (d[0] * separation) + "," + d[1] * separation + ")rotate(" + ((d[0] + d[1]) * 20 + elapsed / 6) + ")";
});
});
function cross(x, y) {
var n = x.length,
m = y.length,
z = new Array(n * m);
for (var i = 0, k = 0; i < n; ++i) {
for (var j = 0; j < m; ++j, ++k) {
z[k] = [x[i], y[j]];
}
}
return z;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment