This animation demonstrates the use of d3.active to create chained transitions in D3 4.0.
Created
March 8, 2016 23:11
-
-
Save mbostock/346f4d967650b27c0511 to your computer and use it in GitHub Desktop.
Chained Transitions II
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> | |
<body> | |
<script src="//d3js.org/d3.v4.0.0-alpha.28.min.js"></script> | |
<script> | |
var margin = {top: 40, right: 40, bottom: 40, left: 40}, | |
width = 960 - margin.left - margin.right, | |
height = 500 - margin.top - margin.bottom; | |
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); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
svg.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", slide); | |
function slide() { | |
d3.active(this) | |
.attr("cx", width) | |
.transition() | |
.attr("cx", 0) | |
.transition() | |
.on("start", slide); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment