-
Notifications
You must be signed in to change notification settings - Fork 0
/
bricks.html
183 lines (157 loc) · 4.04 KB
/
bricks.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
padding: 0;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
#stage {
position: relative;
width : 100%;
height: 95%;
overflow: hidden;
}
#paddle {
position: absolute;
bottom: 0px;
left: 0px;
width: 50px;
height: 10px;
background: #024;
border-radius: 3px;
}
#score {
position: absolute;
top: 50%;
left: 50%;
font-size: 100px;
color: #ddd;
}
.ball {
border-radius: 100%;
background: #444;
position: absolute;
}
</style>
</head>
<body>
<div id="console" style=""></div>
<div>high score: <span id="high-score" style="">0</span></div>
<div id="score" style="">0</div>
<div id="stage">
<div id="paddle"></div>
</div>
<script>
let score = 0
let highScore = 0
const logConsole = document.getElementById('console')
const $score = document.getElementById('score')
const $highScore = document.getElementById('high-score')
const paddle = document.getElementById('paddle')
const stage = document.getElementById('stage')
const paddleWidth = paddle.clientWidth
const stageWidth = stage.clientWidth
const stageHeight = stage.clientHeight
const paddleRange = stageWidth - paddleWidth
// const maxLeft = garden.clientWidth - ball.clientWidth;
// const maxRight = garden.clientHeight - ball.clientHeight;
const maxLeftAngle = 110;
const maxRightAngle = 70;
function log(dat) {
logConsole.innerHTML = dat
}
console.log(stageWidth)
console.log(paddleWidth)
const paddleStartLeft = (stageWidth/2 - paddleWidth/2)
console.log('paddleStartLeft', paddleStartLeft)
paddle.style.left = paddleStartLeft + 'px'
function paddleMoved(ev) {
let sensorAngle = ev.alpha || 0
// make starting angle = 0 (while pointing straight)
let angle = 90 - sensorAngle
// restrict between -20 and +20
if (angle < -20) angle = -20
else if ((angle > 20)) angle = 20
// bring angle within 0 - 40 degrees
angle = angle + 20
let paddleLeft = (angle/40 * paddleRange)
let logData = sensorAngle.toFixed(2) + ' ' + angle.toFixed(2) + ' ' + paddleLeft.toFixed(2)
// log(logData)
// console.log(logData)
paddle.style.left = paddleLeft + 'px'
}
async function dropBall() {
const ballSize = 15
const ballLeft = Math.random() * (stageWidth - ballSize)
let ball = document.createElement('div')
ball.className = 'ball'
ball.style.left = ballLeft + 'px'
ball.style.width = ball.style.height = ballSize + 'px'
stage.appendChild(ball)
// ball.addEventListener('animationend', () => {
// console.log('Animation ended');
// });
let ballAnimation = ball.animate(
{
top: ['0px', `${stageHeight}px`]
},
{
duration: 1760, // number in ms [this would be equiv of your speed].
// easing: 'ease-in',
iterations: 1,
}
);
ballAnimation.finished.then(() => {
// check if ball is within paddle
// console.log(paddle.style.left)
const paddleLeft = parseInt(paddle.style.left)
if (paddleLeft >= (ballLeft - paddleWidth) && paddleLeft <= (ballLeft)) {
score++
if (highScore < score) {
highScore = score
}
} else {
score = 0
}
$score.innerHTML = score
$highScore.innerHTML = highScore
ball.remove()
dropBall()
})
// function move(el) {
// let top = 0
// function frame() {
// top++
// el.style.top = top + 'px'
// if (top >= stageHeight) {
// clearInterval(id)
// console.log(el)
// }
// }
// let id = setInterval(frame, 10) // draw every 10ms
// }
// move(ball)
}
window.onload = async function () {
if (DeviceOrientationEvent && typeof DeviceOrientationEvent.requestPermission === 'function') {
DeviceOrientationEvent.requestPermission()
.then(response => {
if (response == 'granted') {
window.addEventListener('deviceorientation', (ev) => {
paddleMoved(ev)
})
}
})
.catch(console.error)
} else {
window.addEventListener('deviceorientation', (ev) => {
paddleMoved(ev)
});
}
dropBall()
}
</script>
</body>
</html>