collect device-motion
to a useful value. Include user permission and event listener.
npm install lesca-sensor-motion --save
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>;
};
method | description | return |
---|---|---|
.permission() | require user permission | Promise |
.addEventListener(force : number, handler : function) |
exceeds the value of force | 20 |
.destroy() | destroy event | void |
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 |
- maintain if necessary