Skip to content

Commit

Permalink
Feat: width option (#3259)
Browse files Browse the repository at this point in the history
* Feat: width option

* Snapshots
  • Loading branch information
katspaugh authored Oct 15, 2023
1 parent 354efaa commit 5d7dd12
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 1 deletion.
38 changes: 38 additions & 0 deletions cypress/e2e/options.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,42 @@ describe('WaveSurfer options tests', () => {
})
})
})

it('should accept a numeric width option', () => {
cy.window().then((win) => {
return new Promise((resolve, reject) => {
win.wavesurfer = win.WaveSurfer.create({
container: id,
url: '../../examples/audio/demo.wav',
width: 100,
})

win.wavesurfer.once('ready', () => {
cy.get(id).matchImageSnapshot('width-100')
win.wavesurfer.setOptions({ width: 300 })
cy.get(id).matchImageSnapshot('width-300')
resolve()
})
})
})
})

it('should accept a CSS value for the width option', () => {
cy.window().then((win) => {
return new Promise((resolve, reject) => {
win.wavesurfer = win.WaveSurfer.create({
container: id,
url: '../../examples/audio/demo.wav',
width: '10rem',
})

win.wavesurfer.once('ready', () => {
cy.get(id).matchImageSnapshot('width-10rem')
win.wavesurfer.setOptions({ width: '200px' })
cy.get(id).matchImageSnapshot('width-200px')
resolve()
})
})
})
})
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions examples/all-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const options = {
container: 'body',
/** The height of the waveform in pixels */
height: 128,
/** The width of the waveform in pixels or any CSS value; defaults to 100% */
width: 300,
/** Render each audio channel as a separate waveform */
splitChannels: false,
/** Stretch the waveform to the full height */
Expand Down Expand Up @@ -69,6 +71,12 @@ const schema = {
max: 512,
step: 1,
},
width: {
value: 300,
min: 10,
max: 2000,
step: 1,
},
cursorWidth: {
value: 1,
min: 0,
Expand Down
7 changes: 7 additions & 0 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class Renderer extends EventEmitter<RendererEvents> {
<style>
:host {
user-select: none;
min-width: 1px;
}
:host audio {
display: block;
Expand Down Expand Up @@ -524,6 +525,12 @@ class Renderer extends EventEmitter<RendererEvents> {
this.progressWrapper.innerHTML = ''
this.wrapper.style.width = ''

// Width
if (this.options.width != null) {
this.scrollContainer.style.width =
typeof this.options.width === 'number' ? `${this.options.width}px` : this.options.width
}

// Determine the width of the waveform
const pixelRatio = window.devicePixelRatio || 1
const parentWidth = this.scrollContainer.clientWidth
Expand Down
4 changes: 3 additions & 1 deletion src/wavesurfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import Renderer from './renderer.js'
import Timer from './timer.js'

export type WaveSurferOptions = {
/** Required: an HTML element or selector where the waveform will be rendered. */
/** Required: an HTML element or selector where the waveform will be rendered */
container: HTMLElement | string
/** The height of the waveform in pixels, or "auto" to fill the container height */
height?: number | 'auto'
/** The width of the waveform in pixels or any CSS value; defaults to 100% */
width?: number | string
/** The color of the waveform */
waveColor?: string | string[] | CanvasGradient
/** The color of the progress mask */
Expand Down

0 comments on commit 5d7dd12

Please sign in to comment.