Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active August 5, 2020 20:35
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbostock/1d48ce4f3024edfc2277 to your computer and use it in GitHub Desktop.
Save mbostock/1d48ce4f3024edfc2277 to your computer and use it in GitHub Desktop.
Spiral Triangle
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<style>
rect {
fill: #fff;
stroke: #000;
}
</style>
<svg width="960" height="500">
<defs>
<clipPath id="clip-upper">
<rect width="960" height="305" x="-480" y="-305"></rect>
</clipPath>
<clipPath id="clip-lower">
<rect width="960" height="195" x="-480" y="0"></rect>
</clipPath>
</defs>
<g clip-path="url(#clip-upper)" transform="translate(480,305)"></g>
<g clip-path="url(#clip-lower)" transform="translate(480,305)"></g>
</svg>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500,
triangleSize = 400,
squareCount = 71,
squareSize = 90,
speed = .08;
var square = d3.selectAll("g")
.selectAll("g")
.data(function(d, i) { return i ? [0, 1, 2] : [2, 0, 1]; })
.enter().append("g")
.attr("transform", function(i) { return "rotate(" + (i * 120 + 60) + ")translate(0," + -triangleSize / Math.sqrt(12) + ")"; })
.selectAll("rect")
.data(d3.range(squareCount))
.enter().append("rect")
.datum(function(i) { return i / squareCount; })
.attr({width: squareSize, height: squareSize, x: -squareSize / 2, y: -squareSize / 2});
d3.timer(function(elapsed) {
square
.attr("transform", function(t) { return "translate(" + (t - .5) * triangleSize + ",0)rotate(" + (t * 120 + elapsed * speed) + ")"; });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment