Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: avoid color bleed behind progress canvas #3285

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,17 +432,17 @@ class Renderer extends EventEmitter<RendererEvents> {
)

// Draw a progress canvas
const progressCanvas = canvas.cloneNode() as HTMLCanvasElement
progressContainer.appendChild(progressCanvas)
const progressCtx = progressCanvas.getContext('2d') as CanvasRenderingContext2D
if (canvas.width > 0 && canvas.height > 0) {
const progressCanvas = canvas.cloneNode() as HTMLCanvasElement
const progressCtx = progressCanvas.getContext('2d') as CanvasRenderingContext2D
progressCtx.drawImage(canvas, 0, 0)
// Set the composition method to draw only where the waveform is drawn
progressCtx.globalCompositeOperation = 'source-in'
progressCtx.fillStyle = this.convertColorValues(options.progressColor)
// This rectangle acts as a mask thanks to the composition method
progressCtx.fillRect(0, 0, canvas.width, canvas.height)
progressContainer.appendChild(progressCanvas)
}
// Set the composition method to draw only where the waveform is drawn
progressCtx.globalCompositeOperation = 'source-in'
progressCtx.fillStyle = this.convertColorValues(options.progressColor)
// This rectangle acts as a mask thanks to the composition method
progressCtx.fillRect(0, 0, canvas.width, canvas.height)
}

private renderChannel(channelData: Array<Float32Array | number[]>, options: WaveSurferOptions, width: number) {
Expand Down Expand Up @@ -632,9 +632,11 @@ class Renderer extends EventEmitter<RendererEvents> {

renderProgress(progress: number, isPlaying?: boolean) {
if (isNaN(progress)) return
this.progressWrapper.style.width = `${progress * 100}%`
this.cursor.style.left = `${progress * 100}%`
this.cursor.style.marginLeft = Math.round(progress * 100) === 100 ? `-${this.options.cursorWidth}px` : ''
const percents = progress * 100
this.canvasWrapper.style.clipPath = `polygon(${percents}% 0, 100% 0, 100% 100%, ${percents}% 100%)`
this.progressWrapper.style.width = `${percents}%`
this.cursor.style.left = `${percents}%`
this.cursor.style.marginLeft = Math.round(percents) === 100 ? `-${this.options.cursorWidth}px` : ''

if (this.isScrolling && this.options.autoScroll) {
this.scrollIntoView(progress, isPlaying)
Expand Down