/
ExponentDeviceMotion.web.ts
39 lines (36 loc) · 1.11 KB
/
ExponentDeviceMotion.web.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { SyntheticPlatformEmitter } from '@unimodules/core';
import { isSensorEnabledAsync } from './utils/isSensorEnabledAsync.web';
const eventName = 'devicemotion';
export default {
get name(): string {
return 'ExponentDeviceMotion';
},
/**
* Gravity on the planet this module supports (currently just Earth) represented as m/s^2.
*/
get Gravity(): number {
return 9.80665;
},
async isAvailableAsync(): Promise<boolean> {
if (typeof DeviceMotionEvent === 'undefined') {
return false;
}
return await isSensorEnabledAsync(eventName);
},
_handleMotion(motion) {
// TODO: Bacon: Can rotation be calculated?
SyntheticPlatformEmitter.emit('deviceMotionDidUpdate', {
acceleration: motion.acceleration,
accelerationIncludingGravity: motion.accelerationIncludingGravity,
interval: motion.interval,
rotationRate: motion.rotationRate,
orientation: window.orientation,
});
},
startObserving() {
window.addEventListener(eventName, this._handleMotion);
},
stopObserving() {
window.removeEventListener(eventName, this._handleMotion);
},
};