Catching Mac trackpad zoom

后端 未结 5 506
感动是毒
感动是毒 2020-12-24 03:32

Currently in my application i am catching the mouse wheel events and perform zoom in or out on a Canvas element. If user uses Mac and tries to perform zoom with the trackpad

相关标签:
5条回答
  • 2020-12-24 03:51

    As far as I know, the trackpad pinch doesn't trigger touch or gesture events, so Hammer.js will not detect it.

    I was able to simulate pinch detection in Chrome by using Hamster.js. It will trigger the mouse wheel event and you can use the delta to determine if the user is zooming in or out.

    This solution didn't work in Safari though.

    0 讨论(0)
  • 2020-12-24 03:55

    There is no way to catch the pinch zoom-in/zoom-out of the trackpad with only Javascript

    0 讨论(0)
  • 2020-12-24 03:57

    After looking around, does this help any ? it covers both web app and javascript in its discussion and comes to hammer.js as a possible solution to detecting pinch events... Simplest way to detect a pinch

    0 讨论(0)
  • 2020-12-24 04:16

    At least in Chrome, trackpad "pinch-to-zoom" triggers a wheel/mousewheel event that appears as though the ctrl key is pressed. You can capture this event just like any other wheel/mousewheel event and prevent its default from occurring. Here is an example using jQuery:

    $("canvas").on("mousewheel", function(e) {
        if (e.ctrlKey) {
            e.preventDefault();
            e.stopImmediatePropagation();
    
            // perform desired zoom action here
        }
    });
    
    0 讨论(0)
  • 2020-12-24 04:18

    Starting from Safari 9.1 you can catch zoom and rotation events from OSX devices. For more information read the GestureEvent Class Reference. Note that this only works in Safari but since your question was about "Mac trackpad zoom" I think this is what you are looking for.

    function zoom(e) {
      console.log(e.scale)
      e.preventDefault()
    }
    document.addEventListener('gesturestart', zoom)
    document.addEventListener('gesturechange', zoom)
    document.addEventListener('gestureend', zoom)


    Side note: these events are also supported in Safari on iOS.

    0 讨论(0)
提交回复
热议问题