Skip to content

Commit 10afc4c

Browse files
committed
fill style to replace color, fix options bug
1 parent b12625a commit 10afc4c

File tree

18 files changed

+411
-182
lines changed

18 files changed

+411
-182
lines changed

README.md

Lines changed: 229 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Visualizes waveform data (peaks), for example the ones that got generated using: <https://github.com/chrisweb/waveform-data-generator>
88

9-
![audio waveform visualizer output: waveform image](./docs/images/waveform_example.png)
9+
![audio waveform visualizer output: waveform image](./docs/images/waveform-visualizer.gif)
1010

1111
Waveform created using data that got generated using the [waveform data generator](https://github.com/chrisweb/waveform-data-generator)
1212

@@ -20,6 +20,234 @@ if you want to use the waveform visualizer in your own project you can install i
2020

2121
Check out the [simple waveform example](/examples/simple-waveform/README.md)
2222

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:
36+
37+
```ts
38+
import { Waveform, IWaveLayoutOptions, IWaveCoreOptions, IWaveClickCallback } from 'waveform-visualizer'
39+
```
40+
41+
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:
60+
61+
![audio waveform visualizer output: waveform image](./docs/images/waveform_example_50.png)
62+
63+
if you prefer a wave which only displays the top peaks then set the value for **waveTopPercentage** to 100, the result look like this:
64+
65+
![audio waveform visualizer output: waveform image](./docs/images/waveform_example_100.png)
66+
67+
if you want to only display bottom peaks set **waveTopPercentage** to 0:
68+
69+
![audio waveform visualizer output: waveform image](./docs/images/waveform_example_0.png)
70+
71+
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:
72+
73+
![audio waveform visualizer output: waveform image](./docs/images/waveform_example_70.png)
74+
75+
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:
80+
81+
```ts
82+
const waveformClickCallback = (clickHorizontalPositionInPercent: number): void => {
83+
84+
console.log('waveformClickCallback: ', clickHorizontalPositionInPercent)
85+
86+
// tell the audio player to move to 50% of the sound
87+
88+
}
89+
```
90+
91+
Next create a core options object and we will add 4 options:
92+
93+
* the out layout options object
94+
* the data for the waveform peaks
95+
* our click callback function
96+
* the ID of the canvas element
97+
98+
```ts
99+
const waveCoreOptions: IWaveCoreOptions = {
100+
layout: waveLayoutOptions,
101+
data: [10,16,14,15,17,15,17,18,24,23,21,40,64,66,66,63,68,70,68,70,71,69,68,66,66,67,71,30,29,29,30,67,59,59,69,67,60,64,66,69,64,64,70,66,58,61,36,31,31,31,37,35,31,31,35,36,38,34,36,33,31,50,71,68,55,69,69,62,60,78,59,92,100,98,78,65,63,72,71,68,66,65,65,61,63,62,65,65,66,69,64,70,46,28,29,23,62,64,57,64,70,71,56,61,67,69,62,70,66,58,61,47,33,35,33,33,34,32,32,66,69,57,55,85,78,59,66,65,58,81,86,81,78,61,68,73,57,55,74,84,70,53,75,62,68,72,72,80,66,66,37,30,24,23,67,62,58,66,67,60,58,64,67,65,64,70,66,57,63,68,70,58,61,68,64,59,64,62,73,63,71,71,61,57,47,20,21,20,21,22,22,21,22,25,25,29,25,24,19,21],
102+
waveformClickCallback: onWaveClickHandler,
103+
}
104+
```
105+
106+
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 = new Waveform(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+
<canvas id="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:
121+
122+
```ts
123+
const canvasElement = document.getElementById(visualizerOptions.canvasElementId) as HTMLCanvasElement
124+
125+
waveform.setCanvasElement(canvasRef.current)
126+
```
127+
128+
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:
147+
148+
```ts
149+
const playerPlayingProgressCallback = (progressInPercent) => {
150+
waveform.draw(progressInPercent)
151+
}
152+
```
153+
154+
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
159+
160+
React component example to create a waveform:
161+
162+
```ts
163+
import { useRef, useCallback, useEffect } from 'react'
164+
import { Waveform, IWaveLayoutOptions, IWaveCoreOptions } from 'waveform-visualizer'
165+
166+
const WaveformComponent = () => {
167+
168+
const canvasRef = useRef<HTMLCanvasElement>()
169+
170+
const onWaveClickHandler = useCallback((clickHorizontalPositionInPercent: number): void => {
171+
172+
console.log('waveformClickCallback: ', clickHorizontalPositionInPercent)
173+
174+
// tell the audio player to move to 50% of the sound
175+
176+
}, [])
177+
178+
const initializeWaveform = useCallback(() => {
179+
180+
const waveLayoutOptions: IWaveLayoutOptions = {
181+
waveTopPercentage: 50,
182+
peakTopFillStyle: 'deepskyblue',
183+
peakBottomFillStyle: 'dodgerblue ',
184+
}
185+
186+
const waveCoreOptions: IWaveCoreOptions = {
187+
layout: waveLayoutOptions,
188+
data: [10,16,14,15,17,15,17,18,24,23,21,40,64,66,66,63,68,70,68,70,71,69,68,66,66,67,71,30,29,29,30,67,59,59,69,67,60,64,66,69,64,64,70,66,58,61,36,31,31,31,37,35,31,31,35,36,38,34,36,33,31,50,71,68,55,69,69,62,60,78,59,92,100,98,78,65,63,72,71,68,66,65,65,61,63,62,65,65,66,69,64,70,46,28,29,23,62,64,57,64,70,71,56,61,67,69,62,70,66,58,61,47,33,35,33,33,34,32,32,66,69,57,55,85,78,59,66,65,58,81,86,81,78,61,68,73,57,55,74,84,70,53,75,62,68,72,72,80,66,66,37,30,24,23,67,62,58,66,67,60,58,64,67,65,64,70,66,57,63,68,70,58,61,68,64,59,64,62,73,63,71,71,61,57,47,20,21,20,21,22,22,21,22,25,25,29,25,24,19,21],
189+
waveformClickCallback: onWaveClickHandler,
190+
}
191+
192+
const waveform = new Waveform(waveCoreOptions)
193+
194+
waveform.setCanvasElement(canvasRef.current)
195+
196+
waveform.draw(0)
197+
198+
}, [onWaveClickHandler])
199+
200+
useEffect(() => {
201+
initializeWaveform()
202+
}, [initializeWaveform])
203+
204+
return (
205+
<div className="audioWaveForm">
206+
<canvas ref={canvasRef} width="200px" height="60px" />
207+
</div>
208+
)
209+
})
210+
211+
export default WaveformComponent
212+
```
213+
214+
## core options
215+
216+
* 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+
23251
## development: build
24252

25253
install the latest nodejs (if you haven't already) [nodejs](https://nodejs.org)

dist/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { Waveform, ICoreOptions, IWaveLayoutOptions, IWaveformClickCallback } from './library/core';
1+
export { Waveform, IWaveCoreOptions, IWaveLayoutOptions, IWaveClickCallback } from './library/core';

0 commit comments

Comments
 (0)