-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.js
386 lines (287 loc) · 12.5 KB
/
testing.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
document.addEventListener("DOMContentLoaded", function () {
/*
const sphere = document.querySelector(".sphere");
// Get the shockwave element
const shockwave = document.querySelector(".shockwave");
const fadein = document.querySelector(".fadein");
// Get the lower-middle part of the screen
const lowerMiddleX = window.innerWidth / 2;
const lowerMiddleY = window.innerHeight * 0.75;
// Trigger animation on page load
sphere.style.animationPlayState = "running";
// Listen for the end of the sphere animation
sphere.addEventListener("animationend", function () {
sphere.style.animationPlayState = "paused";
sphere.remove();
// Set shockwave position to lower-middle
shockwave.style.left = lowerMiddleX + "px";
shockwave.style.top = lowerMiddleY + "px";
// Trigger shockwave animation
shockwave.style.animationPlayState = "running";
setTimeout(() => {
fadein.style.animationPlayState = "running";
}, 4500);
});
shockwave.addEventListener("animationend", function () {
// Reset shockwave animation state
const scene = document.getElementById("scene");
scene.style.display = "none";
const fadein = document.getElementById(fadein);
fadein.style.display = "block";
fadein.style.animationPlayState = "running";
});
fadein.addEventListener("animationend", function () {
// Reset shockwave animation state
const scene = document.getElementById("scene");
scene.style.display = "none";
});
*/
const rainContainer = document.getElementById("rain");
const follower = document.getElementById("follower");
const container = document.querySelector(".container");
const ring = document.querySelector(".ring");
let prevX, prevY;
let gust = "fall";
let opened = false;
// Preload the opened umbrella image
const openedUmbrellaImage = new Image();
openedUmbrellaImage.src = "openedumbrella.png";
let isMouseDown = false;
let pickedUp = false;
// TESTING
function createBlurredImage(x, y, velocity) {
const blurAmount = 15;
//Math.min(velocity / 10, 5); // Adjust the blur intensity
const opacity = Math.min(0.5, velocity / 100); // Adjust the maximum opacity
const blurredImage = document.createElement("img");
blurredImage.src = "umbrella.png";
blurredImage.style.width = "100px";
blurredImage.style.height = "100px";
blurredImage.style.position = "absolute";
blurredImage.style.left = x - follower.offsetWidth / 2 + "px";
blurredImage.style.top = y - follower.offsetHeight / 2 + "px";
blurredImage.style.filter = `blur(${blurAmount}px)`;
blurredImage.style.opacity = opacity;
container.appendChild(blurredImage);
// Remove the blurred image after a short delay
setTimeout(() => {
container.removeChild(blurredImage);
}, 100); // Adjust the delay as needed
}
// END TEST
document.addEventListener("mousemove", function (event) {
const mouseX = event.clientX;
const mouseY = event.clientY;
if (!isMouseDown) {
// If the mouse is not down, update the position of the closed umbrella
follower.style.left = mouseX - follower.offsetWidth / 2 + "px";
follower.style.top = mouseY - follower.offsetHeight / 2 + "px";
const velocityX = prevX ? mouseX - prevX : 0;
const velocityY = prevY ? mouseY - prevY : 0;
const velocity = Math.sqrt(velocityX ** 2 + velocityY ** 2)
createBlurredImage(mouseX, mouseY, velocity);
prevX = mouseX;
prevY = mouseY;
// Apply motion blur using CSS filter
} else {
// If the mouse is down, update the position of the opened umbrella
follower.style.left = mouseX - follower.offsetWidth / 2 + "px";
follower.style.top = mouseY - follower.offsetHeight / 2 + "px";
}
});
// Important functions
container.addEventListener("mousedown", function () {
isMouseDown = true;
if (isMouseDown) {
follower.style.transition = "transform 0.2s ease";
follower.style.transform = "rotate(-45deg)";
// Use requestAnimationFrame to ensure the next rendering cycle
requestAnimationFrame(() => {
setTimeout(() => {
follower.style.transition = "transform 0s ease";
}, 200);
// Use another requestAnimationFrame to ensure the next rendering cycle
requestAnimationFrame(() => {
setTimeout(() => {
follower.src = openedUmbrellaImage.src;
opened = true;
follower.style.transform = "rotate(0deg)";
}, 200);
// Reset rotation
requestAnimationFrame(() => {
follower.style.transition = "transform 0.2s ease";
});
});
});
}
createRingEffect(event.clientX, event.clientY);
});
container.addEventListener("mouseup", function () {
isMouseDown = false;
// Rotate the opened umbrella smoothly before changing
if (!isMouseDown) { // Corrected condition
follower.src = "umbrella.png";
opened = false;
follower.style.transform = "rotate(0deg)"; // Reset rotation
}
});
function createRaindrop() {
const raindrop = document.createElement("div");
raindrop.classList.add("raindrop");
raindrop.style.left = Math.random() * window.innerWidth + "px";
const variable = Math.random() * 2 + 1;
raindrop.style.opacity = Math.random()+0.1;
raindrop.style.animationDuration = variable + "s";
raindrop.style.animationName = gust;
//raindrop.style.animationDuration = variable + "s";
raindrop.style.height = 1/variable * 50 + "px";
rainContainer.appendChild(raindrop);
raindrop.addEventListener("animationiteration", function () {
//raindrop.style.top = "-100px";
raindrop.remove;
});
}
requestAnimationFrame(checkCollision);
let isWKeyPressed = false;
let isSKeyPressed = false;
function createRingEffect(mouseX, mouseY) {
const ring = document.createElement("div");
ring.classList.add("ring");
const ringLeft = mouseX - ring.offsetWidth / 2;
const ringTop = mouseY - ring.offsetHeight / 2;
ring.style.left = ringLeft + "px";
ring.style.top = ringTop + "px";
ring.addEventListener("animationiteration", function () {
ring.classList.add("ring.active");
container.removeChild(ring);
});
// Append the ring element to the container
container.appendChild(ring);
// Remove the ring element after the animation completes
ring.addEventListener("animationend", function () {
container.removeChild(ring);
});
}
function windGust() {
let rightLeft = Math.round(Math.random()*2);
if(rightLeft == 0) {
gust = "gustFallRight";
}
if (rightLeft == 2) {
gust = "gustFallLeft";
}
if (rightLeft == 1) {
gust = "fall"
}
}
// Create initial raindrops
//WORKING
function createGlow(x,y) {
const blurAmount = 15;
const glowEffect = document.createElement("img");
glowEffect.src = "umbrellaGlow.png";
glowEffect.style.animationName = "fade";
glowEffect.style.animationDuration = 1 + "s";
glowEffect.style.width = "100px";
glowEffect.style.height = "100px";
glowEffect.style.position = "absolute";
glowEffect.style.left = y + "px";
glowEffect.style.top = x + "px";
glowEffect.style.filter = `blur(${blurAmount}px)`;
glowEffect.style.opacity = 1;
container.appendChild(glowEffect);
// Remove the blurred image after a short delay
setTimeout(() => {
container.removeChild(glowEffect);
}, 950); // Adjust the delay as needed
}
function checkCollision(raindrop) {
const raindropRect = raindrop.getBoundingClientRect();
const umbrellaRect = follower.getBoundingClientRect();
const cATop = umbrellaRect.top + 100;
const cABottom = umbrellaRect.top + 5;
// Check for collision with the top of the umbrella
const isCollision = (
(raindropRect.bottom < cATop && raindropRect.bottom > cABottom) &&
(raindropRect.left < umbrellaRect.right && raindropRect.right > umbrellaRect.left));
//follower.style.border = isCollision ? "2px solid blue" : "none";
if (isCollision) {
createGlow(umbrellaRect.top, umbrellaRect.left);
// Log the coordinates of the collision
/* console.log("Collision Coordinates: ", {
raindropTop: raindropRect.top,
raindropLeft: raindropRect.left,
});
*/
}
return isCollision;
}
function iterateAndDeleteRaindrops() {
const raindrops = document.querySelectorAll(".raindrop");
if (opened) {
raindrops.forEach((raindrop) => {
const raindropRect = raindrop.getBoundingClientRect();
const isVisible =
raindropRect.bottom >= 0 &&
raindropRect.top <= window.innerHeight &&
raindropRect.right >= 0 &&
raindropRect.left <= window.innerWidth;
if (isVisible && checkCollision(raindrop)) {
// Raindrop is above the umbrella, remove it
raindrop.remove();
}
});
}
// Use requestAnimationFrame for continuous iteration
requestAnimationFrame(iterateAndDeleteRaindrops);
}
function hailStorm() {
const hail = document.createElement("img");
hail.src = "hail.png";
hail.style.userSelect = "none";
const size = Math.round(Math.random() * 50) + 20;
const speedFactor = Math.sqrt(size); // Use square root for smoother relationship
const animationDuration = 0.1+speedFactor/2 + "s";
hail.style.left = Math.random() * window.innerWidth + "px";
hail.style.top = -500 - (Math.random()*100) + "px";
createWarningSnowflake(hail.style.top, hail.style.left, animationDuration);
hail.style.animationName = "hailFall";
hail.style.animationDuration = animationDuration;
hail.style.animationTimingFunction = "linear"; // Set linear timing function
hail.style.width = size + "px";
hail.style.height = size + "px";
hail.style.position = "absolute";
hail.style.opacity = 1;
hail.addEventListener("animationend", function () {
// Remove the snowflake when the animation ends
container.removeChild(hail);
});
container.appendChild(hail);
}
function createWarningSnowflake(height,x, duration) {
const snowflake = document.createElement("img");
snowflake.src = "snowflake.png";
snowflake.classList.add("snowflake");
snowflake.style.left = x;
snowflake.style.top = height+1000 + "px";
snowflake.style.animationName = "snowflakeFall";
snowflake.style.animationTimingFunction = "ease-out"; // Set linear timing function
snowflake.style.animationDuration = 2 + "s";
//snowflake.style.boxShadow = "0 0 20px 20px rgba(255,255,255,1)";
snowflake.style.width = "20px";
snowflake.style.height = "20px";
snowflake.style.position = "absolute";
snowflake.style.opacity = 1;
snowflake.addEventListener("animationend", function () {
// Remove the snowflake when the animation ends
container.removeChild(snowflake);
});
container.appendChild(snowflake);
}
// Generate continuous rain
requestAnimationFrame(iterateAndDeleteRaindrops);
setInterval(createRaindrop, 500);
setInterval(windGust, 100);
setInterval(hailStorm, 5000);
//setInterval(makeSnowBlur, 5);
});