Skip to content

Instantly share code, notes, and snippets.

@romsson
Last active November 15, 2018 14:01
Show Gist options
  • Save romsson/d0fea7a618adb89bf4dced999a8b9a16 to your computer and use it in GitHub Desktop.
Save romsson/d0fea7a618adb89bf4dced999a8b9a16 to your computer and use it in GitHub Desktop.
[grid] overlapping circles + clipping
license: mit
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: Helvetica;
font-size: 20px;
}
circle {
fill: none;
stroke: white;
stroke-width: 1;
}
</style>
<body>
<script src="http://d3js.org/d3.v4.js"></script>
<script src="http://romsson.github.io/d3-gridding/build/d3-gridding.js"></script>
<script>
var width = 400,
height = 400,
offset = 50;
// params to play with
var n = 50,
m = 5,
data = d3.range(n);
var svg = d3.select("body")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + offset + ", " + offset + ")");
var params = {
"size": [width, height],
"offset": [0, 0],
"mode": "grid"
};
var gridding = d3.gridding()
.params(params);
var griddingData = gridding(data);
var w = griddingData[0].width;
var h = griddingData[0].height;
var defs = svg.append("defs").selectAll("clipPath")
.data(griddingData)
.enter()
.append("clipPath")
.attr("id", function(d, i) { return "clip_" + i; })
.append("rect")
.attr("width", function(d) { return d.width; })
.attr("height", function(d) { return d.height; });
var g = svg.selectAll("g")
.data(griddingData)
.enter()
.append("g")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")";
})
.attr("clip-path", function(d, i) { return "url(#clip_" + i + ")" })
var circles = g.selectAll("#circle")
.data(d3.range(m).sort(d3.descending))
.enter().append("circle")
.attr("class", "circle")
.attr("cx", w)
.attr("cy", h)
.style("fill", function(d) {
if(Math.random() > .4)
return "black";
else
return "white";
})
.attr("id", "circle")
.attr("r", function(d) { return w/4 + 1 * (d / (2*m)) * w; })
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment