-
-
Save AnweshGangula/ad5e2cfe7cf7b5e75fb08252830d4c1d to your computer and use it in GitHub Desktop.
Simple rain-like animation in d3, rendered in svg rather than canvas.
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> | |
<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