Last active
June 27, 2019 17:00
-
-
Save mbostock/ad550c9d6d156ac726b45f48fa6ff2c7 to your computer and use it in GitHub Desktop.
I Make Circles
This file contains hidden or 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 hidden or 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> | |
<style> | |
button { | |
position: absolute; | |
top: 10px; | |
left: 10px; | |
} | |
circle { | |
fill: none; | |
stroke: #000; | |
stroke-width: 1.5px; | |
} | |
</style> | |
<button>Click Me</button> | |
<svg width="960" height="500"></svg> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script> | |
var svg = d3.select("svg"), | |
margin = {top: 30, right: 30, bottom: 30, left: 30}, | |
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 x = d3.scaleTime() | |
.range([0, width]); | |
var xAxis = d3.axisBottom(x); | |
var xAxisG = g.append("g") | |
.attr("transform", "translate(0, " + height + ")"); | |
d3.timer(function() { | |
var now = Date.now(); | |
x.domain([now - 5000, now]); | |
xAxisG.call(xAxis); | |
}); | |
d3.select("button").on("click", function() { | |
var time = Date.now(); | |
var circle = g.append("circle") | |
.attr("r", 80) | |
.attr("stroke-opacity", 0) | |
.attr("cy", Math.random() * height); | |
circle.transition("time") | |
.duration(5000) | |
.ease(d3.easeLinear) | |
.attrTween("cx", function(d) { return function(t) { return x(time); }; }) | |
circle.transition() | |
.duration(750) | |
.ease(d3.easeCubicOut) | |
.attr("r", 3.5) | |
.attr("stroke-opacity", 1) | |
.transition() | |
.delay(5000 - 750 * 2) | |
.ease(d3.easeCubicIn) | |
.attr("r", 80) | |
.attr("stroke-opacity", 0) | |
.remove(); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment