Skip to content

Instantly share code, notes, and snippets.

@adamchandra
Forked from mbostock/.block
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save adamchandra/388a8c4e383c3df721dd to your computer and use it in GitHub Desktop.

Select an option

Save adamchandra/388a8c4e383c3df721dd to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<style type="text/css">
path {
fill-rule: evenodd;
stroke: #333;
stroke-width: 1.5px;
}
.sun path {
fill: #6baed6;
}
.planet path {
fill: #9ecae1;
}
.ring path {
fill: #c6dbef;
}
</style>
</head>
<body>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.5.0"></script>
<script type="text/javascript">
var w = 960,
h = 500,
r = 80,
x = Math.sin(2 * Math.PI / 3),
y = Math.cos(2 * Math.PI / 3),
speed = 20;
var svg = d3.select("body").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + w / 2 + "," + h / 2 + ")scale(.625)");
svg.append("svg:g")
.attr("class", "ring")
.data([{angle: 2.25, teeth: 80, radius: -r * 5, ring: true}])
.append("svg:path")
.attr("d", gear);
svg.append("svg:g")
.attr("class", "sun")
.data([{angle: 0, teeth: 16, radius: r}])
.append("svg:path")
.attr("d", gear);
svg.append("svg:g")
.attr("class", "planet")
.attr("transform", "translate(0,-" + r * 3 + ")")
.data([{angle: 0, teeth: 32, radius: -r * 2}])
.append("svg:path")
.attr("d", gear);
svg.append("svg:g")
.attr("class", "planet")
.attr("transform", "translate(" + -r * 3 * x + "," + -r * 3 * y + ")")
.data([{angle: 0, teeth: 32, radius: -r * 2}])
.append("svg:path")
.attr("d", gear);
svg.append("svg:g")
.attr("class", "planet")
.attr("transform", "translate(" + r * 3 * x + "," + -r * 3 * y + ")")
.data([{angle: 0, teeth: 32, radius: -r * 2}])
.append("svg:path")
.attr("d", gear);
function gear(d) {
var n = d.teeth,
r2 = Math.abs(d.radius),
r0 = r2 - 8,
r1 = r2 + 8,
r3 = d.ring ? (r3 = r0, r0 = r1, r1 = r3, r2 + 20) : 20,
da = Math.PI / n,
a0 = -Math.PI / 2,
i = -1,
path = ["M0,", -r0];
while (++i < n) path.push(
"A", r0, ",", r0, " 0 0,1 ", r0 * Math.cos(a0 += da), ",", r0 * Math.sin(a0),
"L", r2 * Math.cos(a0), ",", r2 * Math.sin(a0),
"L", r1 * Math.cos(a0 += da / 3), ",", r1 * Math.sin(a0),
"A", r1, ",", r1, " 0 0,1 ", r1 * Math.cos(a0 += da / 3), ",", r1 * Math.sin(a0),
"L", r2 * Math.cos(a0 += da / 3), ",", r2 * Math.sin(a0),
"L", r0 * Math.cos(a0), ",", r0 * Math.sin(a0));
path.push("M0,", -r3, "A", r3, ",", r3, " 0 0,0 0,", r3, "A", r3, ",", r3, " 0 0,0 0,", -r3, "Z");
return path.join("");
}
d3.timer(function() {
svg.selectAll("path").attr("transform", function(d) {
return "rotate(" + (d.angle += speed / d.radius) + ")";
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment