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

Update wavesurfer.minimap.js #955

Merged
merged 7 commits into from
Feb 7, 2017
Merged

Conversation

entonbiba
Copy link
Contributor

@entonbiba entonbiba commented Feb 2, 2017

renders the minimap without the need to resize the window. #954 #942 #951

Preview:
http://codepen.io/entonbiba/pen/ggeGyG

Changed

    this.wavesurfer.on('ready', this.render.bind(this));

to

    this.wavesurfer.on('ready', my.render.bind(this));

renders the minimap without the need to resize the window.
Changed 

        this.wavesurfer.on('ready', this.render.bind(this));

to 

        this.wavesurfer.on('ready', my.render.bind(this));
added the fixes for the drawPeaks()
@@ -94,7 +94,7 @@ WaveSurfer.Minimap = WaveSurfer.util.extend({}, WaveSurfer.Drawer, WaveSurfer.Dr

bindWaveSurferEvents: function () {
var my = this;
this.wavesurfer.on('ready', this.render.bind(this));
this.wavesurfer.on('ready', my.render());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is causing errors. I guess you meant my.render.bind(this)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error i get with my.render() is:

Uncaught TypeError: Cannot read property 'apply' of undefined
    at [...]/wavesurfer.js/dist/wavesurfer.js:736:15

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not needed at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@piem what code are you using that it gives the error?

@katspaugh without the change it wouldn't show the minimap on on ready event and the user is required to resize the window for it to work properly or call minimap.render(); right after.

    var minimap1 = wavesurfer.initMinimap({
        container: document.querySelector('#minimap'),
        height: 30,
        waveColor: '#ddd',
        progressColor: '#999',
        cursorColor: '#68A93D'
    });
    //without calling this it won't render on ready
    minimap1.render();

In the resize event it's calling my.render(); which makes it work when resizing the window. On the ready event it's not calling it so it won't render the minimap until you resize the window or call minimap.render() yourself after wavesurfer.initMinimap().

    var onResize = function () {
            if (prevWidth != my.wrapper.clientWidth) {
                prevWidth = my.wrapper.clientWidth;
                my.render();
                my.progress(my.wavesurfer.backend.getPlayedPercents());
            }
        };

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@piem @katspaugh happy with @entonbiba's feedback? thoughts?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.wavesurfer.on('ready', my.render()); is certainly wrong, since it's calling the render function immediately.

You need to either subscribe like the original code:

this.wavesurfer.on('ready', this.render.bind(this))):

Or simply call this.render().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@katspaugh yes this.render(); will work. I had already updated it to that in the preview code but didn't in this pull. I'll do that now.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@entonbiba you are missing the point.

this.wavesurfer.on('ready', someCallback) – that's a subscription. The first parameter is the name of an event, the second parameter is a function.

What you are doing is calling the callback immediately, instead of passing it as a parameter:
this.wavesurfer.on('ready', someCallback())

This is equivalent to

someCallback();
this.wavesurfer.on('ready', undefined)

So if you think the render method should be called immediately, just call it without the on-ready call.

check to see if a parameter called "miniRenderOnLoad" is set to true, if it is render the minimap on load.
@entonbiba
Copy link
Contributor Author

@katspaugh what about adding a check to see if a parameter called "miniRenderOnLoad" is set to true? It would make the rending on load optional instead of default. This way this.wavesurfer.on('ready', this.render.bind(this))): remains as default.

Something like this ok to add?

@katspaugh
Copy link
Owner

@entonbiba I don't really get why it's needed. How can it render the minimap (which is just a small copy of the waveform) before the main waveform is ready?

@@ -94,7 +94,17 @@ WaveSurfer.Minimap = WaveSurfer.util.extend({}, WaveSurfer.Drawer, WaveSurfer.Dr

bindWaveSurferEvents: function () {
var my = this;
this.wavesurfer.on('ready', this.render());
// check if parameter renderOnLoad is definied and set to true to render minimap on load
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo 'definied'

@@ -94,7 +94,17 @@ WaveSurfer.Minimap = WaveSurfer.util.extend({}, WaveSurfer.Drawer, WaveSurfer.Dr

bindWaveSurferEvents: function () {
var my = this;
this.wavesurfer.on('ready', this.render());
// check if parameter renderOnLoad is definied and set to true to render minimap on load
var miniRenderOnLoad = (this.params.miniRenderOnLoad||false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces around || missing

if (typeof this.params.miniRenderOnLoad !== "undefined") {
if (miniRenderOnLoad) {
this.render();
} else{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after else

} else{
this.wavesurfer.on('ready', this.render.bind(this))):
}
} else{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space after else

@entonbiba
Copy link
Contributor Author

entonbiba commented Feb 6, 2017

@katspaugh It will render after the initMinimap is called and if the parameter miniRenderOnLoad is set to true. Which will be inside the wavesurfer ready event. Therefore it will render after the main waveform is rendered, fixing the onload issue with the minimap.

I added the parameter as an option, in case someone doesn't want to render it right away. They will have to resize the window like that, the way it currently works. I still think it should render right away.

@katspaugh
Copy link
Owner

Let's just render it right away unconditionally, maybe?

will update docs and create example for minimap plugin
- minimap should be initialized within the main wavesurfer ready event.
@entonbiba
Copy link
Contributor Author

@katspaugh ok updated. I'll update the example and add it to the plugins section.

Once the debounce function is added to master, I'll update the resizing event for minimap. debounce pull request #964

@katspaugh katspaugh merged commit e633171 into katspaugh:master Feb 7, 2017
@katspaugh
Copy link
Owner

Merged, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants