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.
Last active
January 23, 2020 06:59
-
-
Save mbostock/7004f92cac972edef365 to your computer and use it in GitHub Desktop.
Text Transition I
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 | |
redirect: https://observablehq.com/@d3/transition-texttween |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
h1 { | |
font: 400 120px/500px "Helvetica Neue"; | |
text-align: center; | |
width: 960px; | |
height: 500px; | |
margin: 0; | |
} | |
</style> | |
<h1>0</h1> | |
<script src="//d3js.org/d3.v4.0.0-alpha.23.min.js"></script> | |
<script> | |
var format = d3.format(",d"); | |
d3.select("h1") | |
.transition() | |
.duration(2500) | |
.on("start", function repeat() { | |
d3.active(this) | |
.tween("text", function() { | |
var that = d3.select(this), | |
i = d3.interpolateNumber(that.text().replace(/,/g, ""), Math.random() * 1e6); | |
return function(t) { that.text(format(i(t))); }; | |
}) | |
.transition() | |
.delay(1500) | |
.on("start", repeat); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment