
This can be done setting the fillParent property to false during the initialization and specifying a minimum width in pixels for every second rendered on the wave through the minPxPerSec property. So, if you don't want to apply the first solution, you can choose this one, that solves the problem specifying the width of the waveform manually. Specify width of waveform manuallyĪs mentioned, the problem happens when the container or the waveform itself is initially hidden when the wavesurfer instance is initialized. Hide the container once again ! Now WaveSurfer should have been rendered normallyĭocument.getElementById("hidden-container").style.display = 'none' Make the container temporarily visibleĭocument.getElementById("hidden-container").style.display = '' Attach on ready listener to WaveSurfer We should be able to render properly the waveform even when the container is initially hidden with the following JavaScript: // Initialize a simple instance of WaveSurfer This ensures that the problematic of the hidden div with no dimensions dissapears as the div will be drawn and therefore it will have the dimensions specified by the browser and Wavesurfer will be able to determine the waveform width automatically.įor example, given the following markup, where the container of the waveform is hidden: The first solution, that may be problematic for some people, is to make the div visible shortly, while the waveform is drawn and then hide it short after that. Force container to be visible and hide it again
#Wavesurfer width how to
In this short article, we will explain you how to draw the wave from an audio using WaveSurfer when the container is initially hidden. WaveSurfer has this problem, when the container or parent container where the waveform should be drawn, isn't visible and therefore it doesn't have defined dimensions (width and height), so when WaveSurfer is initialized with the fillParent property set to true, the waveform is of course drawn, but with 0px as width as it cannot determine the width of the container ! However, when i initialized the wave in the same page, but inside a visible container, the waveform appeared without issue. So, when i initialized a WaveSurfer instance inside one of those hidden tabs, the wave simply wasn't there. In Bootstrap 4, only the primary tab is visible, the rest are initially hidden. However, i noticed an interesting behaviour when working with this inside a Bootstrap 4 Tab. As always, the initialization is pretty straightforward and i never faced any problem with it. A couple of days ago, i needed to use the well known library WaveSurfer.js, a customizable audio waveform visualization, built on top of Web Audio API and HTML5 Canvas.
