Skip to content

Instantly share code, notes, and snippets.



Last active Feb 6, 2019
What would you like to do?
Stroke Dash Interpolation
license: gpl-3.0

By interpolating the stroke-dasharray style property, you can animate a stroke from start to end. This might be used to convey directionality of connected links in a network graphic, such as our graphic of Academy Award nominees. By setting the transition duration proportional to the length of the line and using linear easing, you can further animate the stroke at constant speed.

A related technique is point-along-path interpolation.

<!DOCTYPE html>
<meta charset="utf-8">
path {
fill: none;
stroke: #000;
stroke-width: 3px;
<script src="//"></script>
var points = [
[480, 200],
[580, 400],
[680, 100],
[780, 300],
[180, 300],
[280, 100],
[380, 400]
var line = d3.svg.line()
.tension(0) // Catmull–Rom
var svg ="body").append("svg")
.attr("width", 960)
.attr("height", 500);
.style("stroke", "#ddd")
.style("stroke-dasharray", "4,4")
.attr("d", line);
.attr("d", line)
function transition(path) {
.attrTween("stroke-dasharray", tweenDash)
.each("end", function() {; });
function tweenDash() {
var l = this.getTotalLength(),
i = d3.interpolateString("0," + l, l + "," + l);
return function(t) { return i(t); };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment