I have successfully implemented some code from this D3 example of animated line drawing but I'm none the wiser as to how it works. I would really appreciate a line by line breakdown of how the code works. I'm relatively new to D3; I understand the basics of DOM manipulation with the library, but I feel this requires an intermediate level of understanding which I lack.
I have included below the specific part of the example which I have reengineered. I suspect the clever part is tied up in the .attr("stroke-dashoffset", 0);
& .attr("stroke-dashoffset", totalLength);
parts so a clear explanation of how these work and how they contribute to the effect would be very appreciated.
var totalLength = path.node().getTotalLength();
path
.attr("stroke-dasharray", totalLength + " " + totalLength)
.attr("stroke-dashoffset", totalLength)
.transition()
.duration(2000)
.ease("linear")
.attr("stroke-dashoffset", 0);
svg.on("click", function(){
path
.transition()
.duration(2000)
.ease("linear")
.attr("stroke-dashoffset", totalLength);
})
Many thanks in advance