Skip to content

Instantly share code, notes, and snippets.

@pbogden
Last active April 26, 2016 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbogden/6fce2cb597dfc92c6e52bcea2a73cd05 to your computer and use it in GitHub Desktop.
Save pbogden/6fce2cb597dfc92c6e52bcea2a73cd05 to your computer and use it in GitHub Desktop.
tweens

Using tweens with transitions: Custom tweens with a custom attribute.

<!DOCTYPE html>
<meta charset="utf-8">
<title>tween</title>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>d3 || document.write('<script src="../d3.min.js"><\/script>')</script>
<script>
var width = 960,
height = 500;
var n = 0;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var info = d3.select("body").append("div")
.style({ position: "absolute", top: "50px", left: "50px",
"font-family": "sans-serif", "font-size": "30px" })
svg.append("g")
.attr("transform", "translate(" + (width / 2) + "," + (height / 2) + ")")
.append("path")
.attr("d", d3.svg.symbol().type("cross").size(10000))
.each(cycle);
function cycle() {
n++; // increment cycle counter
d3.select(this).transition()
.duration(10000)
.attrTween("my-tween", myTween)
.attrTween("transform", function() { return d3.interpolateString("rotate(0)", "rotate(720)"); })
.each("end", cycle);
}
function myTween() {
// i increments every time the tween is called, from wherever it's called
var i = 0;
// return the tween
return function(t) { info.html(n + "<br>" + i++ + '<br>tween t: ' + d3.format(".3f")(t)) };
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment