[d565449] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | var react_1 = require("react");
|
---|
| 4 | var util_1 = require("./misc/util");
|
---|
| 5 | var defaultState = {
|
---|
| 6 | acceleration: {
|
---|
| 7 | x: null,
|
---|
| 8 | y: null,
|
---|
| 9 | z: null,
|
---|
| 10 | },
|
---|
| 11 | accelerationIncludingGravity: {
|
---|
| 12 | x: null,
|
---|
| 13 | y: null,
|
---|
| 14 | z: null,
|
---|
| 15 | },
|
---|
| 16 | rotationRate: {
|
---|
| 17 | alpha: null,
|
---|
| 18 | beta: null,
|
---|
| 19 | gamma: null,
|
---|
| 20 | },
|
---|
| 21 | interval: 16,
|
---|
| 22 | };
|
---|
| 23 | var useMotion = function (initialState) {
|
---|
| 24 | if (initialState === void 0) { initialState = defaultState; }
|
---|
| 25 | var _a = react_1.useState(initialState), state = _a[0], setState = _a[1];
|
---|
| 26 | react_1.useEffect(function () {
|
---|
| 27 | var handler = function (event) {
|
---|
| 28 | var acceleration = event.acceleration, accelerationIncludingGravity = event.accelerationIncludingGravity, rotationRate = event.rotationRate, interval = event.interval;
|
---|
| 29 | setState({
|
---|
| 30 | acceleration: {
|
---|
| 31 | x: acceleration.x,
|
---|
| 32 | y: acceleration.y,
|
---|
| 33 | z: acceleration.z,
|
---|
| 34 | },
|
---|
| 35 | accelerationIncludingGravity: {
|
---|
| 36 | x: accelerationIncludingGravity.x,
|
---|
| 37 | y: accelerationIncludingGravity.y,
|
---|
| 38 | z: accelerationIncludingGravity.z,
|
---|
| 39 | },
|
---|
| 40 | rotationRate: {
|
---|
| 41 | alpha: rotationRate.alpha,
|
---|
| 42 | beta: rotationRate.beta,
|
---|
| 43 | gamma: rotationRate.gamma,
|
---|
| 44 | },
|
---|
| 45 | interval: interval,
|
---|
| 46 | });
|
---|
| 47 | };
|
---|
| 48 | util_1.on(window, 'devicemotion', handler);
|
---|
| 49 | return function () {
|
---|
| 50 | util_1.off(window, 'devicemotion', handler);
|
---|
| 51 | };
|
---|
| 52 | }, []);
|
---|
| 53 | return state;
|
---|
| 54 | };
|
---|
| 55 | exports.default = useMotion;
|
---|