Last active
August 29, 2015 14:02
-
-
Save nategood/57541177f9eebcbde1f8 to your computer and use it in GitHub Desktop.
Voronoi + Clip Path Oozing
This file contains 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> | |
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