Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active December 19, 2022 18:21
Show Gist options
  • Save mbostock/b7671cb38efdfa5da3af to your computer and use it in GitHub Desktop.
Save mbostock/b7671cb38efdfa5da3af to your computer and use it in GitHub Desktop.
Arc Corners III
license: gpl-3.0
redirect: https://observablehq.com/@d3/arc-corners
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.paths--straight {
fill: none;
stroke: #777;
}
.paths--round {
fill: #ccc;
stroke: #000;
stroke-width: 1.5px;
stroke-linejoin: round;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var data = [1, 1, 2, 3, 5, 8, 13, 21];
var width = 960,
height = 500,
outerRadius = height / 2 - 30,
innerRadius = outerRadius / 3;
var pie = d3.layout.pie()
.padAngle(.03);
var arc = d3.svg.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var straightPath = svg.append("g")
.attr("class", "paths--straight")
.selectAll("path")
.data(data)
.enter().append("path");
var roundPath = svg.append("g")
.attr("class", "paths--round")
.selectAll("path")
.data(data)
.enter().append("path");
var ease = d3.ease("cubic-in-out"),
duration = 2500;
d3.timer(function(elapsed) {
var t = ease(1 - Math.abs((elapsed % duration) / duration - .5) * 2),
arcs = pie(data);
straightPath.data(arcs).attr("d", arc.cornerRadius(0));
roundPath.data(arcs).attr("d", arc.cornerRadius((outerRadius - innerRadius) / 2 * t));
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment