{
const svgBackgroundColor = "#152c33",
height = 400,
width = 400,
numPoints = 16,
svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.style("background-color", svgBackgroundColor),
container = svg.append("g")
.attr("transform", `translate(${100},${100})`),
radius1 = 80,
radius2 = 81;
let points = getCirclePoints({x: 100, y: 100}, radius1, numPoints);
function drawZigZag(points){
let t = d3.transition()
.duration(3000);
let t2 = d3.transition()
.duration(2000)
.delay(2000);
points.push(points[0])
let zigs = container.selectAll("path")
.data([points], d => d);
zigs
.join(
enter => enter.append("path")
.attr("d", d => d3.line()
.x(d => d.x)
.y(d => d.y)(d))
.attr("stroke", "#f72585")
.attr("fill", "#560bad")
.attr("stroke-width", 5)
.style("opacity", 0.7)
.call(e => e.transition(t)
.attr("d", d => {
if(!points[0].x2){
let pts2 = updatePoints(points, width);
return d3.line().x(d => d.x2).y(d => d.y2)(pts2);
}else{
return d3.line().x(d => d.x2).y(d => d.y2)(d);
}
})
.attr("fill", "#f72585")
)
.call(e => e.transition(t2)
.attr("d", d => d3.line()
.x(d => d.x)
.y(d => d.y)(d))
.attr("stroke", "#f72585")
.attr("fill", "#560bad")
.attr("stroke-width", 5)
.style("opacity", 0.7)
)
)
}
function reRun(){
d3.selectAll("path").remove();
drawZigZag(points);
}
d3.select("button#runAnimation").on("click", reRun);
yield svg.node();
drawZigZag(points);
}