The best way is indeed to center the text yourself instead of using and animating text-anchor
. This is relatively easy, you just have to determine the width of the text and offset it by half that length to center:
.attr("dx", function() {
if(opts.textAnchor == "start") {
return 0;
} else if(opts.textAnchor == "middle") {
return -this.getBBox().width/2 + "px";
}
})
Complete demo here.