d3.forceLimit?

Open
#169 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
javascript

Research direction

Read the force implementations and simulation force registration to understand how a new force fits the existing API. The issue provides a proposed forceLimit implementation and an Observable example; compare that approach with project conventions and tests. Done means a maximum speed limit can be added as a simulation force and its behavior is covered by tests.

Written by the indexing model from the issue text.

Description

idea

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

Dominant language
JavaScript
Stars
2k
Forks
395
Avg merge
5h 46m
Merged PRs (30d)
1

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from d3/d3-force

All issues in d3/d3-force

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.