Skip to content

Instantly share code, notes, and snippets.

@zanarmstrong
Created March 1, 2016 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zanarmstrong/647310cb65466a31761f to your computer and use it in GitHub Desktop.
Save zanarmstrong/647310cb65466a31761f to your computer and use it in GitHub Desktop.
Stroke Dash Interpolation
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<style>
path {
fill: none;
stroke: #000;
stroke-width: 3px;
}
</style>
<script src="//d3js.org/d3.v3.min.js"></script>
<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
.interpolate("cardinal-closed");
var svg = d3.select("body").append("svg")
.datum(points)
.attr("width", 960)
.attr("height", 500);
svg.append("path")
.style("stroke", "#ddd")
.style("stroke-dasharray", "4,4")
.attr("d", line);
svg.append("path")
.attr("d", line)
.call(transition);
function transition(path) {
path.transition()
.duration(7500)
.attrTween("stroke-dasharray", tweenDash)
.each("end", function() { d3.select(this).call(transition); });
}
function tweenDash() {
var l = this.getTotalLength(),
i = d3.interpolateString("0," + l, l + "," + l);
return function(t) { return i(t); };
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment