-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
305 lines (243 loc) · 10.2 KB
/
script.js
File metadata and controls
305 lines (243 loc) · 10.2 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**=============================================================
*
** CLASSES
*
*=============================================================*/
/** ---------------------------------------------------------------------------
* *---------------------------- BUBBLE -------------------------------------
*
* For indivudal attributes assigned to each bubble:
* @param x Where bubble appears across x-axis of width
* @param delay Stagger entrance so new bubbles appear at different times
* @param rate Rate of speed bubble moves to top
* @param scale Transform size of the bubbles toward the top of screen
* @param size Vary the size of the bubbles
* ---------------------------------------------------------------------------- */
class Bubble {
constructor (x, delay, rate, scale, size) {
this.x = x;
this.delay = delay;
this.rate = rate;
this.scale = scale;
this.size = size;
}
}
/** -----------------------------------------------------------------------------
* *--------------------------- MULTIBUBBLE ------------------------------------
* -----------------------------------------------------------------------------*/
class Multibubble {
constructor() {
this.bubbles = [];
this.makeBubble();
this.render();
}
// Function adds 5 individualized bubbles, push to this.bubbles array
makeBubble() {
// Array to iterate for 5 unique bubbles
let bubbleDivs = ['bubble1', 'bubble2', 'bubble3', 'bubble4', 'bubble5'];
for (let i = 0; i < bubbleDivs.length; i++) {
let moveX = Math.floor(Math.random() * 100);
let wait = Math.floor(Math.random() * 5);
let rateArray = [2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6];
let speed = rateArray[Math.floor(Math.random() * rateArray.length)];
let scales = [0.25, 0.5, 0.75, 1.0, 1.25, 1.5];
let randomScale = scales[Math.floor(Math.random() * scales.length)];
let sizes = [25, 50, 75, 100, 125];
let randomSize = sizes[Math.floor(Math.random() * sizes.length)]
// Assign to Bubble class values
Bubble.x = moveX;
Bubble.delay = wait;
Bubble.rate = speed;
Bubble.scale = randomScale;
Bubble.size = randomSize;
// Push to bubbles array
this.bubbles.push(new Bubble(moveX, wait, speed, randomScale, randomSize));
}
}
// Add to the DOM
render() {
for (let i = 0; i < this.bubbles.length; i++) {
let div = document.createElement('div');
div.classList = 'bubble';
document.getElementById('main').appendChild(div);
let enter = this.bubbles[i].x;
div.style.left = enter + "%";
let quick = this.bubbles[i].rate;
div.style.animationDuration = quick + "s";
let stagger = this.bubbles[i].delay;
div.style.animationDelay = stagger + "s";
let scaleUpDown = this.bubbles[i].scale;
div.style.transform = "translateY(-100%) scale("+ scaleUpDown + ")";
let widthHeight = this.bubbles[i].size;
div.style.width = widthHeight + "px";
div.style.height = widthHeight + "px";
//Match new bubbles to existing class on other bubbles
const mainClass = document.getElementById('main').classList;
if (mainClass == 'main bigger') {
div.classList.toggle('bigger');
};
if (mainClass == 'main goth') {
div.classList.toggle('goth');
};
if (mainClass == 'main disco') {
div.classList.toggle('disco');
};
if (mainClass == 'main fancy') {
div.classList.toggle('fancy');
};
}
}
}
/** -----------------------------------------------------------------------
* *----------------------------- EXTRABUBBLE ----------------------------
*
* Extends the Multibubble class so the factory doesn't
* repeat the previous values when adding on
* more bubbles with the "MORE BUBBLES" button.
* See note at bottom of doc for details.
*
*-------------------------------------------------------------------------*/
class Extrabubble extends Multibubble {
makeBubbleSize() {
super.makeBubble();
}
renderNewBubble() {
super.render();
}
}
//*------ FIRST BUBBLES ON PAGE LOAD ------
const myBubbles = new Multibubble;
//--------------------------------------------------------------------------------------------------
/**============================================================
*
** HTML LINKS & EVENT LISTENERS
*
*=============================================================*/
//--------------------------------------------------------------------------------------------------
// **-------- GLOBAL VARIABLES & FUNCTIONS ----------
const main = document.getElementById('main');
// Toggle on/off element's class
function toggleBubbles(elem) {
document.querySelectorAll('.bubble').forEach(bubble => {
bubble.classList.toggle(elem);
});
}
// Remove classes not on element
function removeClass(elem) {
main.classList.remove(elem);
document.querySelectorAll('.bubble').forEach(bubble => {
bubble.classList.remove(elem)});
}
//------------------------------------------------------------------------------------------------
// **-------- MORE BUBBLES button ------
const bubbleButton = document.getElementById('more_bubbles');
bubbleButton.addEventListener('click', () => {
const extra = new Extrabubble;
})
//------------------------------------------------------------------------------------------------
// **------ FASTER button ------
// Can't toggle class like most buttons because it needs to
// increase every single click
const faster = document.getElementById('faster');
faster.addEventListener('click', () => {
// Declare all appended children with class "bubble"
let allBubbles = document.getElementsByClassName('bubble');
for (let i = 0; i < allBubbles.length; i++) {
let speed = allBubbles[i].style.animationDuration;
let oldSpeed = parseFloat(speed);
let newSpeed = oldSpeed - 0.5;
allBubbles[i].style.removeProperty("animation-duration");
if (newSpeed > 0.6) {
allBubbles[i].style.animationDuration = newSpeed + "s";
} else {
// cap speed at half a second
allBubbles[i].style.animationDuration = "0.5s"
}
}
})
//------------------------------------------------------------------------------------------------
//* SLOWER BUTTON
// Works exact same as "FASTER" button except that it adds time to the duration
// instead of subtracting it. Max "slowness": 20 seconds.
const slower = document.getElementById('slower');
slower.addEventListener('click', () => {
let allBubbles = document.getElementsByClassName('bubble');
for (let i = 0; i < allBubbles.length; i++) {
let speed = allBubbles[i].style.animationDuration;
let oldSpeed = parseFloat(speed);
let newSpeed = oldSpeed + 0.5;
allBubbles[i].style.removeProperty("animation-duration");
if (newSpeed < 20) {
allBubbles[i].style.animationDuration = `${newSpeed}s`;
} else {
allBubbles[i].style.animationDuration = `20s`
}
}
})
//------------------------------------------------------------------------------------------------
//** ------ BIGGER BUTTON ------
const bigger = document.getElementById('bigger');
bigger.addEventListener('click', () => {
toggleBubbles('bigger');
main.classList.toggle('bigger');
})
//------------------------------------------------------------------------------------------------
//** ------ GOTH BUTTON ------
const goth = document.getElementById('goth');
goth.addEventListener('click', () => {
removeClass('fancy');
removeClass('disco');
toggleBubbles('goth');
main.classList.toggle('goth');
})
//------------------------------------------------------------------------------------------------
//** ------ DISCO BUTTON ------
const disco = document.getElementById('disco');
disco.addEventListener('click', () => {
removeClass('goth');
removeClass('fancy');
toggleBubbles('disco');
main.classList.toggle('disco');
})
//------------------------------------------------------------------------------------------------
//** ------ FANCY BUTTON ------
const fancy = document.getElementById('fancy');
fancy.addEventListener('click', () => {
removeClass('goth');
removeClass('disco');
toggleBubbles('fancy');
main.classList.toggle('fancy');
})
//------------------------------------------------------------------------------------------------
//* *------ RESET button to clear screen ------
const resetButton = document.getElementById('reset');
resetButton.addEventListener('click', () => {
let bubbleField = document.getElementsByClassName('bubble');
// Iterate and remove each child
for (let i = bubbleField.length - 1; i >= 0; i--) {
let pop = bubbleField[i];
main.removeChild(pop);
main.className = 'main';
}
const resetBubbles = new Extrabubble;
})
//------------------------------------------------------------------------------------------------
//===================================================================================================
/** NOTE:
* ! What's up with the EXTRABUBBLE class?
*
* Clicking the "MORE BUBBLES" button without this extended class
* made a duplicate of the previous added class instead of just adding 5 more.
*
* For example:
* (Letters represent indivudual bubbles)
*
* ON LOAD [5 total]: A B C D E
* ONE CLICK [15 total]: A B C D E A B C D E F G H I J
* TWO CLICKS [30 total]: A B C D E A B C D E F G H I J A B C D E F G H I J K L M N O
*
* By extending the class and using "super" to copy methods,
* the functon creates and pushes unique bubbles to the
* Bubble class and bubbles array.
*
*/