anvaka / panzoom

Universal pan and zoom library (DOM, SVG, Custom)
https://anvaka.github.io/panzoom/demo/attach-via-script.html
MIT License
1.82k stars 293 forks source link

onDoubleClick zoom in zoom out #331

Open Salitehkat opened 4 months ago

Salitehkat commented 4 months ago

Please, anyone knows how to zoom in (to the max) and then zoom out (to the min) using onDoubleClick? Thank you very much

jeanfrancoisgb commented 2 months ago

I'm looking for the same thing and even by trying to disabling the double click completely and manage it myself, it does not work.

HumboldtK commented 2 months ago

I was able to modify the default double click behavior so it zooms in to the max on double click and then zooms all the way back out on double click. I added var isZoomedIn = false; in the main panzoom.js file on line 119, 531c2c870d11145d8eec499348f6a6ff

Then with the function onDoubleClick on line 765 we can do

function onDoubleClick(e) {
              beforeDoubleClick(e);

              var offset = getOffsetXY(e);

              if (transformOrigin) {
                offset = getTransformOriginOffset();
              }

              var defaultZoom = 0.7; // Replace with your default zoom level
              var maxZoom = 100; // Replace with your maximum zoom level

              var currentScale = transform.scale;

              if (!isZoomedIn) {
                smoothZoom(offset.x, offset.y, maxZoom / currentScale);
                isZoomedIn = true;
              } else {
                smoothZoom(offset.x, offset.y, defaultZoom / currentScale);
                isZoomedIn = false;
              }
            }

The result is -

https://github.com/user-attachments/assets/f9f0367c-5a4e-4c66-b15c-0eeefc6fcb63