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

@anvaka/streamlines

v1.5.0

Published

Streamlines calculator

Downloads

23

Readme

streamlines

The library builds streamlines for arbitrary vector fields, trying to keep uniform distance between them.

demo

usage

You can play with the interactive demo, though keep in mind - it's just a demo created in one night. True value of this repository is in the library itself :).

Demo is here: https://anvaka.github.io/streamlines

More advanced example is wind-map - visualization of the winds map with evenly spaced streamlines.

get it

Use it with your favorite bundler:

npm install @anvaka/streamlines

Or include a precompiled version:

https://cdn.jsdelivr.net/gh/anvaka/[email protected]/dist/streamlines.min.js

If you are using precompiled version, it will be available under global name window.streamlines.

use it

var vectorField = p => ({ x: -p.y, y: p.x });

streamlines({
  vectorField,
  onPointAdded(from, to) {
    // called when new point is added to a line
    console.log("point created", from, to);
  },
  onStreamlineAdded(points) {
    // Points is just a sequence of points with `x, y` coordinates through which
    // the streamline goes.
    console.log("stream line created. Number of points: ", points.length);
  }
}).run();

The library allows you to configure various aspects of computation:


streamlines({
  // Vector field is a function that given a point p returns 2d vector
  // field {x, y}
  vectorField(p) { return p; },

  // Defines bounding box of the vector field
  boundingBox: {left: -5, top: -5, width: 10, height: 10},

  // Defines the first point where integration should start. If this is
  // not specified a random point inside boundingBox is selected
  // You can pass array of seed points, they are going to be used one by one
  // if they satisfy the rules.
  seed: {x: -1, y: 1},

  // Separation distance between new streamlines.
  dSep: 0.5

  // Distance between streamlines when integration should stop.
  dTest: 0.25,

  // Integration time step (passed to RK4 method.)
  timeStep: 0.01,

  // If set to true, lines are going to be drawn from the seed points
  // only in the direction of the vector field
  forwardOnly: false
}).run();

The library does not depend on any particular rendering engine, and can be used in the browser or node.js environment. However, for your convenience and reference I've added a simple canvas renderer:

// Let's assume you have a <canvas id='scene'></canvas> in your document:
var canvas = document.getElementById("scene");

// Then you can render to it with this bit of code:
streamlines({
  // As usual, define your vector field:
  vectorField(p) {
    return { x: -p.y, y: p.x };
  },

  // And print the output to this canvas:
  onPointAdded: streamlines.renderTo(canvas)
}).run();

Here is a JSBin for you to try.

Async

The library is asynchronous in its nature. This is done mostly to give you more control over streamline construction process.

The downside is that it is harder to understand the code. It is written in a way so that it can be interrupted at almost every computational step.

If you want to cancel rendering, call dispose method:

var renderer = streamlines({
  vectorField(p) {
    return p;
  }
});

// Launch the construction
renderer.run();

// something has happened and you want to stop?
renderer.dispose();

If you want to understand the algorithm, please read this paper - the library follows it closely.

More examples

  • https://github.com/anvaka/noisylines - renders streamlines from perlin noise
  • https://jsbin.com/kuluvam/1/edit?html,js,output - shows how to set width/color of line segments

License

MIT