Carbon copy of Mike Bostock'chain transition but on canvas! Thanks to d3-canvas-transition module.
Last active
January 3, 2017 14:54
-
-
Save lsbardel/cf2f227b3fccd8e39600908f93038fcd to your computer and use it in GitHub Desktop.
Chained Canvas 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: bsd-3-clause |
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> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>d3 canvas transition</title> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="https://giottojs.org/d3-canvas-transition/0.3.3/d3-canvas-transition.js"></script> | |
</head> | |
<body> | |
<div id="paper"> | |
<label> | |
<input id='svg' name="type" type="radio" checked> | |
<span>svg</span> | |
</label> | |
<label> | |
<input id='canvas' name="type" type="radio"> | |
<span>canvas</span> | |
</label> | |
</div> | |
<div id="example" style="max-width: 960px"></div> | |
<script> | |
(function () { | |
d3.select('#svg').on('click', function () { | |
draw('svg'); | |
}); | |
d3.select('#canvas').on('click', function () { | |
draw('canvas'); | |
}); | |
if (d3.resolution() > 1) { | |
d3.select('#paper').append('label').html( | |
"<input id='canvas-low' name='type' type='radio'><span>canvas low resolution</span>" | |
); | |
d3.select('#canvas-low').on('click', function () { | |
draw('canvas', 1); | |
}); | |
} | |
draw('svg'); | |
function draw(type, r) { | |
var example = d3.select("#example"), | |
width = d3.getSize(example.style('width')), | |
height = Math.min(500, width); | |
example.select('.paper').remove(); | |
var paper = example | |
.append(type) | |
.classed('paper', true) | |
.attr('width', width).attr('height', height).canvasResolution(r).canvas(true); | |
var margin = 40; | |
width = width - 2 * margin; | |
height = height - 2 * margin; | |
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); | |
paper.append('g') | |
.attr("stroke", "black") | |
.attr("stroke-width", 1.5) | |
.attr("transform", "translate(" + margin + "," + margin + ")") | |
.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> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment