JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="spectrumCanvasA" width="400" height="300"></canvas>
<canvas id="spectrumCanvasB" width="400" height="300"></canvas>
JavaScript
const colormaps = [
[
]
]
for (let i = 0; i <= 255; i++) {
colormaps[0].push([255,i,0]);
}
function Spectrum(id, options) {
// Handle options
this.centerHz = (options && options.centerHz) ? options.centerHz : 0;
this.spanHz = (options && options.spanHz) ? options.spanHz : 0;
this.wf_size = (options && options.wf_size) ? options.wf_size : 0;
this.wf_rows = (options && options.wf_rows) ? options.wf_rows : 2048;
this.spectrumPercent = (options && options.spectrumPercent) ? options.spectrumPercent : 25;
this.spectrumPercentStep = (options && options.spectrumPercentStep) ? options.spectrumPercentStep : 5;
this.averaging = (options && options.averaging) ? options.averaging : 0;
this.maxHold = (options && options.maxHold) ? options.maxHold : false;
// Setup state
this.paused = false;
this.fullscreen = false;
this.min_db = -120;
this.max_db = -20;
this.spectrumHeight = 0;
// Colors
this.colorindex = 0;
this.colormap = colormaps[0];
// Create main canvas and adjust dimensions to match actual
this.canvas = document.getElementById(id);
this.canvas.height = this.canvas.clientHeight;
this.canvas.width = this.canvas.clientWidth;
this.ctx = this.canvas.getContext("2d");
this.ctx.fillStyle = "black";
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
// Create offscreen canvas for axes
this.axes = document.createElement("canvas");
this.axes.height = 1; // Updated later
this.axes.width = this.canvas.width;
this.ctx_axes = this.axes.getContext("2d");
// Create offscreen canvas for waterfall
this.wf = document.createElement("canvas");
this.wf.height = this.wf_rows;
this.wf.width = this.wf_size;
this.ctx_wf = this.wf.getContext("2d");
// Trigger first render
this.setAveraging(this.averaging);
this.updateSpectrumRatio();
this.resize();
}
Spectrum.prototype.squeeze = function(value, out_min, out_max) {
...