Skip to content

Instantly share code, notes, and snippets.

Forked from scameron/index.html
Last active February 9, 2016 01:27
Show Gist options
  • Save mbostock/3151228 to your computer and use it in GitHub Desktop.
Save mbostock/3151228 to your computer and use it in GitHub Desktop.
Animated textPath
license: gpl-3.0

This example demonstrates how to animate a path and a textPath at the same time. I wouldn’t use this technique—the text is scrunched up at the beginning of the arc at the beginning of the animation. Instead, I would use the arc to clip the text, so that the text is revealed as the arc expands.

This example also demonstrates two unfortunate bugs in WebKit browsers:

  • You can’t select textPath elements because of a bug in how WebKit handles case-sensitive element names. See bug 83438. WebKit shows no sign of fixing this bug, unfortunately.

  • Updating a path element by itself does not trigger redraw on dependent elements that reference this path. So, even though we update the path, the referencing textPath element will not be redrawn. To workaround this update bug, we create a custom transition that repeatedly sets the textPath’s xlink:href attribute to “#path”.

See related Stack Overflow question.

<!DOCTYPE html>
<meta charset="utf-8">
path {
fill: #3182bd;
text {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 24px;
<script src="//"></script>
var arc = d3.svg.arc()
.endAngle(function(t) { return t * 2 * Math.PI / 3; });
var svg ="body").append("svg")
.attr("width", 960)
.attr("height", 500)
.attr("transform", "translate(480,250)");
.attr("id", "path")
.attr("d", arc(1));
.attr("x", 8)
.attr("dy", 28)
.attr("class", "textpath")
.attr("xlink:href", "#path")
.text("Hello, curved textPath!");
var transition = svg.transition()
.attrTween("d", function() { return arc; });
.attrTween("xlink:href", function() { return function() { return "#path"; }; });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment