JSFiddle - React, Tailwind, and code Playground
JavaScript
var r = 200,
colors = {
a: "#ADD8E6",
b: "#FFFACD",
c: "#FA8072",
ab: "#008000",
bc: "#FF0000",
ca: "#0000FF",
abc: "#000000"
};
var board = d3.select("body").append("svg:svg").attr({
width: 3 * r,
height: 3 * r
});
function dots(x_0, y_0, r, n) {
var a = [],
d_alpha = 2 * Math.PI / n;
for (var alpha = 0; alpha < 2 * Math.PI; alpha += d_alpha) {
a.push([
x_0 + r * Math.cos(alpha),
y_0 + r * Math.sin(alpha)]);
}
return (a);
}
var shape_a = d3.geom.polygon(dots(r, r, r, 80));
var shape_b = d3.geom.polygon(dots(2 * r, r, r, 80));
var shape_c = d3.geom.polygon(dots(1.5 * r, 2 * r, r, 80));
var shape_a_x_b = shape_a.slice();
shape_b.reverse().clip(shape_a_x_b);
var shape_b_x_c = shape_c.slice();
shape_b.clip(shape_b_x_c);
var shape_c_x_a = shape_c.slice();
shape_a.reverse().clip(shape_c_x_a);
var shape_abc = shape_c_x_a.slice();
d3.geom.polygon(shape_b_x_c.reverse()).clip(shape_abc);
board.append("svg:polygon")
.attr({
points: shape_a,
fill: colors.a
});
board.append("svg:polygon")
.attr({
points: shape_b,
fill: colors.b
});
board.append("svg:polygon")
.attr({
points: shape_c,
fill: colors.c
});
board.append("svg:polygon")
.attr({
points: shape_a_x_b,
fill: colors.ab
}).on("click", function () {
alert("I am a intersection of bluish and yellowish circles");
});
board.append("svg:polygon")
.attr({
points: shape_b_x_c,
fill: colors.bc
});
board.append("svg:polygon")
.attr({
points: shape_c_x_a,
fill: colors.ca
});
board.append("svg:polygon")
.attr({
points: shape_abc,
fill: colors.abc
}).on("click", function () {
alert("I am central, the darkest, almost triangle piece");
});