Skip to content

Instantly share code, notes, and snippets.

@mbostock
Last active Feb 9, 2016
Embed
What would you like to do?
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