-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_popcorn.js
77 lines (61 loc) · 2.21 KB
/
move_popcorn.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
id = "popcorn_";
num_popped = 0;
async function move_popcorn() {
const timer = ms => new Promise(res => setTimeout(res, ms));
document.getElementById("start_popping").style.display = "none";
document.getElementById("stop_popping").style.display = "block";
while(document.getElementById("start_popping").style.display == "none") {
for(let i = 1; i < 10; i++) {
if (document.getElementById("start_popping").style.display != "none") {
break;
}
curr_id = id + i;
prev_num = i - 1;
if (prev_num == 0) {
prev_num = 9;
}
prev_id = id + prev_num;
num_popped++;
document.getElementById("num_popped").innerHTML = "Kernels Popped: " + num_popped;
document.getElementById(curr_id).style.marginTop = "-40px";
document.getElementById(curr_id).style.rotate = "360deg";
document.getElementById(prev_id).style.marginTop = "0px";
document.getElementById(prev_id).style.rotate = "0deg";
await timer(500);
}
}
}
function stop_popcorn() {
document.getElementById("start_popping").style.display = "block";
document.getElementById("stop_popping").style.display = "none";
for(let i = 1; i < 10; i++) {
curr_id = id + i;
document.getElementById(curr_id).style.marginTop = "0px";
}
}
num_list = [];
function make_num_list() {
for (i = 1; i < 10; i++) {
num_list.push(i);
}
}
function shuffle() {
currentIndex = num_list.length;
while (currentIndex != 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[num_list[currentIndex], num_list[randomIndex]] = [num_list[randomIndex], num_list[currentIndex]];
}
}
function create_fixed_elements() {
kernel_section = document.getElementById("popcorn_container");
total_add = "<tr>";
for(i = 0; i < num_list.length; i++) {
total_add += '<td><img src="images/single_popcorn.jpg" class="kernel" id="popcorn_' + num_list[i] + '"></td>';
}
total_add += "</tr>";
kernel_section.innerHTML = total_add;
}
make_num_list();
shuffle();
create_fixed_elements();