block by almccon faff15ba671b430040e7b2363b4242dc

Chained Transitions II

Full Screen

This animation demonstrates the use of d3.active to create chained transitions in D3 4.0.

forked from mbostock‘s block: Chained Transitions II

index.html

<!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([1, 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>