Skip to content

Instantly share code, notes, and snippets.

@ErikOnBike
Last active September 30, 2018 14:46
Show Gist options
  • Save ErikOnBike/5caa92c3e7123fae2dad461ffa22c409 to your computer and use it in GitHub Desktop.
Save ErikOnBike/5caa92c3e7123fae2dad461ffa22c409 to your computer and use it in GitHub Desktop.
Text Tween
license: gpl-3.0

This example is a rewrite of the Text Transition I example from Mike Bostock to show the usage of a tween data function.

<!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>{{tween:textTween(d, this)}}</h1>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/d3-template-plugin/build/d3-template.min.js"></script>
<script>
var format = d3.format(",d");
// Create tween function
function textTween(d, node) {
var i = d3.interpolateNumber(d3.select(node).text().replace(/,/g, ""), d);
return function(t) { return format(i(t)); };
}
d3.select("h1")
.template()
.render(0)
;
function repeat() {
d3.select("h1")
.transition()
.duration(2500)
.render(Math.random() * 1e6)
.on("end", repeat)
}
repeat();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment