{
const context = DOM.context2d(width, height);
function drawTree(t) {
context.strokeRect(t.minX, t.minY, t.maxX - t.minX, t.maxY - t.minY);
context.lineWidth /= 1.5;
if (context.lineWidth > .08) if (t.children) t.children.forEach(drawTree);
context.lineWidth *= 1.5;
}
function draw(x, y) {
context.clearRect(0, 0, width, height);
context.lineWidth = .5;
if (debug_tree) {
context.strokeStyle = "orange";
drawTree(tree.data);
context.strokeStyle = "black";
}
context.lineWidth = .25;
context.beginPath();
if (lines.length <= 10000) {
for (const p of lines) {
context.moveTo(p.x1, p.y1);
context.lineTo(p.x2, p.y2);
}
}
context.strokeStyle = "steelblue";
context.stroke();
const { line, contact, distance, candidates } = searchOne(x, y);
if (debug_candidates) {
context.lineWidth = 1;
context.beginPath();
context.strokeStyle = "black";
for (const p of candidates) {
context.moveTo(p.x1, p.y1);
context.lineTo(p.x2, p.y2);
}
context.stroke();
}
context.lineWidth = 3;
context.strokeStyle = "red";
context.beginPath();
context.moveTo(line.x1, line.y1);
context.lineTo(line.x2, line.y2);
context.stroke();
context.strokeStyle = "black";
context.lineWidth = .5;
if (contact) {
context.beginPath();
context.moveTo(x + distance, y);
context.arc(x, y, distance, 0, 2 * Math.PI);
context.stroke();
context.beginPath();
context.moveTo(...contact);
context.arc(...contact, 3, 0, 2 * Math.PI);
context.fillStyle = "red";
context.fill();
}
}
draw(300, 200);
d3.select(context.canvas)
.style("cursor", "crosshair")
.on("mousemove click", function() {
draw(...d3.mouse(this));
});
return context.canvas;
}