-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
executable file
·113 lines (104 loc) · 2.52 KB
/
App.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import React, { useState, useEffect } from "react";
import { StatusBar, Dimensions, Text, View } from "react-native";
import { DeviceMotion } from "expo-sensors";
import Svg, { Circle, Line } from "react-native-svg";
const { height, width } = Dimensions.get("window");
const centerX = width / 2,
centerY = height / 2;
export default function App() {
const [data, setData] = useState({});
useEffect(() => {
_subscribe();
StatusBar.setHidden(true, "fade");
return () => {
_unsubscribe();
};
}, []);
const _setInterval = () => {
DeviceMotion.setUpdateInterval(77);
};
const _subscribe = () => {
DeviceMotion.addListener((devicemotionData) => {
setData(devicemotionData.rotation);
});
_setInterval();
};
const _unsubscribe = () => {
DeviceMotion.removeAllListeners();
};
let { beta, gamma } = data;
gamma = round(gamma);
beta = round(beta);
const shadowElement = (
<>
<Line
x1={centerX}
y1={centerY}
x2={centerX - width / 4}
y2={centerY - height / 4}
stroke="#555"
strokeWidth="60"
/>
<Circle
cx={centerX - width / 4}
cy={centerY - height / 4}
r="30"
fill="#555"
/>
</>
);
const cylinderElement = (
<>
<Line
x1={centerX}
y1={centerY}
x2={centerX - (gamma * width) / 3.14}
y2={centerY - (beta * height) / 3.14}
stroke="lightgray"
strokeWidth="60"
/>
<Circle cx={centerX} cy={centerY} r="30" fill="lightgray" />
{beta >= 0 && (
<Line
x1={centerX}
y1={centerY + 30}
x2={centerX - (gamma * width) / 3.14}
y2={centerY + 30 - (beta * height) / 3.14}
stroke="gray"
strokeWidth="1"
/>
)}
<Circle
cx={centerX - (gamma * width) / 3.14}
cy={centerY - (beta * height) / 3.14}
r="30"
fill="gray"
/>
</>
);
const gammaAlign = gamma <= 1.57 && gamma >= -1.57;
return (
<View
style={{
justifyContent: "center",
alignItems: "center",
flex: 1,
backgroundColor: "#BBB",
}}
>
{gammaAlign && (
<Svg height={height} width={width} originX={centerX} originY={centerY}>
{shadowElement}
{cylinderElement}
</Svg>
)}
{!gammaAlign && <Text color="lightgray">Keep the Screen on Top!</Text>}
</View>
);
}
function round(n) {
if (!n) {
return 0;
}
return Math.floor(n * 100) / 100;
}