Skip to content

Commit 0c7685c

Browse files
committed
add optional force option, to force redraw
1 parent 1902c2a commit 0c7685c

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/library/core.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class Waveform {
4242
protected _waveData: number[];
4343
protected _waveLayoutOptions: IWaveLayoutOptions = Waveform.layoutOptions;
4444
protected _firstDrawing = true;
45-
protected _latestRange: number;
45+
protected _latestRange: number | null = null;
4646
protected _plugins: [] = [];
4747
protected _waveClickCallback: IWaveClickCallback | null = null;
4848

@@ -90,6 +90,10 @@ export class Waveform {
9090

9191
public setWaveData(data: number[]): void {
9292

93+
// reset the _latestRange to allow a new
94+
// draw even if the range did not change
95+
this._latestRange = null;
96+
9397
this._waveData = data;
9498

9599
}
@@ -165,13 +169,18 @@ export class Waveform {
165169
*
166170
* @param range
167171
*/
168-
public draw(range?: number): void {
172+
public draw(range?: number, force = false): void {
169173

170174
// measure fps
171175
//this.fps();
172176

173177
const peaksLength = this._waveData.length;
174178

179+
if (peaksLength === 0) {
180+
// nothing to draw
181+
return;
182+
}
183+
175184
// the canvas width is the width of all the peaks, plus the width of
176185
// all the spaces, the amount of spaces is equal to the amount of peaks
177186
// minus one
@@ -186,7 +195,8 @@ export class Waveform {
186195
peaksRange = Math.round(range * peaksPercentage);
187196

188197
// if the range did not change since last draw don't redraw
189-
if (peaksRange === this._latestRange) {
198+
// except if force is true
199+
if (peaksRange === this._latestRange && !force) {
190200
return;
191201
}
192202

0 commit comments

Comments
 (0)