Skip to content

Commit

Permalink
Fix: very wide container canvas width
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Aug 11, 2024
1 parent 15ac8d7 commit a1aa5a5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
27 changes: 25 additions & 2 deletions cypress/e2e/options.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ describe('WaveSurfer options tests', () => {
})
})

it('should load a blob', () => {
it('should load a blob', (done) => {
cy.window().then((win) => {
const wavesurfer = win.WaveSurfer.create({
container: id,
Expand All @@ -616,7 +616,30 @@ describe('WaveSurfer options tests', () => {
10,
)

cy.get(id).matchImageSnapshot('loadBlob')
wrapReady(wavesurfer).then(() => {
cy.get(id).matchImageSnapshot('loadBlob')
done()
})
})
})

it('should render in a very wide container', (done) => {
cy.window().then((win) => {
const container = win.document.querySelector(id)
container.style.width = '21000px'

const wavesurfer = win.WaveSurfer.create({
container,
url: '../../examples/audio/demo.wav',
peaks: new Array(512).fill(0.5).map((v, i) => v * Math.sin(i / 16)),
})

cy.get(id).matchImageSnapshot('very-wide-container')

wrapReady(wavesurfer).then(() => {
container.style.width = ''
done()
})
})
})
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 11 additions & 7 deletions src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,14 +520,8 @@ class Renderer extends EventEmitter<RendererEvents> {
) {
const pixelRatio = this.getPixelRatio()
const { clientWidth } = this.scrollContainer

// Render a single canvas if it fits in the viewport
if (clientWidth * pixelRatio >= width) {
this.renderSingleCanvas(channelData, options, clientWidth, height, 0, canvasContainer, progressContainer)
return
}

const totalWidth = width / pixelRatio

let singleCanvasWidth = Math.min(Renderer.MAX_CANVAS_WIDTH, clientWidth, totalWidth)
let drawnIndexes: Record<number, boolean> = {}

Expand Down Expand Up @@ -568,6 +562,16 @@ class Renderer extends EventEmitter<RendererEvents> {

// Calculate how many canvases to render
const numCanvases = Math.ceil(totalWidth / singleCanvasWidth)

// Render all canvases if the waveform doesn't scroll
if (!this.isScrollable) {
for (let i = 0; i < numCanvases; i++) {
draw(i)
}
return
}

// Lazy rendering
const viewPosition = this.scrollContainer.scrollLeft / totalWidth
const startCanvas = Math.floor(viewPosition * numCanvases)

Expand Down

0 comments on commit a1aa5a5

Please sign in to comment.