Skip to content

Instantly share code, notes, and snippets.

@basilesimon
Created May 1, 2018 16:20
Show Gist options
  • Save basilesimon/7b9b2aa88ef4d2bcff56d7120ef7bd7d to your computer and use it in GitHub Desktop.
Save basilesimon/7b9b2aa88ef4d2bcff56d7120ef7bd7d to your computer and use it in GitHub Desktop.
fresh block
license: mit
<!DOCTYPE html>
<html>
<head>
<title>Animate Multiple Paths</title>
<script src="http://d3js.org/d3.v4.js"></script>
<style type="text/css">
html, body {
margin: 0; padding: 0;
height: 100%;
}
path {
fill: none;
stroke: #333;
stroke-width: 1px;
}
</style>
</head>
<body>
<svg id="svg"></svg>
<script type="text/javascript">
const draw = function(data) {
const h = 500, w = 960;
const line = d3.line();
const svg = d3.select("#svg")
.attr("width", w)
.attr("height", h)
.style("background", "#fff");
const g = svg.append("g");
const p = g.selectAll("path")
.data(data)
.enter()
.append("path")
.attr("d", d => line(d))
.attr("stroke-dasharray", function() {
const totalLength = this.getTotalLength();
return totalLength + " " + totalLength;
})
.attr("stroke-dashoffset", function() {
const totalLength = this.getTotalLength();
return totalLength;
})
.transition()
.duration((d,i) => (i +1) * 2000)
.ease(d3.easeLinear)
.attr("stroke-dashoffset", 0);
};
const randomLineData = [[[10,30],[900,30]],
[[10,60],[900,60]],
[[10,90],[900,90]],
[[10,120],[900,120]],
[[10,150],[900,150]],
[[10,180],[900,180]],
[[10,210],[900,210]]];
draw(randomLineData);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment