d3.js - 将碰撞检测示例从 v3 转换为 v4 (D3)

标签 d3.js

我正在尝试转换碰撞检测示例 https://bl.ocks.org/mbostock/3231298

到 D3 V4。

我已经转换了原力,但我无法让它发挥作用。谁能指出我做错了什么?

谢谢

var width = window.innerWidth
    var height = window.innerHeight

    var nodes = d3.range(200).map(function() { return {radius: Math.random() * 12 + 4}; }),
        root = nodes[0];
    var color = d3.scaleOrdinal().range(d3.schemeCategory20)

    root.radius = 0;
    root.fixed = true;

    const forceX = d3.forceX(width / 2).strength(0.015)
    const forceY = d3.forceY(height / 2).strength(0.015)

    var force = d3.forceSimulation()
        .force('x', forceX)
        .force('y',  forceY)
        .force('charge', function(d, i) { return i ? 0 : -2000; })
        .on('tick', ticked)
        .nodes(nodes);

    // var force = d3.layout.force()
    //     .gravity(0.05)
    //     .charge(function(d, i) { return i ? 0 : -2000; })
    //     .nodes(nodes)
    //     .size([width, height]);


    // force.start();

    var svg = d3.select(ctr)
        .attr("width", width)
        .attr("height", height);

    svg.selectAll("circle")
        .data(nodes.slice(1))
        .enter().append("circle")
        .attr("r", function(d) { return d.radius; })
        .style("fill", function(d, i) { return color(i % 3); });

    function ticked(e) {
        var q = d3.quadtree(nodes),
            i = 0,
            n = nodes.length;

        while (++i < n) q.visit(collide(nodes[i]));

        svg.selectAll("circle")
            .attr("cx", function(d) { return d.x; })
            .attr("cy", function(d) { return d.y; });
    };

    svg.on("mousemove", function() {
        var p1 = d3.mouse(this);
        root.px = p1[0];
        root.py = p1[1];
        force.restart();
    });

    function collide(node) {
        var r = node.radius + 16,
            nx1 = node.x - r,
            nx2 = node.x + r,
            ny1 = node.y - r,
            ny2 = node.y + r;
        return function(quad, x1, y1, x2, y2) {
            if (quad.point && (quad.point !== node)) {
                var x = node.x - quad.point.x,
                    y = node.y - quad.point.y,
                    l = Math.sqrt(x * x + y * y),
                    r = node.radius + quad.point.radius;
                if (l < r) {
                    l = (l - r) / l * .5;
                    node.x -= x *= l;
                    node.y -= y *= l;
                    quad.point.x += x;
                    quad.point.y += y;
                }
            }
            return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
        };
    }

最佳答案

这是我对 SO 的第一个回答,所以任何跟随我的人,请随时将我推向正确的方向。话虽这么说,我也对此感兴趣,所以这是我的解释:

Forcesimulation 现在提供了一种称为“碰撞”的力,它应该完全取代 v3 示例中的 collide 函数:

.force("collide", d3.forceCollide().radius(function(d)
        {
            if(d === root){
                return Math.random() * 50 + 100;
            }
            return d.r + 0.5;
        }).iterations(5))

所以,整个模拟现在看起来像:

    var simulation = d3.forceSimulation()
        .velocityDecay(0.2)
        .force("x", d3.forceX(width / 2).strength(0.015))
        .force("y", d3.forceY(height / 2).strength(0.015))
        .force("collide", d3.forceCollide().radius(function(d)
        {
            if(d === root){
                return Math.random() * 50 + 100;
            }
            return d.r + 0.5;
        }).iterations(5))
        .nodes(nodes);

还有一些其他的小东西你遗漏了,比如用以下方法固定第一个节点(我们的鼠标控制的节点)的位置:

        var p1 = d3.mouse(this);
        root.fx = p1[0];
        root.fy = p1[1];

工作示例:https://jsfiddle.net/akLhhgzr/

关于d3.js - 将碰撞检测示例从 v3 转换为 v4 (D3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44055869/

相关文章:

javascript - 如何模拟对 d3 树节点的点击?

javascript - 使用 D3/JavaScript 在不停止动画的情况下对 SVG 中的动画 GIF 进行排序

javascript - 如何在 D3.js 中鼠标悬停时更改饼图的其他部分?

html - d3 节点标记

javascript - d3 : Adding and removing force. 基于 slider 值的节点

d3.js - 如何在 D3.js 中仅使用 2 个坐标绘制矩形

javascript - 连接直方图顶部的简单折线图

d3.js - 如何使用 d3.format 获取可本地化或可自定义的 si 代码

javascript - D3 工具提示错误 : "Cannot read property ' - 1' of undefined"

javascript - 如何使用 d3.js 制作正确的倒置堆叠图?