svg = {
const svg = d3
.create("svg")
.attr("width", size.width)
.attr("height", size.height)
.attr("viewBox", [0, 0, size.width, size.height])
.attr(
"style",
"max-width: 100%; height: auto; height: intrinsic;border: solid 1px"
);
const curves = svg
.append("g")
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-width", strokeWidth);
curves.append("path");
function updateCurve() {
curves.select("path").attr("d", drawCurves(d3.path(), initialPoints));
svg
.selectAll("circle")
.data(initialPoints)
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y);
}
const newCircle = null;
svg.call(
d3
.drag()
.subject((e, d) => {
const r = { x: e.x, y: e.y };
initialPoints.push(r);
return r;
})
.on("start", (e) => {
newCircle = svg
.selectAll("circle")
.data(initialPoints)
.enter()
.append("circle")
.attr("r", "5")
.attr("cx", e.x)
.attr("cy", e.y)
.call(addDrag);
updateCurve();
})
.on("drag", (e) => {
e.subject.x = e.x;
e.subject.y = e.y;
updateCurve();
})
.on("end", (e) => {
})
);
function addDrag(selection) {
selection.call(
d3
.drag()
.on("start", (e) => {})
.on("drag", function (e) {
e.subject.x = e.x;
e.subject.y = e.y;
d3.select(this)
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y);
updateCurve();
})
);
}
svg
.selectAll("circle")
.data(initialPoints)
.join("circle")
.attr("r", "5")
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y)
.call(addDrag);
updateCurve();
return svg;
}