This animation demonstrates the use of transition.transition and d3.active to create chained transitions. Chained transitions inherit the reference time of the original transition, guaranteeing synchronicity.
Last active
May 3, 2019 07:49
-
-
Save mbostock/1125997 to your computer and use it in GitHub Desktop.
Chained Transitions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
circle { | |
fill: #000; | |
stroke: #000; | |
stroke-width: 1.5px; | |
} | |
</style> | |
<svg width="960" height="500"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var svg = d3.select("svg"), | |
margin = {top: 40, right: 40, bottom: 40, left: 40}, | |
width = svg.attr("width") - margin.left - margin.right, | |
height = svg.attr("height") - margin.top - margin.bottom, | |
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
var y = d3.scalePoint() | |
.domain(d3.range(50)) | |
.range([0, height]); | |
var z = d3.scaleLinear() | |
.domain([10, 0]) | |
.range(["hsl(62,100%,90%)", "hsl(228,30%,20%)"]) | |
.interpolate(d3.interpolateHcl); | |
g.selectAll("circle") | |
.data(y.domain()) | |
.enter().append("circle") | |
.attr("r", 25) | |
.attr("cx", 0) | |
.attr("cy", y) | |
.style("fill", function(d) { return z(Math.abs(d % 20 - 10)); }) | |
.transition() | |
.duration(2500) | |
.delay(function(d) { return d * 40; }) | |
.on("start", function repeat() { | |
d3.active(this) | |
.attr("cx", width) | |
.transition() | |
.attr("cx", 0) | |
.transition() | |
.on("start", repeat); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment