viewof state = {
const selection = d3.create("svg").attr("viewBox", [0, 0, width, height]),
svg = selection.node(),
path = d3.geoPath(),
l = selection.append("path").attr("class", "lasso"),
g = selection.append("g"),
points = g
.selectAll("circle")
.data(data)
.join("circle")
.attr("r", 1.5)
.attr("transform", d => `translate(${d})`);
selection.append("defs").append("style").text(`
.selected {r: 2.5; fill: red}
.lasso { fill-rule: evenodd; fill-opacity: 0.1; stroke-width: 1.5; stroke: #000; }
`);
function draw(polygon) {
l.datum({
type: "LineString",
coordinates: polygon
}).attr("d", path);
const selected = polygon.length > 2 ? [] : data;
points.classed(
"selected",
polygon.length > 2
? d => d3.polygonContains(polygon, d) && selected.push(d)
: false
);
svg.value = { polygon, selected };
svg.dispatchEvent(new CustomEvent('input'));
}
selection.call(lasso().on("start lasso end", draw));
draw(defaultLasso);
return svg;
}