viewof polygonDrawing = {
const svg = d3.select(DOM.svg(width, 350));
let startPolygon = svg.append("g")
.selectAll("path")
.data([points])
.enter().append("path")
.attr("d", d => line(d))
.style("stroke", "grey")
.style("fill", "none")
.style("stroke-width", "2");
let cleanedPolygon = svg.append("g")
.attr("transform", "translate(400,0)")
.selectAll("path")
.data([resampleSegments(cleanPoints(points))])
.enter().append("path")
.attr("d", d => lineSmoothed(d))
.style("stroke", "grey")
.style("fill", "#eee")
.style("stroke-width", "4");
let cleanedPoints = svg.append("g")
.attr("transform", "translate(400,0)")
.selectAll("circle")
.data(cleanPoints(points))
.enter().append("circle")
.attr("cx", d => d[0])
.attr("cy", d => d[1])
.attr("r", 2)
.style("fill", "red");
let resampledPoints = svg.append("g")
.attr("pointer-events", "none")
.selectAll("circle")
.data(resampleSegments(points))
.enter().append("circle")
.attr("cx", d => d[0])
.attr("cy", d => d[1])
.attr("r", 3.5)
.style("fill", "purple");
let startPoints = svg.append("g").selectAll("circle")
.data(points)
.enter().append("circle")
.attr("cx", d => d[0])
.attr("cy", d => d[1])
.attr("r", 5)
.style("fill", "red")
.call(d3.drag()
.subject(([x, y]) => ({x, y}))
.on("drag", dragged));
update();
function dragged(d) {
d[0] = d3.event.x;
d[1] = d3.event.y;
update();
}
function update() {
startPolygon.attr("d", d => line(d));
cleanedPolygon.data([resampleSegments(cleanPoints(points))])
.attr("d", d => lineSmoothed(d));
resampledPoints.data(resampleSegments(points))
.attr("cx", d => d[0])
.attr("cy", d => d[1]);
cleanedPoints.data(cleanPoints(points))
.attr("cx", d => d[0])
.attr("cy", d => d[1])
startPoints.attr("cx", d => d[0])
.attr("cy", d => d[1]);
}
return svg.node();
}