-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
122 lines (107 loc) · 3.32 KB
/
script.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
114
115
116
117
118
119
120
121
122
$ = (s) => document.querySelector(s)
const magic8BallInit = () => {
var movementsCounter = 0,
shaking = false,
shaken = false,
shakeTimeout
function getMovementTotal(a) {
return Math.abs(a.x) + Math.abs(a.y)
}
function getAnswer() {
var responses = [
"It is certain",
"It is decidedly so",
"Without a doubt",
"Yes, definitely",
"You may rely on it",
"As I see it, yes",
"Most likely",
"Outlook good",
"Yes",
"Signs point to yes",
"Reply hazy try again",
"Ask again later",
"Better not tell you now",
"Cannot predict now",
"Concentrate and ask again",
"Don't count on it",
"My reply is no",
"My sources say no",
"Outlook not so good",
"Very doubtful"
]
return responses[Math.floor(Math.random() * responses.length)]
}
function showAnswer() {
answer.style = "opacity: 1"
answerText.innerText = getAnswer()
}
function motionHandler(frem) {
var e = window.motionEventData
let movementTotal = getMovementTotal(e.acceleration || e.accelerationIncludingGravity)
// debug.innerText = frem
if(getMovementTotal(e.acceleration) > 5) {
const x = Math.floor(e.acceleration.x) * 5
const y = Math.floor(e.acceleration.y*-1) * 5
eightball.style.transitionDuration = `0`
eightball.style.transform = `translate(${x}px, ${y}px)`
}
else {
eightball.style.transform = `none`
}
if(movementTotal > 10) {
movementsCounter++
if(movementsCounter > 15) {
shaking = true
answer.classList.add('bubbling')
clearTimeout(shakeTimeout) // Prevents multiple answers
shakeTimeout = setTimeout(() => {
shaken = true
}, 1000)
numbereight.style = "opacity: 0;display: none"
}
}
else if(movementTotal < 7) {
if(shaken === true) {
showAnswer()
shaking = false
shaken = false
answer.classList.add('active')
answer.classList.remove('bubbling')
}
movementsCounter = 0
}
}
window.addEventListener('devicemotion', e => {
if(window.motionAnimationFrame)
cancelAnimationFrame(window.motionAnimationFrame)
window.motionEventData = e
window.motionAnimationFrame = window.requestAnimationFrame(motionHandler)
})
numbereight.addEventListener('click', () => {
if(typeof DeviceMotionEvent != 'undefined' && DeviceMotionEvent) {
if(typeof DeviceMotionEvent.requestPermission == 'function') {
DeviceMotionEvent.requestPermission()
}
}
else {
requestAnimationFrame(function() {
document.querySelector('.prompt').style.opacity = 1
document.querySelector('.prompt').innerText = "Your device does not support motion"
document.querySelector('.prompt').style.color = "red"
})
}
numbereight.classList.add('inactive')
answer.classList.add('active')
document.querySelector('.prompt').style = 'opacity: 0'
})
}
magic8BallInit();
const iOSWebAppPopup = () => {
if(window.navigator.userAgent.match(/iPhone|iPad/) && ! window.navigator.standalone) {
const popup = $('#ios-add-to-homescreen-popup')
popup.style.display = "block"
popup.onclick = (e) => e.currentTarget.style.display = 'none'
}
}
iOSWebAppPopup()