main
Last change
on this file since 0c6b92a was d565449, checked in by stefan toskovski <stefantoska84@…>, 3 months ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.6 KB
|
Rev | Line | |
---|
[d565449] | 1 | import { useEffect, useState } from 'react';
|
---|
| 2 | import { off, on } from './misc/util';
|
---|
| 3 | var defaultState = {
|
---|
| 4 | acceleration: {
|
---|
| 5 | x: null,
|
---|
| 6 | y: null,
|
---|
| 7 | z: null,
|
---|
| 8 | },
|
---|
| 9 | accelerationIncludingGravity: {
|
---|
| 10 | x: null,
|
---|
| 11 | y: null,
|
---|
| 12 | z: null,
|
---|
| 13 | },
|
---|
| 14 | rotationRate: {
|
---|
| 15 | alpha: null,
|
---|
| 16 | beta: null,
|
---|
| 17 | gamma: null,
|
---|
| 18 | },
|
---|
| 19 | interval: 16,
|
---|
| 20 | };
|
---|
| 21 | var useMotion = function (initialState) {
|
---|
| 22 | if (initialState === void 0) { initialState = defaultState; }
|
---|
| 23 | var _a = useState(initialState), state = _a[0], setState = _a[1];
|
---|
| 24 | useEffect(function () {
|
---|
| 25 | var handler = function (event) {
|
---|
| 26 | var acceleration = event.acceleration, accelerationIncludingGravity = event.accelerationIncludingGravity, rotationRate = event.rotationRate, interval = event.interval;
|
---|
| 27 | setState({
|
---|
| 28 | acceleration: {
|
---|
| 29 | x: acceleration.x,
|
---|
| 30 | y: acceleration.y,
|
---|
| 31 | z: acceleration.z,
|
---|
| 32 | },
|
---|
| 33 | accelerationIncludingGravity: {
|
---|
| 34 | x: accelerationIncludingGravity.x,
|
---|
| 35 | y: accelerationIncludingGravity.y,
|
---|
| 36 | z: accelerationIncludingGravity.z,
|
---|
| 37 | },
|
---|
| 38 | rotationRate: {
|
---|
| 39 | alpha: rotationRate.alpha,
|
---|
| 40 | beta: rotationRate.beta,
|
---|
| 41 | gamma: rotationRate.gamma,
|
---|
| 42 | },
|
---|
| 43 | interval: interval,
|
---|
| 44 | });
|
---|
| 45 | };
|
---|
| 46 | on(window, 'devicemotion', handler);
|
---|
| 47 | return function () {
|
---|
| 48 | off(window, 'devicemotion', handler);
|
---|
| 49 | };
|
---|
| 50 | }, []);
|
---|
| 51 | return state;
|
---|
| 52 | };
|
---|
| 53 | export default useMotion;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.