Skip to content

Commit 9264d23

Browse files
authored
Feat: restore exportImage (#3376)
* Feat: add exportImage * Tests
1 parent 2a333b9 commit 9264d23

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

cypress/e2e/basic.cy.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,38 @@ describe('WaveSurfer basic tests', () => {
179179
})
180180
})
181181

182+
describe('exportImage', () => {
183+
it('should export an image as a data-URI', () => {
184+
cy.window()
185+
.then((win) => {
186+
return win.wavesurfer.exportImage()
187+
})
188+
.then((data) => {
189+
expect(data[0]).to.match(/^data:image\/png;base64,/)
190+
})
191+
})
192+
193+
it('should export an image as a JPEG data-URI', () => {
194+
cy.window()
195+
.then((win) => {
196+
return win.wavesurfer.exportImage('image/jpeg', 0.75)
197+
})
198+
.then((data) => {
199+
expect(data[0]).to.match(/^data:image\/jpeg;base64,/)
200+
})
201+
})
202+
203+
it('should export an image as a blob', () => {
204+
cy.window()
205+
.then((win) => {
206+
return win.wavesurfer.exportImage('image/webp', 0.75, 'blob')
207+
})
208+
.then((data) => {
209+
expect(data[0]).to.be.a('blob')
210+
})
211+
})
212+
})
213+
182214
it('should destroy wavesurfer', () => {
183215
cy.window().then((win) => {
184216
win.wavesurfer.destroy()

src/renderer.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,34 @@ class Renderer extends EventEmitter<RendererEvents> {
644644
this.scrollIntoView(progress, isPlaying)
645645
}
646646
}
647+
648+
async exportImage(format: string, quality: number, type: 'dataURL' | 'blob'): Promise<string[] | Blob[]> {
649+
const canvases = this.canvasWrapper.querySelectorAll('canvas')
650+
if (!canvases.length) {
651+
throw new Error('No waveform data')
652+
}
653+
654+
// Data URLs
655+
if (type === 'dataURL') {
656+
const images = Array.from(canvases).map((canvas) => canvas.toDataURL(format, quality))
657+
return Promise.resolve(images)
658+
}
659+
660+
// Blobs
661+
return Promise.all(
662+
Array.from(canvases).map((canvas) => {
663+
return new Promise<Blob>((resolve, reject) => {
664+
canvas.toBlob(
665+
(blob) => {
666+
blob ? resolve(blob) : reject(new Error('Could not export image'))
667+
},
668+
format,
669+
quality,
670+
)
671+
})
672+
}),
673+
)
674+
}
647675
}
648676

649677
export default Renderer

src/wavesurfer.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,24 @@ class WaveSurfer extends Player<WaveSurferEvents> {
470470
this.initPlayerEvents()
471471
}
472472

473+
/**
474+
* Export the waveform image as a data-URI or a blob.
475+
*
476+
* @param format The format of the exported image, can be `image/png`, `image/jpeg`, `image/webp` or any other format supported by the browser.
477+
* @param quality The quality of the exported image, for `image/jpeg` or `image/webp`. Must be between 0 and 1.
478+
* @param type The type of the exported image, can be `dataURL` (default) or `blob`.
479+
* @returns A promise that resolves with an array of data-URLs or blobs, one for each canvas element.
480+
*/
481+
public async exportImage(format: string, quality: number, type: 'dataURL'): Promise<string[]>
482+
public async exportImage(format: string, quality: number, type: 'blob'): Promise<Blob[]>
483+
public async exportImage(
484+
format = 'image/png',
485+
quality = 1,
486+
type: 'dataURL' | 'blob' = 'dataURL',
487+
): Promise<string[] | Blob[]> {
488+
return this.renderer.exportImage(format, quality, type)
489+
}
490+
473491
/** Unmount wavesurfer */
474492
public destroy() {
475493
this.emit('destroy')

0 commit comments

Comments
 (0)