From 93ceb130d6b6128c1a243c0b726719f745e2cfd2 Mon Sep 17 00:00:00 2001 From: Kraig Walker Date: Wed, 11 Nov 2015 23:36:54 +0000 Subject: [PATCH] Add Feature Detect for Force Touch (AKA 3d Touch) Adds a feature detect for mouse force related events, and whether force values can be read from the MouseEvent object. --- feature-detects/forcetouch.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 feature-detects/forcetouch.js diff --git a/feature-detects/forcetouch.js b/feature-detects/forcetouch.js new file mode 100644 index 0000000000..b4cc43b198 --- /dev/null +++ b/feature-detects/forcetouch.js @@ -0,0 +1,29 @@ +/*! +{ + "name": "Force Touch Events", + "property": "forcetouch", + "authors": ["Kraig Walker"], + "notes": [{ + "name": "Responding to Force Touch Events from JavaScript", + "href": "https://developer.apple.com/library/prerelease/mac/documentation/AppleApplications/Conceptual/SafariJSProgTopics/Articles/RespondingtoForceTouchEventsfromJavaScript.html" + }] +} +!*/ +/* DOC +Tests whether the browser supports the detection of Force Touch Events. +Force Touch Events allow custom behaviours and interactions to take place based on the given pressure or change in pressure from a compatible trackpad. + +Force Touch events are available in OS X 10.11 and later on devices equipped with Force Touch trackpads. +*/ +define(['Modernizr', 'hasEvent', 'prefixed'], function(Modernizr, hasEvent, prefixed) { + Modernizr.addTest('forcetouch', function() { + // github.com/Modernizr/Modernizr/issues/1613 + // Test if the browser supports the force touch event progression (see notes link) + if (!hasEvent(prefixed('mouseforcewillbegin', window, false), window)) { + return false; + } + + // Test if the browser provides thresholds defining a "force touch" from a normal touch/click event + return MouseEvent.WEBKIT_FORCE_AT_MOUSE_DOWN && MouseEvent.WEBKIT_FORCE_AT_FORCE_MOUSE_DOWN; + }); +});