I'm having trouble getting a transition to repeat, for a series of elements, in this case a set of three lines. The animation runs just fine once, but when it is repeated (with the same data), all three lines merge into a single line (the last array in data
). What am I doing wrong?
(function() {
var w = 100, h = 100
var div = d3.select('#sketches').append('div')
var svg = div.append("svg")
.attr("width", w)
.attr("height", h)
var x = 0, y = 55
var data = [
[x, y, x+20, y-40],
[x+10, y, x+30, y-40],
[x+20, y, x+40, y-40]
];
(function lines() {
svg.selectAll('line')
.data(data).enter().append('line')
.attr("stroke", "black")
.attr('x1', function(d) {return d[0]})
.attr('y1', function(d) {return d[1]})
.attr('x2', function(d) {return d[2]})
.attr('y2', function(d) {return d[3]})
.transition()
.duration(3000)
.ease('linear')
.attr('x1', function(d) {return d[0] + 60})
.attr('y1', function(d) {return d[1]})
.attr('x2', function(d) {return d[2] + 60})
.attr('y2', function(d) {return d[3]})
.each('end', function(d) {
d3.select(this).remove()
lines()
})
})()
})()
body {
padding: 1rem;
}
svg {
background-color: silver;
stroke: black;
stroke-width: 1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="sketches"></div>