Skip to content

Commit d7a3939

Browse files
committed
Improved draw simple spectrum code
1 parent 7932979 commit d7a3939

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/graph_spectrum_plot.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const BLUR_FILTER_PIXEL = 1,
1313
PID_ERROR_VERTICAL_CHUNK = 5,
1414
ZOOM_X_MAX = 5,
1515
MIN_DBM_VALUE = -40,
16-
MAX_DBM_VALUE = 10;
16+
MAX_DBM_VALUE = 10,
17+
MAX_SPECTRUM_LINE_COUNT = 30000;
1718

1819
export const SPECTRUM_TYPE = {
1920
FREQUENCY: 0,
@@ -206,9 +207,6 @@ GraphSpectrumPlot._drawFrequencyGraph = function (canvasCtx) {
206207

207208
this._drawGradientBackground(canvasCtx, WIDTH, HEIGHT);
208209

209-
const barWidth = WIDTH / (PLOTTED_BUFFER_LENGTH / 5) - 1;
210-
let x = 0;
211-
212210
const barGradient = canvasCtx.createLinearGradient(0, HEIGHT, 0, 0);
213211
barGradient.addColorStop(
214212
constrain(0 / this._zoomY, 0, 1),
@@ -230,13 +228,17 @@ GraphSpectrumPlot._drawFrequencyGraph = function (canvasCtx) {
230228
canvasCtx.fillStyle = barGradient;
231229

232230
const fftScale = HEIGHT / (this._zoomY * 100);
233-
for (let i = 0; i < PLOTTED_BUFFER_LENGTH; i += 5) {
231+
// Limit maximal count of drawing line to get good performance
232+
const stepData = Math.floor(PLOTTED_BUFFER_LENGTH / MAX_SPECTRUM_LINE_COUNT) + 1;
233+
const stepX = WIDTH / (PLOTTED_BUFFER_LENGTH / stepData);
234+
const barWidth = Math.max(stepX, 1);
235+
let x = 0;
236+
for (let i = 0; i < PLOTTED_BUFFER_LENGTH; i += stepData) {
234237
const barHeight = this._fftData.fftOutput[i] * fftScale;
235238
canvasCtx.fillRect(x, HEIGHT - barHeight, barWidth, barHeight);
236-
x += barWidth + 1;
239+
x += stepX;
237240
}
238241

239-
240242
//Draw imported spectrums
241243
const curvesColors = [
242244
"Blue",

0 commit comments

Comments
 (0)