network = () => {
const svg = d3.select(DOM.svg(width, height)).style("background", "#121212");
const g = svg.append("g").attr("transform", "translate(100,50)");
const colorInter = d3.interpolateCubehelix("#000", "#EDF5EE");
const diagram = delaunay.voronoi([0, 0, width, width / 1.6]);
const links = [];
const { halfedges, triangles, hull } = delaunay;
for (let i = 0, n = halfedges.length; i < n; ++i) {
const j = halfedges[i];
if (j < i) continue;
const ti = triangles[i] * 2;
const tj = triangles[j] * 2;
links.push({ source: nodes[ti / 2], target: nodes[tj / 2] });
}
let node = hull;
links.push({ source: nodes[node.i], target: nodes[node.next.i] });
while (((node = node.next), node !== hull))
links.push({ source: nodes[node.i], target: nodes[node.next.i] });
links.forEach((link) => {
link.defaultColor = "#333";
const dx = link.source[0] - link.target[0];
const dy = link.source[1] - link.target[1];
link.distance = Math.sqrt(dx * dx + dy * dy);
});
nodes.forEach((pt) => {
pt.edges = links.filter((link) => pt === link.source || pt === link.target);
});
const edges = g
.selectAll("path.link")
.data(links)
.enter()
.append("path")
.attr("stroke-width", "1")
.style("shape-rendering", "crisp-edges")
.attr("stroke-opacity", (d) => {
return (
1 -
Math.sqrt(
Math.pow(d.source[0] - d.target[0], 2) +
Math.pow(d.source[1] - d.target[1], 2)
) /
200
);
})
.attr("stroke", (d) => {
var mid = (d.source[1] + d.target[1]) / 2;
console.log("mid je", mid, height);
return color(mid / height);
})
.attr("d", (d) => `M${d.source} L${d.target}`);
const circles = g
.selectAll("circle")
.data(nodes)
.enter()
.append("circle")
.attr("r", (d) => Math.random() * 3.5)
.attr("opacity", (d) => opacityRand())
.attr("fill", (d, i) => {
return color(d[1] / height);
})
.attr("cx", (d) => d[0])
.attr("cy", (d) => d[1]);
return svg.node();
}