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

webgl-plot

v0.7.1

Published

High-performance 2D plotting library based on native WebGL

Downloads

4,155

Readme

Npm Build Yarn Build Code scanning DOI

Live demo 🚀

webgl-plot

multi-line high-performance 2D plotting library using native WebGL. The advantages are:

  • Simple and efficient 2D WebGL library
  • Using WebGL native line drawing
  • High update rate which matches the screen's refresh rate (FPS)
  • Works for both dynamic and static data
  • supports both thin (native) and thick lines
  • Full control over the color of each line in each frame
  • No dependencies
  • Works on any browsers/platforms that supports WebGL
  • Compatible with OffScreenCanvas and WebWorkers for offloading cpu time from the main thread
  • Ideal for embedded systems with low resources or large datasets

Use cases

Dynamic: When plotting real-time multiple waveforms are required. For example, software-based oscilloscopes, Arduino, microcontrollers, FPGA user interfaces. This framework also can be used in combination with ElectronJS.

Static: Enables rapid pan and zoom capability for inspecting very large datasets. See the static example

Thick Lines

However notice that due to computation of the line data points, the performance of the thick lines is nearly 6 times slower than the normal lines. Only use thick lines when you need to see the lines clearly for example when highlighting a specific line. Further information can be found below. For benchmarking, see the benchmark section.

Version next coming soon 🎉

The next version is currently under development. More computation is moved to the GPU, significantly improving performance. These improvements specifically benefit the rolling plot and the scatter plot. However, these changes require a rewrite of the main library and migration to webgl2. The current version will remain as no maintenance is needed since it is based on pure javascript. See an example here.

Python vesrion now released!! 🥳

See pyglplot for the python equivalent of this library. However, please notice the python version is at its early stages.

Getting started

Create an HTML canvas with an appropriate width or height:

<div>
  <canvas style="width: 100%;" id="my_canvas"></canvas>
</div>

Import the webgl-plot library using ES6 modules:

import { WebglPlot, WebglLine, ColorRGBA } from "webgl-plot";

Prepare the canvas:

const canvas = document.getElementById("my_canvas");
const devicePixelRatio = window.devicePixelRatio || 1;
canvas.width = canvas.clientWidth * devicePixelRatio;
canvas.height = canvas.clientHeight * devicePixelRatio;

Note: The canvas width and height must be set in order to be able to draw on the canvas.

Initialization:

const numX = canvas.width;
const color = new ColorRGBA(Math.random(), Math.random(), Math.random(), 1);
const line = new WebglLine(color, numX);
const wglp = new WebglPlot(canvas);

Automatically arrange X values between [-1,1]:

line.arrangeX();

Add the line to the webgl canvas:

wglp.addLine(line);

Configure the requestAnimationFrame call:

function newFrame() {
  update();
  wglp.update();
  requestAnimationFrame(newFrame);
}
requestAnimationFrame(newFrame);

Add the update function:

function update() {
  const freq = 0.001;
  const amp = 0.5;
  const noise = 0.1;

  for (let i = 0; i < line.numPoints; i++) {
    const ySin = Math.sin(Math.PI * i * freq * Math.PI * 2);
    const yNoise = Math.random() - 0.5;
    line.setY(i, ySin * amp + yNoise * noise);
  }
}

Don't forget to update the canvas with wglp.update() each time you want to redraw the changes that you have made to the line objects.

Edit WebGLplot

Demos

See examples based on vanilla JS at webgl-plot-examples

See examples based on React

See SPAD Simulation which use WebGL-Plot as an oscilloscope display

React Examples

For a basic React example see here:

Edit WebGL-Plot React

React website is under development...

https://webgl-plot-react.vercel.app/

JS Bundles

To use WebGL-Plot as a JS pre-bundled package first import the following in your HTML file:

<script src="https://cdn.jsdelivr.net/gh/danchitnis/webgl-plot@master/dist/webglplot.umd.min.js"></script>

See examples on how to use this bundle in Codepen and JSfiddle

For ES6 module and direct browser import use:

<script type="module" src="your-code.js"></script>

and in your-code.js:

import { WebglPlot, WebglLine, ColorRGBA } from "<http source>";

You can use web-based bundlers such as esm.sh, unpkng, JSdeliver ,and jspm to import the library to get the appropriate http source. See an example here: JSfiddle

Thanks to TimDaub for testing the ES6 module.

Benchmark

Native Line and Thick Line.

See Benchmark for more detailed analysis.

Internal test

ESM, off-screen, UMD

API Documentation

See here 📑

How to use with embedded systems applications?

You can use WebUSB, Web Bluetooth, and Serial API. You can use ComPort for a basic implementation of Serial API

Build

npm i
npm run build

License

MIT