-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgyroscope.js
79 lines (69 loc) · 1.99 KB
/
gyroscope.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const gyroscope = {
//sensor refresh rate 1/60
rotateDisplay: 0,
leftToRight: 0,
frontToBack:0,
movementTowardsToAway: 0,
movementLeftToRight: 0,
movementUpToDown: 0,
//
// DeviceOrientationEvent
//
requestDeviceOrientation: function() {
if (
typeof DeviceOrientationEvent !== "undefined" &&
typeof DeviceOrientationEvent.requestPermission === "function"
) {
DeviceOrientationEvent.requestPermission()
.then((permissionState) => {
if (permissionState === "granted") {
//what to do with gyroscope: >IOS13
gyroscope.getOrientationData();
}
})
.catch(console.error);
} else {
//what to do with gyroscope: other devices
gyroscope.getOrientationData();
}
},
//
// DeviceMotionEvent
//
requestDeviceMotion: function() {
if (
typeof DeviceMotionEvent !== "undefined" &&
typeof DeviceMotionEvent.requestPermission === "function"
) {
DeviceMotionEvent.requestPermission()
.then((permissionState) => {
if (permissionState === "granted") {
//what to do with gyroscope: >IOS13
gyroscope.getMotionData();
}
})
.catch(console.error);
} else {
//what to do with gyroscope: other devices
gyroscope.getMotionData();
}
},
// ---------------
getOrientationData: function() {
window.addEventListener("deviceorientation", (event) => {
gyroscope.rotateDisplay = event.alpha;
gyroscope.frontToBack = event.beta;
gyroscope.leftToRight = event.gamma;
// updateGyroscope();
});
},
getMotionData: function() {
window.addEventListener("devicemotion", (event) => {
gyroscope.movementTowardsToAway = event.acceleration.z;
gyroscope.movementLeftToRight = event.acceleration.x;
gyroscope.movementUpToDown = event.acceleration.y;
// updateGyroscope();
});
}
}
export{gyroscope};