-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgyroscope.html
65 lines (64 loc) · 1.61 KB
/
gyroscope.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<title>Title</title>
<style>
html{
height: 100%;
}
body{
height: 100%;
margin:0;
position: relative;
-webkit-perspective: 300px;
perspective: 300px;
}
#box{
position: absolute;
left: 50%;
top: 50%;
margin: -100px 0 0 -100px;
width: 200px;
height: 200px;
background: red;
}
</style>
<script src="../public/js/m.Tween.js"></script>
</head>
<body>
<div id="box"></div>
</body>
<script>
const box = document.getElementById('box');
let isStart = true;
let lastX = 0;
let lastY = 0;
let startDeg = {};
let initElDeg = {};
window.addEventListener('deviceorientation',function (ev) {
let x = ev.beta;
let y = ev.gamma;
if(isStart){
//start
initElDeg.x = css(box,'rotateX');
initElDeg.y = css(box,'rotateY');
startDeg.x = x;
startDeg.y = y;
isStart = false;
}else{
//move
if(Math.abs(x-lastX)>1 || Math.abs(y-lastY)>1){
let dis = {};
dis.x = x - startDeg.x;
dis.y = y -startDeg.y;
css(box,'rotateX',initElDeg.x+dis.x);
css(box,'rotateY',initElDeg.y+dis.y);
lastX = x;
lastY = y;
}
}
})
</script>
</html>