Skip to content

Instantly share code, notes, and snippets.

Last active Jan 23, 2020
What would you like to do?
Text Transition I
license: gpl-3.0

A demonstration of using transition.tween to interpolate text. The transition.text method doesn’t do this by default because it’s often distracting; instead, it just sets the new text value when the transition starts. Another way to transition text is to cross-fade.

<!DOCTYPE html>
<meta charset="utf-8">
h1 {
font: 400 120px/500px "Helvetica Neue";
text-align: center;
width: 960px;
height: 500px;
margin: 0;
<script src="//"></script>
var format = d3.format(",d");"h1")
.on("start", function repeat() {
.tween("text", function() {
var that =,
i = d3.interpolateNumber(that.text().replace(/,/g, ""), Math.random() * 1e6);
return function(t) { that.text(format(i(t))); };
.on("start", repeat);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment