在我的示例中,我想设置所有圆不仅相互碰撞,而且还与圆弧碰撞,我该怎么做,不胜感激。
var simulation = d3.forceSimulation(nodes)
.force('charge', d3.forceManyBody().strength(5))
.force('x', d3.forceX().x((d) => {
return centerGroup[topicArrayList.indexOf(d.topic)][0] * 0.9
}))
.force('y', d3.forceY().y((d) => {
return centerGroup[topicArrayList.indexOf(d.topic)][1] * 0.9
}))
.force('collision', d3.forceCollide().radius((d) => {
return d.radius;
}))
.on('tick', ticked);
function ticked() {
var u = d3.select('svg g')
.selectAll('circle')
.data(nodes);
u.enter()
.append('circle')
.attr('r', function(d) {
return d.radius;
})
.merge(u)
.attr('cx', function(d) {
return d.x;
})
.attr('cy', function(d) {
return d.y;
})
u.exit().remove();
}
最佳答案
您可以使用 Mike Bostock's Bounded Force Layout 建议的技巧:更新 .ticked()
中节点的位置时,添加检查以确保没有将某些内容放置在边界之外。但是,需要做一些数学运算才能使边界成为有厚度的圆弧而不是没有厚度的矩形。
关于javascript - 如何在D3.js中设置节点和其他svg元素之间的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45664885/