npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shake-detector

v0.3.8

Published

Mobile device shakes detection

Downloads

72

Readme

npm npm bundle size NPM GitHub top language

shake-detector

Mobile device shakes detection

Setup

NPM

npm i shake-detector
import ShakeDetector from 'shake-detector';
// ...
const shakeDetector = new ShakeDetector();

CDN

<script src="https://unpkg.com/shake-detector"></script>
const shakeDetector = new window.ShakeDetector();

IOS limitation

IOS (since 12.2) requires a user's permission to listen to the motion events, and it could be obtained only in response to user interaction. ShakeDetector will do nothing on IOS 12.2+ in case that such permission was not granted.

There are two options:

  1. Notify ShakeDetector that your page has the permission
shakeDetector.confirmPermissionGranted();
shakeDetector.start();
  1. Request the permission via ShakeDetector
const requestTrigger = document.getElementById('requestTrigger');
// requestPermission argument is optional
// default is document.documentElement
shakeDetector.requestPermission(requestTrigger).then(() => {
    shakeDetector.start();
});

In this case ShakeDetector will set a one-time click event listener and will request the permission on click.

Options

const options = {
    threshold: 8,
    debounceDelay: 500,
};
const shakeDetector = new ShakeDetector(options);

| Option | Type | Default | Measure | Description | | --------------: | :----: | ------: | :-------------: | :--------------------------------------------------------------------------------------- | | threshold | number | 15 | m/s2 | device acceleration that will be registered as a shake | | debounceDelay | number | 1000 | ms | shake won't be registered if this amount of time has not passed after the previous shake |

API

| Method | Arguments | Return | Description | | -------------------------: | :-----------------------------: | :-------------------------: | :---------------------------------------------------------------------------------------------------- | | subscribe | listener{function} | ShakeDetectorinstance | Adds a handler to the shake event handlers pool | | unsubscribe | listener{function} | ShakeDetectorinstance | Removes a handler from the shake event handlers pool | | start | {void} | ShakeDetectorinstance | Starts monitoring the motion event | | stop | {void} | ShakeDetectorinstance | Stops monitoring the motion event | | confirmPermissionGranted | {void} | ShakeDetectorinstance | Notifies the detector that permission to listen to the motion events has already been granted | | requestPermission | triggerElement{HTMLElement} | Promise | Requests a user's permission to listen to the motion events |

Chaining

All ShakeDetector methods except requestPermission return its instance for chaining.

const onShake = () => {
    console.log('shake!');
};
new ShakeDetector().confirmPermissionGranted().subscribe(onShake).start();

Shake event

Once shake is detected, ShakeDetector fire an event on window. One can use it instead of the subscription via subscribe method.

const onShake = () => {
    console.log('shake!');
};
const shakeDetector = new ShakeDetector();
shakeDetector.start();
window.addEventListener(ShakeDetector.SHAKE_EVENT, onShake);

Documentation

Please find the full docs here

Roadmap

  • demo page with configuration options
  • provide magnitude of the shake for the shake listeners - how energetic the shake was

License

Copyright © 2021, Sergey Chernykh. Released under the MIT License.