WebGL-Plot

by danchitnis

HTML

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

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

CSS

body {
  background-color: rgb(0.1, 0.1, 0.1);
  color: white;
  font-family: Arial, Helvetica, sans-serif;
}

.canvas {
  width: 100%;
  height: 70vh;
}

JavaScript

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

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

line.arrangeX();
wglp.addLine(line);

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

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);
  }
}