-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.js
More file actions
140 lines (119 loc) · 4.75 KB
/
Engine.js
File metadata and controls
140 lines (119 loc) · 4.75 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
// ===== CONFETTI ===== //
function launchConfettiOverlay(options = {}) {
const {
quantity = 100,
emoji = false,
emojis = ["🎉", "✨", "🎈", "💫"],
colorRange = [0, 360],
durationRange = [2000, 4000],
rainbowMode = false, // Rainbow Mode (true/false)
screenFlash = false, // Flash Effect (true/false)
blinkConfetti = false, // Shining Confetti (true/false)
swingEffect = false, // Confettin Swing Effect (true/false)
} = options;
const overlay = document.getElementById('confetti-overlay');
overlay.style.display = 'block';
if (screenFlash) {
overlay.style.backgroundColor = 'white';
setTimeout(() => {
overlay.style.backgroundColor = 'transparent';
}, 200);
}
for (let i = 0; i < quantity; i++) {
const confetti = emoji ? document.createElement('span') : document.createElement('div');
const hue = Math.floor(Math.random() * (colorRange[1] - colorRange[0]) + colorRange[0]);
const left = `${Math.random() * 100}vw`;
const top = `-${Math.random() * 100}px`;
const angle = Math.random() * 360;
const rotate = `${Math.random() * 360}deg`;
const delay = Math.random() * 1000;
const fallDuration = Math.random() * (durationRange[1] - durationRange[0]) + durationRange[0];
if (emoji) {
confetti.textContent = emojis[Math.floor(Math.random() * emojis.length)];
confetti.style.fontSize = `${Math.random() * 20 + 10}px`;
confetti.style.display = 'inline-block';
} else {
confetti.classList.add('confetti');
confetti.style.setProperty('--hue', hue);
}
confetti.classList.add('confetti-emoji');
confetti.style.position = 'absolute';
confetti.style.left = left;
confetti.style.top = top;
confetti.style.transform = `rotate(${rotate})`;
confetti.style.opacity = 1;
confetti.style.pointerEvents = 'none';
confetti.style.transition = `opacity 1s ease-out`;
// Rainbow Mode
if (rainbowMode) {
confetti.style.animation = `fall-rainbow ${fallDuration / 1000}s ease-out ${delay / 1000}s forwards`;
} else {
confetti.style.animation = `fall ${fallDuration / 1000}s ease-out ${delay / 1000}s forwards`;
}
// Shinning Effect (blink)
if (blinkConfetti) {
confetti.style.animation += `, blink 1s linear infinite`;
}
// Swing Confetti Effect (Swing)
if (swingEffect) {
confetti.style.animation += `, fall-swing ${fallDuration / 1000}s ease-out ${delay / 1000}s forwards`;
}
overlay.appendChild(confetti);
setTimeout(() => {
confetti.style.opacity = 0;
setTimeout(() => confetti.remove(), 1000);
}, fallDuration + delay);
}
setTimeout(() => {
overlay.style.display = 'none';
}, durationRange[1] + 2000);
}
// ===== SHAKE ===== //
function launchShake({
element = 'body', // element to shake, it can be a ".element" or even a "#element" too
duration = 500, // Controls the duration of the shake animation
strength = 'normal', // Options: "weakest", "weak", "normal", "strong" and "very strong"
flash = false // Enables or desables the flash effect
} = {}) {
const el = document.querySelector(element);
if (!el) return;
// Optional flash
if (flash) {
const flashEl = document.createElement('div');
flashEl.style.position = 'fixed';
flashEl.style.top = 0;
flashEl.style.left = 0;
flashEl.style.width = '100vw';
flashEl.style.height = '100vh';
flashEl.style.backgroundColor = 'white';
flashEl.style.opacity = '0.8';
flashEl.style.zIndex = '9999';
flashEl.style.pointerEvents = 'none';
flashEl.style.transition = 'opacity 0.4s ease-out';
document.body.appendChild(flashEl);
setTimeout(() => {
flashEl.style.opacity = '0';
setTimeout(() => flashEl.remove(), 400);
}, 50);
}
const strengthMap = {
'weakest': { class: 'shake-xs', time: 300 },
'weak': { class: 'shake-sm', time: 400 },
'normal': { class: 'shake-md', time: 500 },
'strong': { class: 'shake-lg', time: 600 },
'very strong': { class: 'shake-xl', time: 800 }
};
const { class: shakeClass, time: animTime } = strengthMap[strength] || strengthMap['normal'];
let interval;
const repeatShake = () => {
el.classList.remove(shakeClass); // reset first in case it's still active
void el.offsetWidth; // force reflow
el.classList.add(shakeClass);
};
repeatShake(); // first run
interval = setInterval(repeatShake, animTime);
setTimeout(() => {
clearInterval(interval);
el.classList.remove(shakeClass);
}, duration);
}