I was messing around with transitions, and I noticed some stuttering and flickering when the transitions apply to the selection in another function. If, however, the transition is applied with a chain of methods, it works exactly as prescribed.
The following is a small example ( Fiddle ) of simply moving some text. The first, leftmost line is magically teleported down the page until the transition begins. The second, rightmost line has a smooth transition from the top to the bottom of the page.
Why is this teleport happening? Obviously, applying transitions in a separate function is not the same as chains, but is there a way to achieve this? Let's say I want to apply the same transition to many different objects - extracted from different samples - then is there a way to cancel the transition to my own function without having this stutter?
var svg = d3.select('svg'); var textElem = svg.append('text') .data(['hello world']) .attr('x', 30) .attr('y', 100) .attr('fill', '#000') .attr('id', 'a') .text(function (d) { return d; }); var textElem2 = svg.append('text') .data(['some text']) .attr('x', 230) .attr('y', 100) .attr('fill', '#000') .attr('id', 'a') .text(function (d) { return d; }); setTimeout(foo, 3000); function foo() { textElem.data(['hello world, again!']); applyTextTransitions(textElem); textElem.attr({ x: 30, y: 150 }); textElem.text(function (d) { return d; }); textElem2.data(['some more text!']) .transition() .duration(1000) .style('opacity', 0) .transition() .duration(1000) .style('opacity', 1) .attr({ x: 230, y: 150 }) .text(function (d) { return d; }); } function applyTextTransitions(element) { element .transition() .duration(1000) .style('opacity', 0) .transition() .duration(1000) .style('opacity', 1); }
source share