-
Notifications
You must be signed in to change notification settings - Fork 0
/
bouncing-balls.html
77 lines (76 loc) · 2.08 KB
/
bouncing-balls.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bouncing Balls</title>
<style>
.sprite {
position: absolute;
width: 10vh;
height: 10vh;
background-color: rgba(23, 55, 214, 0.621);
border-radius: 50%;
transform: scale(1);
top: 0vh;
left: 0vw;
transition: transform 1s ease-out,
background-color 1s linear,
top 0.1s linear,
left 0.1s linear;
}
.sprite.explosion {
transform: scale(8);
background-color: rgba(255, 0, 76, 0);
}
#score {
position: absolute;
top: 1vh;
left: 1vw;
font-size: 10vh;
}
</style>
</head>
<body>
<div id="score"></div>
</body>
<script>
let count = 10
let score = document.getElementById('score');
for (let i=0; i < count; i++) {
let sprite = document.createElement('div');
sprite.classList.add('sprite');
sprite.addEventListener('click', () => {
sprite.classList.add('explosion');
sprite.style.pointerEvents = 'none';
count = count - 1;
score.innerHTML = count;
sprite._killed = true;
},{once: true});
sprite.addEventListener('transitionend', () => {
if (sprite._killed) {
sprite.remove();
}
});
document.body.appendChild(sprite);
// closure
let ypos = 0;
let xpos = 0;
let yspeed = Math.random()+0.2;
let xspeed = Math.random()+0.2;
setInterval(() => {
ypos = ypos + yspeed;
xpos = xpos + xspeed;
sprite.style.top = ypos + 'vh';
sprite.style.left = xpos + 'vw';
if ( ypos > 90 || ypos < 0) {
yspeed = yspeed * -1;
}
if ( xpos > 90 || xpos < 0) {
xspeed = xspeed * -1;
}
},100);
}
score.innerHTML = count;
</script>
</html>