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

ngraph.asyncforce

v1.0.0

Published

Force based graph layout with web workers

Downloads

1,568

Readme

ngraph.asyncforce

Force based graph layout with web workers. This module provides async layout only. You will need to use it in combination with a renderer to get something on the screen.

usage

Layout can be computer in interactive and offline model.

In interactive mode you request the layout to perform n iterations per cycle. This is most suitable for the use cases when you ask layout to compute positions from requestAnimationFrame() callback. This will allow you to save CPU cycles when users are not watching at the page and make a device battery life longer.

// assume graph is an instance of ngraph.graph
// e.g.  var graph = require('ngraph.generators').grid(10, 10);
var createLayout = require('ngraph.asyncforce');

var layout = createLayout(graph);

function render() {
  requestAnimationFrame(render);
  // ... other stuff
  layout.step();
}

You can access positions of each node by calling a synchronous method:

var pos = layout.getNodePosition(nodeId);
assert(typeof pos.x === 'number' && typeof pos.y === 'number');

The getNodePosition() method will return last known position for the given node id. You can use this value to provide interactive graph rendering.

In offline mode you don't care as much about battery life. All you need is compute N iterations of the layout without blocking the main thread.

var iterationsToCompute = 150;
var layout = createLayout(graph, {
  async: {
    // tell layout that we want to compute all at once:
    maxIterations: iterationsToCompute,
    stepsPerCycle: iterationsToCompute,

    // Run it till the end:
    waitForStep: false
  }
});

layout.on('cycle', function() {
  graph.forEachNode(printPosition);
});

function printPosition(node) {
  console.log(node.id, layout.getNodePosition(node.id));
}

demo

See demo folder for examples in combination with various renderers.

I'm not using this module very often, and your suggestions for the API design are very welcome!

license

MIT