viewof display = {
const nodes = [];
let mouse = null;
const svg = d3.create("svg")
.property("value",{nodes: []})
.attr("viewBox",[0, -height, width, height]);
svg.append("g")
.call(xAxis);
svg.append("g")
.call(yAxis);
let node = svg.append("g")
.selectAll("circle");
const simulation = d3.forceSimulation(nodes)
.force("test",constantForce)
.alphaDecay([0])
.alphaTarget([0])
.velocityDecay([0.0])
.on("tick", ticked);
function constantForce(){
for (var i = 0, n = nodes.length, node, accely = -ay/10, accelx = ax/10; i<n; ++ i){
node = nodes[i];
node.vx += accelx;
node.vy += accely;
}
}
function ticked() {
node.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y-25)
svg.insert('circle')
.attr('cx',node.attr('cx'))
.attr('cy',node.attr('cy'))
.attr('r',5)
.style('fill','orange')
.style('opacity', 0.4);
svg.insert('arrow');
}
function spawn(initial){
nodes.push(initial);
node = node
.data(nodes).join(
enter => enter.append("circle").attr("r", 0)
.call(enter => enter.transition().attr("r", 5)),
update => update,
exit => exit.remove()
);
simulation.nodes(nodes);
simulation.alpha(1).restart();
svg.property("value", {
nodes: nodes.map(d => ({id: d.index})),
});
svg.dispatch("input");
}
spawn({x:xi*10 , y:-yi*10,vx:vxi,vy:-vyi});
invalidation.then(() => simulation.stop());
return svg.node();
}