Skip to content

Pinch Gesture on web platform #4535

Open

Description

@JoshBashed

Connected to: bevyengine/bevy#23421

Description

I am trying to detect pinch to zoom gestures when using winit on the web. This is problematic because pinch to zoom gestures use the same MouseWheel event scrolling uses. The only difference is that event.ctrlKey is set.

This results in a hacky solution to try to detect whether the control key is pressed by listening to two separate events.

Solution

Adding modifier state directly to the MouseWheel event and other events similar to it would allow higher ease of use for situations such as this one. Another solution could be to expose just a variable called is_pinching that represents the control key.

A new proposed solution is to send a pinch event when the ctrlKey modifier is sent during the event.

Example JS Code

// current
document.addEventListener('wheel', (e) => {
    dispatchModifiers(...)
    dispatchWheelEvent({ x: e.deltaX, y: e.deltaY });
});

// proposed
document.addEventListener('wheel', (e) => {
    dispatchModifiers(...)
    if (!e.ctrlKey) dispatchWheelEvent({ x: e.deltaX, y: e.deltaY, modifiers: ... });
    else dispatchPinchEvent({ … });
});

Activity

  1. madsmtm commented on Mar 19, 2026

    @madsmtm
  2. JoshBashed commented on Mar 19, 2026

    @JoshBashed
    Author

    We've recently talked about this in #4522 too. Would that work for you?

    This is a global. I think just adding it to the event is best.

  3. madsmtm commented on Mar 19, 2026

    @madsmtm
  4. JoshBashed commented on Mar 19, 2026

    @JoshBashed
    Author
  5. madsmtm commented on Mar 19, 2026

    @madsmtm
  6. JoshBashed commented on Mar 19, 2026

    @JoshBashed
    Author
  7. alice-i-cecile commented on Mar 19, 2026

    @alice-i-cecile
  8. JoshBashed commented on Mar 19, 2026

    @JoshBashed
    Author
  9. JoshBashed commented on Mar 19, 2026

    @JoshBashed
    Author
  10. madsmtm commented on Mar 19, 2026

    @madsmtm
  11. added
    DS - web
    S - api
    on Mar 19, 2026
  12. madsmtm commented on Mar 20, 2026

    @madsmtm
  13. kchibisov commented on Mar 21, 2026

    @kchibisov
  14. JoshBashed commented on Mar 22, 2026

    @JoshBashed
    Author
  15. kchibisov commented on Mar 22, 2026

    @kchibisov
  16. JoshBashed commented on Mar 22, 2026

    @JoshBashed
    Author
  17. JoshBashed commented on Mar 22, 2026

    @JoshBashed
    Author
  18. kchibisov commented on Mar 22, 2026

    @kchibisov
  19. changed the title [-]Design of the `ModifiersChanged` event is poor[/-] [+]Pinch Gesture on web platform[/+] on Mar 22, 2026
  20. JoshBashed commented on Mar 22, 2026

    @JoshBashed
    Author
  21. kchibisov commented on Mar 22, 2026

    @kchibisov
  22. kchibisov commented on Mar 22, 2026

    @kchibisov
  23. nicoburns commented on Mar 22, 2026

    @nicoburns
    Contributor

    I took a look into this, and my initial conclusion is that the correct thing to do here (specifically on the web platform) is to map wheel events with ctrlKey: true to PinchGesture events rather than MouseWheel events at the Winit level. This seems to have broad browser support and be a de facto standard (albeit a weird one). The only place this isn't supported is in Safari on iOS which has dedicated non-standard gesturestart and gesturechange events instead.

    Documented at: https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent#browser_compatibility

  24. JoshBashed commented on Mar 22, 2026

    @JoshBashed
    Author

    I took a look into this, and my initial conclusion is that the correct thing to do here (specifically on the web platform) is to map wheel events with ctrlKey: true to PinchGesture events rather than MouseWheel events at the Winit level. This seems to have broad browser support and be a de facto standard (albeit a weird one). The only place this isn't supported is in Safari on iOS which has dedicated non-standard gesturestart and gesturechange events instead.

    Documented at: https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent#browser_compatibility

    I really dislike using non-standard APIs, but I think it might be important to do so here. If not, we are ignoring a major platform. I'm happy to develop towards that goal.

    web_sys seems to support it with the cfg(web_sys_unstable_apis) feature. https://docs.rs/web-sys/latest/web_sys/struct.GestureEvent.html

  25. madsmtm commented on Mar 24, 2026

    @madsmtm
    Member

    I chimed in on the issue briefly on Bevy's Discord because it was being discussed there, the bulk of the discussion still happened here.

    I agree with Nico's assessment.

    web_sys seems to support it with the cfg(web_sys_unstable_apis) feature. https://docs.rs/web-sys/latest/web_sys/struct.GestureEvent.html

    I'm not 100% certain of how wasm-bindgen works, but I think we could safely copy-paste the definition to Winit as a fallback (and hopefully delete it in a few years if WebKit ever decides to be standards-conformant here).

  26. JoshBashed commented on Mar 24, 2026

    @JoshBashed
    Author

    I'm not 100% certain of how wasm-bindgen works, but I think we could safely copy-paste the definition to Winit as a fallback (and hopefully delete it in a few years if WebKit ever decides to be standards-conformant here).

    It actually is on computers. It provides more information through the gesture event API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DS - webS - api

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions