GithubHelp home page GithubHelp logo

zhangaz1 / ngraph.asyncforce Goto Github PK

View Code? Open in Web Editor NEW

This project forked from anvaka/ngraph.asyncforce

0.0 0.0 0.0 547 KB

Force based graph layout with web workers

License: MIT License

JavaScript 99.95% HTML 0.05%

ngraph.asyncforce's Introduction

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

ngraph.asyncforce's People

Contributors

anvaka avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.