Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d3.forceLimit? #169

Open
mbostock opened this issue Jul 9, 2020 · 0 comments
Open

d3.forceLimit? #169

mbostock opened this issue Jul 9, 2020 · 0 comments
Labels

Comments

@mbostock
Copy link
Member

mbostock commented Jul 9, 2020

A way to limit the maximum instantaneous force (speed) of nodes.

function forceLimit(limit) {
  let nodes;

  function force() {
    for (const node of nodes) {
      const speed = Math.hypot(node.vx, node.vy);
      if (speed > limit) {
        node.vx = node.vx / speed * limit;
        node.vy = node.vy / speed * limit;
      }
    }
  }

  force.limit = function(_) {
    return arguments.length ? (limit = +_, force) : limit;
  };

  force.initialize = function (_) {
    nodes = _;
  };

  return force;
}

To use:

simulation.force("limit", forceLimit(8));

https://observablehq.com/d/1849551f209b16ed

@Fil Fil added the idea label Sep 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants