openseadragon

RemoveHandler in OpenSeadragon


I have problems to remove a handler from the viewer.

viewer.addHandler('viewport-change', function() {
     // do stuff works
});

viewer.addHandler('zoom', function() {
    if (viewer.viewport.getZoom() > threshold) {
        viewer.removeHandler('viewport-change', function() {
            console.log("removed");
        });
        console.log("Zoom:" + viewer.viewport.getZoom());
    }
});

I can see the output with the zoom factor, but I never saw the "removed" output. Also just adding and removing the "viewport-change"-handler did not worked. removeAllHandlers seems to work, but I fail with removing only one handler.

What I really try to do is something like a swipe effect. If the image is not zoomed in and the left edge hits the viewer border i want to show next image. Maybe there is a better way to do that.

Thanks in advance


Solution

  • In order to make removeHandler work, it needs to be the exact same function that you used with addHandler, like so:

    var viewportChangeHandler = function() {
         // do stuff works
    };
    
    viewer.addHandler('viewport-change', viewportChangeHandler);
    
    viewer.addHandler('zoom', function() {
        if (viewer.viewport.getZoom() > threshold) {
            viewer.removeHandler('viewport-change', viewportChangeHandler);
            console.log("Zoom:" + viewer.viewport.getZoom());
        }
    });
    

    That said, if all you want to do is detect swipes, just a handler on canvas-drag (plus some additional logic of your own writing) should be sufficient.