Skip to content

Instantly share code, notes, and snippets.

@nategood
Last active August 29, 2015 14:02
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 nategood/57541177f9eebcbde1f8 to your computer and use it in GitHub Desktop.
Save nategood/57541177f9eebcbde1f8 to your computer and use it in GitHub Desktop.
Voronoi + Clip Path Oozing
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
html, body {
margin: 0; padding: 0;
}
circle {
stroke-width: 0;
fill-opacity: .7;
}
circle:hover {
fill-opacity: 1;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var padding = 0,
width = 960; //window.innerWidth,
height = 500; //window.innerHeight;
var points = [];
var amt = 25;
for (var i = 0; i < amt; i++) {
var w = Math.floor(Math.random() * (width - padding)),
h = Math.floor(Math.random() * (height - padding));
points.push([w, h]);
}
var voronoi = d3.geom.voronoi()
.clipExtent([[padding, padding], [width - padding, height - padding]]);
var color = d3.scale.category20();
var colorIndex = function(d, i) { return color(i); };
var buildPath = function(d) { return "M" + d.join("L") + "Z"; };
var svg = d3.select("body").append("svg")
.style("background", "#fff")
.attr("width", width)
.attr("height", height);
var defs = svg.append("defs");
defs.selectAll("clipPath")
.data(voronoi(points))
.enter()
.append("clipPath")
.attr("id", function(d, i) { return "cp-" + i; })
.append("path")
.attr("d", buildPath)
.attr("transform", function(d, i) { return "translate(-" + points[i][0] + ", -" + points[i][1] + ")"});
svg.selectAll("circle")
.data(points)
.enter().append("circle")
.style("fill", colorIndex)
.attr("clip-path", function(d, i) { return "url(#cp-" + i + ")"; })
.attr("transform", function(d) { return "translate(" + d + ")"; })
.attr("r", 1)
.transition()
.ease("cubic-out")
.duration(10000)
.attr("r", 500);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment