Skip to content

Instantly share code, notes, and snippets.

@sarah37
Last active April 30, 2018 20:52
Show Gist options
  • Select an option

  • Save sarah37/b89da244e08421ce337b9569dee33897 to your computer and use it in GitHub Desktop.

Select an option

Save sarah37/b89da244e08421ce337b9569dee33897 to your computer and use it in GitHub Desktop.
Raindrops II
height: 960
var w = 960;
var h = 960;
var mycolours = d3.scaleOrdinal()
.domain([0,1,2,3,4,5])
.range(["#566270", //dark grey
"#8395ad", // greyish blue
"#e0e3da", // light greyish
"#c2e1fa", // light blue
])
// create svg
var svg = d3.select("#svgDiv")
.append("svg")
.attr("width", w)
.attr("height", h);
var locations = []
for (var i = 0; i < 4 ; i++) {
for (var j = 0; j < 4; j++) {
locations.push([i*200+180,j*200+180])
}
}
setInterval(function() {
var dot = locations[Math.floor(Math.random() * locations.length)]
svg
.append("circle")
.attr("cx", function(d) {return dot[0]})
.attr("cy", function(d) {return dot[1]})
.attr("r", 0)
.style("fill", mycolours(Math.floor(Math.random() * 5)))
.style("opacity", 1)
.transition()
.duration(3500)
.ease(d3.easeQuadOut)
.attr("r", 100)
.style("opacity", 0)
.remove()
}, 10)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Raindrops II</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://d3js.org/d3.v5.min.js"></script>
</head>
<body>
<div id="svgDiv"></div>
<script type="text/javascript" src="circles.js"></script>
</body>
</html>
body, html {
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #fff;
}
#svgDiv {
position: absolute;
left: 0;
right: 0;
margin: 0;
padding: 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment