Skip to content

Instantly share code, notes, and snippets.

@jorpic
Last active August 29, 2015 14:02
Show Gist options
  • Save jorpic/037e6d91212a4cb1a547 to your computer and use it in GitHub Desktop.
Save jorpic/037e6d91212a4cb1a547 to your computer and use it in GitHub Desktop.
d3.js spiral
<!DOCTYPE html>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="http://coffeescript.org/extras/coffee-script.js"></script>
<script type="text/coffeescript">
x = 1
data = _.map([1..36], -> x *= 0.892)
squares = d3.select('body').append('svg')
.attr('width', innerWidth)
.attr('height', innerHeight)
.selectAll("g")
.data(data)
.enter()
.append("svg:g")
.classed("square", true)
squares.append("svg:rect")
.attr("rx", 1)
.attr("ry", 1)
.attr("x", -2)
.attr("y", -2)
.attr("width", 4)
.attr("height", 4)
.attr("transform", (d) -> "scale(#{d*innerWidth})")
.style("fill", (d,i) -> ["#eeeeee", "#fff"][i%2])
t = 0
setInterval(
->
t = if t >= 1200 then 0 else t + 1
alpha = 5*360*t/1200
squares
.attr("transform", (d, i) ->
cx = innerWidth/2 + 36*Math.cos(-alpha/5)
cy = innerHeight/2 + 36*Math.sin(-alpha/5)
"translate(#{[cx, cy]})rotate(#{alpha*i})")
, 110)
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment