Example of Custom Arc Tweening
Last active
January 21, 2017 22:15
-
-
Save philipcdavis/dafa2489338f593c526a8f9978f0dee1 to your computer and use it in GitHub Desktop.
Ring of Bars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Bars</title> | |
<style> | |
body { | |
background-color: black; | |
} | |
.container { | |
background-color: black; | |
margin: 0 auto; | |
display: block; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="chart"></div> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var width = 960; | |
var height = 500; | |
var padding = {top: 30, right: 30, bottom: 30, left: 30}; | |
var svg = d3.select("#chart").append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.attr("class", "container") | |
.append("g") | |
.attr("transform", "translate("+width / 2+", "+height / 2+")"); | |
var rays = 100; | |
var rayWidth = 0.02; | |
var cir = Math.PI * 2; | |
var chunk = cir / rays; | |
var data = d3.range(rays).map(function(d, i) { | |
return { | |
startAngle: (i * chunk), | |
endAngle: (i * chunk) + rayWidth, | |
outerRadius: 150 - i | |
} | |
}); | |
var arc = d3.arc() | |
.innerRadius(50); | |
var piece = svg | |
.selectAll(".piece") | |
.data(data) | |
.enter() | |
.append("path") | |
.attr("class", "piece") | |
.style("fill", "white") | |
.transition() | |
.duration(2000) | |
.delay(function(d,i) { | |
return i * 20 | |
}) | |
.attrTween("d", arcTween()); | |
function arcTween() { | |
return function(d) { | |
var interpolate = d3.interpolateNumber(51, d.outerRadius); | |
return function(t) { | |
d.outerRadius = interpolate(t); | |
return arc(d); | |
}; | |
}; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment