1
- import { Canvas } from './canvas' ;
1
+ import { Canvas } from './canvas'
2
2
3
3
export interface IWaveCoreOptions {
4
4
canvasElement ?: HTMLCanvasElement ;
@@ -35,35 +35,35 @@ export class Waveform {
35
35
peakBottomFillStyle : '#ff2975' ,
36
36
peakTopProgressFillStyle : '#ffd319' ,
37
37
peakBottomProgressFillStyle : '#ff901f'
38
- } ;
38
+ }
39
39
40
- protected _canvasContext : CanvasRenderingContext2D ;
41
- protected _canvasElement : HTMLCanvasElement ;
42
- protected _waveData : number [ ] ;
43
- protected _waveLayoutOptions : IWaveLayoutOptions = Waveform . layoutOptions ;
44
- protected _firstDrawing = true ;
45
- protected _latestRange : number | null = null ;
46
- protected _plugins : [ ] = [ ] ;
47
- protected _waveClickCallback : IWaveClickCallback | null = null ;
40
+ protected _canvasContext : CanvasRenderingContext2D
41
+ protected _canvasElement : HTMLCanvasElement
42
+ protected _waveData : number [ ]
43
+ protected _waveLayoutOptions : IWaveLayoutOptions = Waveform . layoutOptions
44
+ protected _firstDrawing = true
45
+ protected _latestRange : number | null = null
46
+ protected _plugins : [ ] = [ ]
47
+ protected _waveClickCallback : IWaveClickCallback | null = null
48
48
49
49
constructor ( waveCoreOptions ?: IWaveCoreOptions ) {
50
50
51
51
if ( waveCoreOptions !== undefined ) {
52
52
53
53
if ( waveCoreOptions . canvasElement !== undefined ) {
54
- this . setCanvasElement ( waveCoreOptions . canvasElement ) ;
54
+ this . setCanvasElement ( waveCoreOptions . canvasElement )
55
55
}
56
56
57
57
if ( waveCoreOptions . data !== undefined ) {
58
- this . setWaveData ( waveCoreOptions . data ) ;
58
+ this . setWaveData ( waveCoreOptions . data )
59
59
}
60
60
61
61
if ( waveCoreOptions . layout !== undefined ) {
62
- this . setLayoutOptions ( waveCoreOptions . layout ) ;
62
+ this . setLayoutOptions ( waveCoreOptions . layout )
63
63
}
64
64
65
65
if ( waveCoreOptions . waveformClickCallback !== undefined ) {
66
- this . setWaveformClickCallback ( waveCoreOptions . waveformClickCallback ) ;
66
+ this . setWaveformClickCallback ( waveCoreOptions . waveformClickCallback )
67
67
}
68
68
69
69
}
@@ -72,96 +72,96 @@ export class Waveform {
72
72
73
73
public setCanvasElement ( canvasElement : HTMLCanvasElement ) : void {
74
74
75
- this . _canvasElement = canvasElement ;
75
+ this . _canvasElement = canvasElement
76
76
77
- const canvas = new Canvas ( ) ;
77
+ const canvas = new Canvas ( )
78
78
79
- this . _canvasContext = canvas . getContext ( canvasElement ) ;
79
+ this . _canvasContext = canvas . getContext ( canvasElement )
80
80
81
- this . _addClickWaveListener ( ) ;
81
+ this . _addClickWaveListener ( )
82
82
83
83
}
84
84
85
85
public getCanvasElement ( ) : HTMLCanvasElement {
86
86
87
- return this . _canvasElement ;
87
+ return this . _canvasElement
88
88
89
89
}
90
90
91
91
public setWaveData ( data : number [ ] ) : void {
92
92
93
93
// reset the _latestRange to allow a new
94
94
// draw even if the range did not change
95
- this . _latestRange = null ;
95
+ this . _latestRange = null
96
96
97
- this . _waveData = data ;
97
+ this . _waveData = data
98
98
99
99
}
100
100
101
101
public getWaveData ( ) : number [ ] {
102
102
103
- return this . _waveData ;
103
+ return this . _waveData
104
104
105
105
}
106
106
107
107
public setLayoutOptions ( layout : IWaveLayoutOptions ) : void {
108
108
109
- Object . assign ( this . _waveLayoutOptions , layout ) ;
109
+ Object . assign ( this . _waveLayoutOptions , layout )
110
110
111
111
}
112
112
113
113
public getLayoutOptions ( ) : IWaveLayoutOptions {
114
114
115
- return this . _waveLayoutOptions ;
115
+ return this . _waveLayoutOptions
116
116
117
117
}
118
118
119
119
public setWaveformClickCallback ( waveformClickCallback : IWaveClickCallback ) : void {
120
120
121
- this . _waveClickCallback = waveformClickCallback ;
121
+ this . _waveClickCallback = waveformClickCallback
122
122
123
123
}
124
124
125
125
public getWaveformClickCallback ( ) : IWaveClickCallback {
126
126
127
- return this . _waveClickCallback ;
127
+ return this . _waveClickCallback
128
128
129
129
}
130
130
131
131
protected _addClickWaveListener ( ) : void {
132
132
133
- this . _canvasElement . addEventListener ( 'click' , this . _canvasElementClick . bind ( this ) ) ;
133
+ this . _canvasElement . addEventListener ( 'click' , this . _canvasElementClick . bind ( this ) )
134
134
135
135
}
136
136
137
137
protected _removeClickWaveListener ( ) : void {
138
138
139
- this . _canvasElement . removeEventListener ( 'click' , this . _canvasElementClick . bind ( this ) ) ;
139
+ this . _canvasElement . removeEventListener ( 'click' , this . _canvasElementClick . bind ( this ) )
140
140
141
141
}
142
142
143
143
protected _canvasElementClick ( event : MouseEvent ) : void {
144
144
145
145
if ( this . _waveClickCallback !== null ) {
146
146
147
- event . preventDefault ( ) ;
147
+ event . preventDefault ( )
148
148
149
- const canvasHorizontalPositionInPixel = this . _getMouseHorizontalPosition ( event ) ;
150
- const pixelsPerPercent = this . _canvasElement . width / 100 ;
151
- const clickHorizontalPositionInPercent = canvasHorizontalPositionInPixel / pixelsPerPercent ;
149
+ const canvasHorizontalPositionInPixel = this . _getMouseHorizontalPosition ( event )
150
+ const pixelsPerPercent = this . _canvasElement . width / 100
151
+ const clickHorizontalPositionInPercent = canvasHorizontalPositionInPixel / pixelsPerPercent
152
152
153
- this . _waveClickCallback ( clickHorizontalPositionInPercent ) ;
153
+ this . _waveClickCallback ( clickHorizontalPositionInPercent )
154
154
155
155
}
156
156
157
157
}
158
158
159
159
protected _getMouseHorizontalPosition ( event : MouseEvent ) : number {
160
160
161
- const boundingClientRectangle = this . _canvasElement . getBoundingClientRect ( ) ;
162
- const position = event . clientX - boundingClientRectangle . left ;
161
+ const boundingClientRectangle = this . _canvasElement . getBoundingClientRect ( )
162
+ const position = event . clientX - boundingClientRectangle . left
163
163
164
- return position ;
164
+ return position
165
165
166
166
}
167
167
@@ -174,101 +174,101 @@ export class Waveform {
174
174
// measure fps
175
175
//this.fps();
176
176
177
- const peaksLength = this . _waveData . length ;
177
+ const peaksLength = this . _waveData . length
178
178
179
179
if ( peaksLength === 0 ) {
180
180
// nothing to draw
181
- console . warn ( 'can not draw, peaks array (waveData) is empty' ) ;
182
- return ;
181
+ console . warn ( 'can not draw, peaks array (waveData) is empty' )
182
+ return
183
183
}
184
184
185
185
if ( range < 0 || range > 100 ) {
186
- console . warn ( 'range value must be >= 0 and <= 100' ) ;
187
- return ;
186
+ console . warn ( 'range value must be >= 0 and <= 100' )
187
+ return
188
188
}
189
189
190
190
// the canvas width is the width of all the peaks, plus the width of
191
191
// all the spaces, the amount of spaces is equal to the amount of peaks
192
192
// minus one
193
- const canvasWidth = ( peaksLength * this . _waveLayoutOptions . peakWidthInPixel ) + ( ( peaksLength - 1 ) * this . _waveLayoutOptions . spaceWidthInPixel ) ;
193
+ const canvasWidth = ( peaksLength * this . _waveLayoutOptions . peakWidthInPixel ) + ( ( peaksLength - 1 ) * this . _waveLayoutOptions . spaceWidthInPixel )
194
194
195
- let peaksRange = 0 ;
195
+ let peaksRange = 0
196
196
197
- const peaksPercentage = peaksLength / 100 ;
197
+ const peaksPercentage = peaksLength / 100
198
198
199
- peaksRange = Math . round ( range * peaksPercentage ) ;
199
+ peaksRange = Math . round ( range * peaksPercentage )
200
200
201
201
// if the range did not change since last draw don't redraw
202
202
// except if force is true
203
203
if ( peaksRange === this . _latestRange && ! force ) {
204
- return ;
204
+ return
205
205
}
206
206
207
- this . _latestRange = peaksRange ;
207
+ this . _latestRange = peaksRange
208
208
209
- const canvasHeight = this . _waveLayoutOptions . waveHeightInPixel ;
209
+ const canvasHeight = this . _waveLayoutOptions . waveHeightInPixel
210
210
211
211
// canvas dimensions
212
- this . _canvasElement . height = canvasHeight ;
213
- this . _canvasElement . width = canvasWidth ;
212
+ this . _canvasElement . height = canvasHeight
213
+ this . _canvasElement . width = canvasWidth
214
214
215
215
// each peak is the line and the line width is the peak width
216
- this . _canvasContext . lineWidth = this . _waveLayoutOptions . peakWidthInPixel ;
216
+ this . _canvasContext . lineWidth = this . _waveLayoutOptions . peakWidthInPixel
217
217
218
218
// the max height of the top peaks
219
- const topPeakMaxHeightInPixel = this . _waveLayoutOptions . waveHeightInPixel * ( this . _waveLayoutOptions . waveTopPercentage / 100 ) ;
219
+ const topPeakMaxHeightInPixel = this . _waveLayoutOptions . waveHeightInPixel * ( this . _waveLayoutOptions . waveTopPercentage / 100 )
220
220
221
221
// the max height of the bottom peaks
222
- const bottomPeakMaxHeightInPixel = this . _waveLayoutOptions . waveHeightInPixel * ( ( 100 - this . _waveLayoutOptions . waveTopPercentage ) / 100 ) ;
222
+ const bottomPeakMaxHeightInPixel = this . _waveLayoutOptions . waveHeightInPixel * ( ( 100 - this . _waveLayoutOptions . waveTopPercentage ) / 100 )
223
223
224
224
// canvas background fill style
225
- this . _canvasContext . fillStyle = this . _waveLayoutOptions . waveBackgroundFillStyle ;
226
- this . _canvasContext . fillRect ( 0 , 0 , canvasWidth , canvasHeight ) ;
225
+ this . _canvasContext . fillStyle = this . _waveLayoutOptions . waveBackgroundFillStyle
226
+ this . _canvasContext . fillRect ( 0 , 0 , canvasWidth , canvasHeight )
227
227
228
- let i ;
228
+ let i
229
229
230
230
for ( i = 0 ; i < peaksLength ; i ++ ) {
231
231
232
- let topStrokeFillStyle ;
233
- let bottomStrokeFillStyle ;
232
+ let topStrokeFillStyle
233
+ let bottomStrokeFillStyle
234
234
235
235
if ( i < peaksRange ) {
236
236
237
- topStrokeFillStyle = this . _waveLayoutOptions . peakTopProgressFillStyle ;
238
- bottomStrokeFillStyle = this . _waveLayoutOptions . peakBottomProgressFillStyle ;
237
+ topStrokeFillStyle = this . _waveLayoutOptions . peakTopProgressFillStyle
238
+ bottomStrokeFillStyle = this . _waveLayoutOptions . peakBottomProgressFillStyle
239
239
240
240
} else {
241
241
242
- topStrokeFillStyle = this . _waveLayoutOptions . peakTopFillStyle ;
243
- bottomStrokeFillStyle = this . _waveLayoutOptions . peakBottomFillStyle ;
242
+ topStrokeFillStyle = this . _waveLayoutOptions . peakTopFillStyle
243
+ bottomStrokeFillStyle = this . _waveLayoutOptions . peakBottomFillStyle
244
244
245
245
}
246
246
247
- const peakHeightInPercent = this . _waveData [ i ] ;
247
+ const peakHeightInPercent = this . _waveData [ i ]
248
248
249
249
// the horizontal position of a peak
250
- const peakHorizontalPosition = ( ( i + 1 ) * this . _waveLayoutOptions . peakWidthInPixel ) + ( i * this . _waveLayoutOptions . spaceWidthInPixel ) ;
250
+ const peakHorizontalPosition = ( ( i + 1 ) * this . _waveLayoutOptions . peakWidthInPixel ) + ( i * this . _waveLayoutOptions . spaceWidthInPixel )
251
251
252
252
// waveform top
253
- this . _canvasContext . beginPath ( ) ;
254
- this . _canvasContext . moveTo ( peakHorizontalPosition , topPeakMaxHeightInPixel ) ;
255
- this . _canvasContext . lineTo ( peakHorizontalPosition , topPeakMaxHeightInPixel - ( topPeakMaxHeightInPixel * ( peakHeightInPercent / 100 ) ) ) ;
256
- this . _canvasContext . strokeStyle = topStrokeFillStyle ;
257
- this . _canvasContext . stroke ( ) ;
253
+ this . _canvasContext . beginPath ( )
254
+ this . _canvasContext . moveTo ( peakHorizontalPosition , topPeakMaxHeightInPixel )
255
+ this . _canvasContext . lineTo ( peakHorizontalPosition , topPeakMaxHeightInPixel - ( topPeakMaxHeightInPixel * ( peakHeightInPercent / 100 ) ) )
256
+ this . _canvasContext . strokeStyle = topStrokeFillStyle
257
+ this . _canvasContext . stroke ( )
258
258
259
259
// waveform bottom
260
- this . _canvasContext . beginPath ( ) ;
261
- this . _canvasContext . moveTo ( peakHorizontalPosition , topPeakMaxHeightInPixel ) ;
262
- this . _canvasContext . lineTo ( peakHorizontalPosition , topPeakMaxHeightInPixel + ( bottomPeakMaxHeightInPixel * ( peakHeightInPercent / 100 ) ) ) ;
263
- this . _canvasContext . strokeStyle = bottomStrokeFillStyle ;
264
- this . _canvasContext . stroke ( ) ;
260
+ this . _canvasContext . beginPath ( )
261
+ this . _canvasContext . moveTo ( peakHorizontalPosition , topPeakMaxHeightInPixel )
262
+ this . _canvasContext . lineTo ( peakHorizontalPosition , topPeakMaxHeightInPixel + ( bottomPeakMaxHeightInPixel * ( peakHeightInPercent / 100 ) ) )
263
+ this . _canvasContext . strokeStyle = bottomStrokeFillStyle
264
+ this . _canvasContext . stroke ( )
265
265
266
266
}
267
267
}
268
268
269
269
public destroy ( ) : void {
270
270
271
- this . _removeClickWaveListener ( ) ;
271
+ this . _removeClickWaveListener ( )
272
272
273
273
}
274
274
}
0 commit comments