You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Waveform created using data that got generated using the [waveform data generator](https://github.com/chrisweb/waveform-data-generator)
12
12
@@ -20,6 +20,234 @@ if you want to use the waveform visualizer in your own project you can install i
20
20
21
21
Check out the [simple waveform example](/examples/simple-waveform/README.md)
22
22
23
+
## documentation
24
+
25
+
### vanilla javascript example
26
+
27
+
Open your project in your favorite IDE (VSCode) and then [install](#installation) the waveform dependency
28
+
29
+
Now that the dependency is installed, import the waveform-visualizer package into your javascript code like this:
30
+
31
+
```js
32
+
import { Waveform } from'waveform-visualizer'
33
+
```
34
+
35
+
if you are using typescript, which is what I will do now for the rest of this example, also import the interfaces so that we have typed options objects (for the core and the layout options), like so:
next create a layout options object, to tweak the look of the waveform a bit:
42
+
43
+
```ts
44
+
const waveLayoutOptions:IWaveLayoutOptions= {
45
+
waveTopPercentage: 50,
46
+
peakTopFillStyle: 'deepskyblue',
47
+
peakBottomFillStyle: 'dodgerblue ',
48
+
}
49
+
```
50
+
51
+
Note: a **fill style** can be a css color name (which is what we use here) or a color hex code or an rgb(a) color, a canvas gradient or a canvas pattern, for more details about what values can be used for the *FillStyle options, check out the [layout options chapter](#layout-options)
52
+
53
+
All layout options are optional, at this point we are only setting 3 options:
54
+
55
+
* the first one is called "waveTopPercentage" and is used to the tell the waveform where the middle is, if you set it to 50 like in this example, then the top of the waveform will be 50% and the bottom 50% too, meaning the top peaks will be a mirror image of the bottom peaks
56
+
* next we set **peakTopFillStyle** to deepskyblue, because we just set the waveTopPercentage to 50% the top half of the waveform peaks will use the **deepskyblue**
57
+
* finally we **peakBottomFillStyle** to **dodgerblue** meaning the bottom half peaks will be another color (of course if you prefer you can also use the same color twice for the bottom as well as the top peaks)
58
+
59
+
using these 3 options this is the result you will get:
and if you want the top peaks to be bigger (70% of the height) than the bottom peaks (30% of the height), then set the value for **waveTopPercentage** to 70:
Note: You can also change the width of the peaks, the width of the space between peaks and a lot more via the layout options, for a full list of available options and their default values, check out the [layout options chapter](#layout-options) in this readme
76
+
77
+
Note 2: If you want to see an example of how to use gradient fill styles, check out the client code of the [simple waveform example](/examples/simple-waveform/README.md) in this repository
78
+
79
+
next we will create a **click callback** function, this callback will get triggered by the waveform every time a user clicks on the waveform, it will accept a parameter called **clickHorizontalPositionInPercent** which is the horizontal position (in percent) of the waveform based on the mouse position, this means that if the user clicks exactly in the middle of the waveform then this value will be 50%, based on that information you can then tell your audio (sound) player to seek (move) to that position of the song:
Note: if you want to generate peaks data for one of your songs or sounds, you can for example use another tool I created which is called **waveform-data-generator**, it is written in javascript (nodejs) and available on github too: <https://github.com/chrisweb/waveform-data-generator>
107
+
108
+
Now it is time to create an instance of the waveform visualizer and pass the core options object to it:
109
+
110
+
```ts
111
+
const waveform =newWaveform(waveCoreOptions)
112
+
```
113
+
114
+
Now we need to create an HTML canvas element to display the waveform, open your HTML document and create a new HTML canvas element like so:
115
+
116
+
```html
117
+
<canvasid="waveformCanvas"></canvas>
118
+
```
119
+
120
+
then back into our javascript code, get the canvas element and then pass it to the waveform using the **setCanvasElement** method:
Note: all the options can be set when creating the waveform instance or like above using setters, there are also getters for each option in case you want to read an option and know what value is currently set, for a full list of all setters and getters check out the [waveform methods chapter](#waveform-methods)
129
+
130
+
If you are not using vanilla javascript but React, then I recommend to use React Hook called [useRef](https://react.dev/reference/react/useRef) instead:
131
+
132
+
```ts
133
+
const canvasRef =useRef<HTMLCanvasElement>()
134
+
135
+
waveform.setCanvasElement(canvasRef.current)
136
+
```
137
+
138
+
Note: for a full react example check out the next chapter [](#react-component-example)
139
+
140
+
Final step, we need to tell the waveform to perform the initial draw so that something gets painted inside of our canvas element, like so:
141
+
142
+
```ts
143
+
waveform.draw()
144
+
```
145
+
146
+
The draw function has an optional parameter called **range**, the range must be a number between 0 and 100, it tells the waveform at what position your sound (song) currently it, if you have a player and that player allows you to set a callback function when the sound (song) is playing, then you should call the draw function again every time that playing progress callback gets triggered, like so:
To see an example of what it looks like when the range gets modified, check out the animated gif on top of this README or install the example from this repository, which uses an audio player that redraws the waveform element every time its progress callback gets triggered: [simple waveform and audio player example](/examples/simple-waveform/README.md)
155
+
156
+
### React component example
157
+
158
+
Note: This code does the exact same thing as the previous example but uses React instead of vanilla javascript
* canvasElement?: [HTMLCanvasElement] (optional, if not set here you must use the **setCanvasElement** method) the canvas element that will get used to draw the waveform
217
+
* data?: [number[]], (optional, if not set here you must use the **setWaveData** method) an array of numbers (list of peaks values)
218
+
* layout?: [IWaveLayoutOptions], (optional, if not set the waveform will use its own default values, can be set later using the **setWaveData** method) a layout options object (for a full list of layout options check out the next [layout options chapter](#layout-options))
219
+
* waveformClickCallback?: [IWaveClickCallback], (optional, if not set here you must use the **setWaveformClickCallback** method) a callback function that will get triggered when the user clicks on the waveform, the first parameter of the callback is a number, it tells you the horizontal position of the click, for example a value of 50 means the user clicked in the middle of the waveform, based on that number you can tell your player to seek to that position of the sound (song), to see a working example check out the client source code of [simple waveform example](/examples/simple-waveform/README.md)
220
+
221
+
## layout options
222
+
223
+
* waveHeightInPixel: [number] (default: 100) the height of the waveform inside of the canvas element
224
+
* waveBackgroundFillStyle: [string (hex, rgb(a) or css color name) | CanvasGradient | CanvasPattern] (default: 'transparent') the background color of the waveform
225
+
* peakWidthInPixel: [number] (default: 2) the width in pixel of a single peak
226
+
* spaceWidthInPixel: [number] (default: 1) the width in pixel of the space between each peak
227
+
* waveTopPercentage: [number] (default: 50) a percentage value that defines how big the top half is compared to the bottom half, a value of 50 means the top and bottom half are each 50% of the total height, a value of 70 would create a waveform where the top peaks are 70% high and the bottom 30% of the total height, a value of 100 would only draw the top peaks and a value of 0 would only draw the bottom peaks
228
+
* peakTopFillStyle: [string (hex, rgb(a) or css color name) | CanvasGradient | CanvasPattern] (default: '#f222ff') the color used for the top peaks outside of the range (initial peak color, beyond the range)
229
+
* peakBottomFillStyle: [string (hex, rgb(a) or css color name) | CanvasGradient | CanvasPattern] (default: '#ff2975') the color used for the bottom peaks outside of the range (initial peak color, beyond the range)
230
+
* peakTopProgressFillStyle: [string (hex, rgb(a) or css color name) | CanvasGradient | CanvasPattern] (default: '#ffd319') the color used for the top peaks that are inside of the range
231
+
* peakBottomProgressFillStyle: [string (hex, rgb(a) or css color name) | CanvasGradient | CanvasPattern] (default: '#ff901f') the color used for the bottom peaks that are inside of the range
232
+
233
+
Note: every *FillStyle option accepts as value any [CSS color](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) (so any color string like 'transparent' or 'blue', any hex color like '#000' (hex for black is the default value) or an rgb(a) color like 'rgb(255,255,255)' or 'rgba(0,0,0,0.8)'), a [canvas gradient](https://developer.mozilla.org/en-US/docs/Web/API/CanvasGradient), or a [canvas pattern](https://developer.mozilla.org/en-US/docs/Web/API/CanvasPattern), read more: [MDN: canvas fillStyle property](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/fillStyle)
234
+
235
+
Note: there is no option for the width of a waveform, the width will be determined by the amount of peaks values are in your data array, the width you set for the peaks and the width for the space between each peak:
236
+
237
+
`waveform width = (amount of peaks * peaks width) + ((amount of peaks -1) * peak space width)`
238
+
239
+
## waveform methods
240
+
241
+
* setCanvasElement(canvasElement: HTMLCanvasElement): void, to set the canvas element that will be used to draw the waveform
242
+
* getCanvasElement(): HTMLCanvasElement, to get the canvas element
243
+
* setWaveData(data: number[]): void, to set the data array containing the values (numbers) for each peak
244
+
* getWaveData(): number[], to get the data of the peaks
245
+
* setLayoutOptions(layout: IWaveLayoutOptions): void, to pass new layout options to the waveform and change all or some of the layout options after the initialization of the waveform
246
+
* getLayoutOptions(): IWaveLayoutOptions, to get the current layout options
247
+
* setWaveformClickCallback(waveformClickCallback: IWaveClickCallback): void, to add the click callback to the waveform (which will get triggered when the user clicks on the canvas of the waveform)
248
+
* getWaveformClickCallback(): IWaveClickCallback, to get the current click callback
249
+
* draw(range?: number): void, to tell the waveform to draw the peaks, if a range is set (must be a number between 0 and 100) the color of the peaks inside of the range percentage can get drawn using a different color to show the current playing progress of the sound (or song)
250
+
23
251
## development: build
24
252
25
253
install the latest nodejs (if you haven't already) [nodejs](https://nodejs.org)
0 commit comments