block by pbogden 6fce2cb597dfc92c6e52bcea2a73cd05

tweens

Full Screen

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

index.html

<!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>