Skip to content

High-Performance real-time 2D plotting library based on native WebGL

License

Notifications You must be signed in to change notification settings

TimDaub/webgl-plot

 
 

Repository files navigation

Build Action

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 refresh rate
  • Full control over the color of each line in each frame
  • No dependencies
  • Works on any browser/platform that supports WebGL
  • Ideal for embedded systems with low resources

Use cases

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.

Limitations

cannot change the line width due to the OpenGL implementation of a line. The OpenGL specification only guarantees a minimum of a single pixel line width. There are other solutions to increase the line width however they substantially increase the size of data vector and take a hit on the performance.

Getting started

Create an HTML canvas:

<div>
  <canvas class="canvas" id="my_canvas"></canvas>
</div>

Import WebGL-Plot library using ES6 modules:

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

Initialization:

const canv = document.getElementById("my_canvas");
const devicePixelRatio = window.devicePixelRatio || 1;
const numX = Math.round(canv.clientWidth * devicePixelRatio);
const color = new ColorRGBA(Math.random(), Math.random(), Math.random(), 1);
const line = new WebglLine(color, numX);
const wglp = new WebGLplot(canv);

Add the line to webgl canvas:

line.lineSpaceX(-1, 2 / numX);
wglp.addLine(line);

Configure the requestAnimationFrame call:

function newFrame() {
  update();
  wglp.update();
  window.requestAnimationFrame(newFrame);
}
window.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);
  }
}

Edit WebGLplot

React Example

React example is under development...

Edit WebGL-Plot React

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

JS Bundle

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/bundle.min.js"></script>

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

Notice that this method is only recommended for test and small codes.

API Documentation

See here 📑

How to use with embedded systems applications?

You can use WebUSB, Web Bluetooth, and Serial API. Examples will be provided soon.

Build

npm i
npm run build

License

MIT

About

High-Performance real-time 2D plotting library based on native WebGL

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 84.3%
  • JavaScript 6.9%
  • CSS 6.1%
  • HTML 2.7%