So I started exploring this a few months ago and this weekend I stumbled upon this bug, 1052253 — OS X: Pinch to zoom gesture should map to mousewheel with the control key, like Chrome, in the Mozilla bug tracker and that finally made things click.
Apparently Microsoft with IE10 was the pioneers here, as they enabled pinch-to-zoom gestures from multi-touch trackpad’s to be surfaced as
mousewheelevent with the
ctrlmodifier set to true.
The Chrome team had a discussion about this back in January 2014, and the adaption landed in Chrome M35 as per documented in this bug. Mozilla followed up with parity and landed the support in Firefox 55.
The spec has an example that says: "The user's environment might be configured to associate vertical scrolling with rotation along the y-axis, horizontal scrolling with rotation along the x-axis, and zooming with rotation along the z-axis." From this perspective it seems reasonable to generate deltaZ wheel events for trackpad pinch gestures. This would enable apps like google maps to respond nicely to pinch.
So that was a bit of the historic context. Today this means that we have a de-facto “hack” standard to detect pinch-to-zoom gestures from trackpads, which is supported in multiple browsers.
So how does it work?
You can detect a pinch-to-zoom gesture with this quite simple event handler listening for the
wheel event, where the
e.deltaY value represents your zoom/scale factor when the
e.ctrlKey is set.
The browser support seems to be as following:
After the ctrl key modifer hacker landed there seem to have been a discussion on how this could formalized into a better API as per this dicussion on Twitter:
In March 2016 Apple has shipped new a
GestureEvent for Webkit that shipped as a part of Safari 9.1, and this new event model allow us to detect gesture rotation and much.
You can read more about
GestureEvents in the Apple documentation here: https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW23
I’ve put together a simple demo of a
gestureEventswhen in Webkit. The latter enables rotation of the element, which is quite cool:
Another cool demo I found is using WebGL and the performance is even better: http://jsbin.com/fepuficudolo/3/edit?html,output
Today there doesn't seem to be a web standard for this, but there’s requests opened on the W3C UI Events specification here https://github.com/w3c/uievents/issues/31, and there’s a separate discussion on extending PointerEvents to expose raw Trackpad events here https://github.com/w3c/pointerevents/issues/206, so maybe one day we’ll have a web standard for this. Personally I would expect PointerEvents as the place for this.
Hope that clarified a few things. At least it did for me.