Skip to content

Instantly share code, notes, and snippets.

@AnweshGangula
Forked from nickjevershed/index.html
Created June 9, 2023 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnweshGangula/ad5e2cfe7cf7b5e75fb08252830d4c1d to your computer and use it in GitHub Desktop.
Save AnweshGangula/ad5e2cfe7cf7b5e75fb08252830d4c1d to your computer and use it in GitHub Desktop.
Simple rain-like animation in d3, rendered in svg rather than canvas.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
#graphicContainer {
width: 400px;
background:#F2F2F2;
}
</style>
</head>
<body>
<div id="outer-wrapper">
<div id="graphicContainer">
</div>
</div>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var width = 400;
var height = 200;
var graphic;
var dropsPerSec = 2;
var windStrength = 0;
graphic = d3.select("#graphicContainer").append("svg")
.attr("width", width)
.attr("height", height)
.attr("id", "graphic")
.attr("overflow", "hidden");
function makeRain() {
for (var i = 0; i < dropsPerSec; i++) {
startX = Math.random() * width,
startY = Math.random() * 100 - 100,
endX = startX;
endY = height + 200;
//console.log("startX", startX, "startY", startY);
graphic.insert("circle")
.attr("cx", startX)
.attr("cy", startY)
.attr("r", 1)
.style("fill","steelblue")
.transition()
.duration(2000)
.attr("cx", endX)
.attr("cy", endY)
.remove();
};
}
d3.timer(makeRain, 100);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment