Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active February 9, 2016 01:30
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 mbostock/3305854 to your computer and use it in GitHub Desktop.
Save mbostock/3305854 to your computer and use it in GitHub Desktop.
Custom Tweens
license: gpl-3.0

Use transition.attrTween to customize interpolation during a transition. For example, the default transform interpolation from "rotate(0)" to "rotate(720)" has no effect because 0º and 720º are equivalent; by changing the interpolator to d3.interpolateString, you can animate the rotation.

symbol.transition()
    .attrTween("transform", function() {
      return d3.interpolateString("rotate(0)", "rotate(720)");
    });
<!DOCTYPE html>
<meta charset="utf-8">
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var width = 960,
height = 500;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
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() {
d3.select(this).transition()
.duration(10000)
.attrTween("transform", function() { return d3.interpolateString("rotate(0)", "rotate(720)"); })
.each("end", cycle);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment