lesca-sensor-motion
v2.2.0
Published
collect `device-motion` to a useful value. Include user permission and event listener.
Downloads
5
Readme
Why use it?
collect device-motion
to a useful value. Include user permission and event listener.
Live Demo
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