viewof state = {
const context = DOM.context2d(width, height),
path = d3.geoPath().context(context);
function draw(polygon) {
context.clearRect(0, 0, width, height);
context.beginPath();
path({
type: "LineString",
coordinates: polygon
});
context.fillStyle = "rgba(0,0,0,.1)";
context.fill("evenodd");
context.lineWidth = 1.5;
context.stroke();
const selected = polygon.length > 2 ? [] : data;
for (const d of data) {
const contains =
polygon.length > 2 &&
d3.polygonContains(polygon, d) &&
selected.push(d);
}
context.beginPath();
path.pointRadius(1.5)({ type: "MultiPoint", coordinates: data });
context.fillStyle = "#000";
context.fill();
if (polygon.length > 2) {
context.beginPath();
path.pointRadius(2.5)({ type: "MultiPoint", coordinates: selected });
context.fillStyle = "red";
context.fill();
}
context.canvas.value = { polygon, selected };
context.canvas.dispatchEvent(new CustomEvent('input'));
}
draw(defaultLasso);
return d3
.select(context.canvas)
.call(lasso().on("start lasso end", draw))
.node();
}