Skip to content

Latest commit

 

History

History
49 lines (38 loc) · 1.28 KB

README.md

File metadata and controls

49 lines (38 loc) · 1.28 KB

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