The code for what you want to do is actually much easier than you think. You can do everything in your Sweep
function, including setting the x
attribute to 0:
function Sweep() {
d3.select(this).attr("x", 0)
.transition().ease("linear").duration(sweepDuration)
.attr("x", function(){ return d3.select(this).attr("width") < width ? 0 : width; })
.attr("width", function () { return d3.select(this).attr("width") < width ? width : 0; })
.each("end", Sweep);
}
Complete demo here.