Skip to content

The mobile device motion. include request permission and return user shaking gravity value.

License

Notifications You must be signed in to change notification settings

jameshsu1125/lesca-sensor-motion

Repository files navigation

React React React React React NPM React NPM

Why use it?

collect device-motion to a useful value. Include user permission and event listener.

Installation

npm install lesca-sensor-motion --save

Usage

As a Node module:

import { useState, useEffect, useMemo } from 'react';
import Motion from 'lesca-sensor-motion';

// (1) waiting for permission => Must be user-triggered event and SSL required
// (2) add addListener
const Components = () => {
  const [state, setState] = useState(false);
  const motion = useMemo(() => new Motion(), []);

  const require_permission = () => {
    motion
      .permission()
      .then(() => {
        // permission granted
        setState(true);
      })
      .catch(() => {
        // permission denied
      });
  };

  useEffect(() => {
    if (state) {
      motion.addEventListener(20, (e) => {
        alert(e);
      });
    }
    return () => {
      motion.destroy();
    };
  }, [state]);

  return <button onClick={require_permission}>click me</button>;
};

Development

Methods

method description return
.permission() require user permission Promise
.addEventListener(force: number, handler: function) exceeds the value of force 20
.destroy() destroy event void

Properties

Properties description default
.each : int time of force update 1
.delay : int time delay of callback called 1000
.disable : boolean stop / continue event listen true
.isSupport : boolean permission granted or denied false

Features

  • maintain if necessary

About

The mobile device motion. include request permission and return user shaking gravity value.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published