Awesome Open Source
Awesome Open Source

ShakeTS

small library to detect shake in mobile devices

How to use this

Create Method like:

detectShake() {
//Create object
var myShakeEvent = new Shake();
// Start listening to device motion
myShakeEvent.start();

// Register a shake event listener on window with your callback
window.addEventListener("shake", shakeEventDidOccur, false);

//function to call when shake occurs
function shakeEventDidOccur() {
 // myShakeEvent.reset();
 //do something on shake
 console.log("Shake Occur");
}
}

FOR IOS we may require permission to access device motion sensor (using user action)

window.document.onclick = function () {
      if (
        typeof (DeviceMotionEvent as any).requestPermission()
          .requestPermission === "function"
      ) {
        (DeviceMotionEvent as any)
          .requestPermission()
          .then((permissionState) => {
            if (permissionState === "granted") {
              that.detectShake();
            }
          })
          .catch(console.error);
      } else {
        // handle regular non iOS 13+ devices
        that.detectShake();
      }
    };

For Android device simply use detectshake method


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Javascript (1,158,774
Es6 (1,158,773
Typescript (307,126
Es5 (687
Shake Detection (15