npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

d3-manybody-wasm

v0.1.1

Published

WebAssembly C++ implementation of D3's many-body, X and Y forces

Downloads

9

Readme

d3-manybody-wasm

This module provides WebAssembly-powered implementations of force algorithms used in d3-force: forceManyBody(), forceX(), and forceY(). By leveraging WebAssembly, these implementations can significantly improve the performance of force-directed graph layouts, especially for large graphs, without compromising on layout quality.

This is done through a C++ implementation, which is then compiled to WebAssembly. This allows for faster execution of the force calculations, particularly on larger datasets where JavaScript performance may become a bottleneck.

Installing

If you use NPM, npm install d3-manybody-wasm. Otherwise, clone latest version.

Usage

Before using any of the force functions, you must ensure that the WebAssembly module is initialized. Use the ensureInitialized() function to wait for the initialization to complete:

import { ensureInitialized, forceManyBody, forceX, forceY } from 'd3-manybody-wasm';

ensureInitialized().then(() => {
  const simulation = d3.forceSimulation(nodes)
    .force("link", d3.forceLink().id(d => d.id))
    .force("charge", forceManyBody())
    .force("x", forceX())
    .force("y", forceY());

  // ... rest of your simulation code
});

Aside from that, The forceManyBody(), forceX(), and forceY() functions are designed as drop-in replacements for their d3-force counterparts, using the same API.

API Reference

Many-Body Force

# d3.forceManyBody()

Creates a new many-body force with the default parameters. This force can be used to simulate gravity (attraction) or electrostatic charge (repulsion).

# manyBody.strength([strength])

If strength is specified, sets the strength accessor to the specified number or function and returns this force. If strength is not specified, returns the current strength accessor.

# manyBody.theta([theta])

If theta is specified, sets the Barnes–Hut approximation criterion to the specified number and returns this force. If theta is not specified, returns the current value.

# manyBody.distanceMin([distance])

If distance is specified, sets the minimum distance between nodes and returns this force. If distance is not specified, returns the current minimum distance.

# manyBody.distanceMax([distance])

If distance is specified, sets the maximum distance between nodes and returns this force. If distance is not specified, returns the current maximum distance.

Position Forces

# d3.forceX([x])

Creates a new position force along the x-axis towards the given position x.

# d3.forceY([y])

Creates a new position force along the y-axis towards the given position y.

Both position forces share the following methods:

# force.strength([strength])

If strength is specified, sets the strength accessor to the specified number or function and returns this force. If strength is not specified, returns the current strength accessor.

# force.x([x])

If x is specified, sets the x-coordinate accessor to the specified number or function and returns this force. If x is not specified, returns the current x-coordinate accessor.

# force.y([y])

If y is specified, sets the y-coordinate accessor to the specified number or function and returns this force. If y is not specified, returns the current y-coordinate accessor.

Performance

The WebAssembly implementation can provide decent performance improvements, especially for larger graphs. However, the exact performance gain may vary depending on the specific use case and hardware. I encourage you to benchmark this implementation against the standard D3 force implementation and see for yourself – in particular, data copying between C++ and JavaScript can be a bottleneck. Setting up shared memory and hacking the package a bit to accommodate it might be a good idea if you're looking to optimize further.

License

This project is licensed under the MIT License - see the LICENSE file for details.