Skip to content

Instantly share code, notes, and snippets.

@rainzoo
Forked from mbostock/.block
Created August 10, 2012 13:51
Show Gist options
  • Save rainzoo/3314340 to your computer and use it in GitHub Desktop.
Save rainzoo/3314340 to your computer and use it in GitHub Desktop.
Circular Layout (Raindrops)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Raindrops</title>
<script type="text/javascript" src="https://github.com/mbostock/d3/raw/v1.4.0/d3.js"></script>
<style type="text/css">
body {
background: #012;
}
path {
stroke: #fff;
stroke-opacity: .5;
}
</style>
</head>
<body>
<script type="text/javascript">
var w = 960,
h = 500;
var svg = d3.select("body")
.append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(" + w / 2 + "," + h / 2 + ")");
var gradient = svg.append("svg:defs")
.append("svg:linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "20%")
.attr("x2", "20%")
.attr("y2", "100%");
gradient.append("svg:stop")
.attr("offset", "20%")
.attr("stop-color", "#ccf");
gradient.append("svg:stop")
.attr("offset", "50%")
.attr("stop-color", "#1C425C");
gradient.append("svg:stop")
.attr("offset", "100%")
.attr("stop-color", "#19162B");
// could use transparent gradient overlay to vary raindrop color
svg.selectAll("path")
.data(d3.range(0, 358, 1))
.enter().append("svg:path")
.attr("fill", "url(#gradient)")
.attr("d", function() { return raindrop(10 + Math.random() * 200); })
.attr("transform", function(d) {
return "rotate(" + d + ")"
+ "translate(" + (h / 4 + Math.random() * h / 6) + ",0)"
+ "rotate(90)";
});
// size is linearly proportional to square pixels (not exact, yet)
function raindrop(size) {
var r = Math.sqrt(size / Math.PI);
return "M" + r + ",0"
+ "A" + r + "," + r + " 0 1,1 " + -r + ",0"
+ "C" + -r + "," + -r + " 0," + -r + " 0," + -3*r
+ "C0," + -r + " " + r + "," + -r + " " + r + ",0"
+ "Z";
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment