Skip to content

small typescript module to detect shake in mobile devices

License

Notifications You must be signed in to change notification settings

izeryab/ShakeTS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

small typescript module to detect shake in mobile devices

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published