forked from DishpitDev/Slopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemoji-background.js
112 lines (95 loc) Β· 3.14 KB
/
emoji-background.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
class EmojiBackground {
constructor() {
this.emojis = [];
this.emojiElements = [];
this.container = document.createElement("div");
this.container.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
pointer-events: none;
z-index: -1;
overflow: hidden;
`;
document.body.appendChild(this.container);
this.initialize();
}
async initialize() {
await this.loadEmojis();
this.createEmojis();
this.animate();
}
async loadEmojis() {
try {
const response = await fetch("all-emojis.txt");
const text = await response.text();
// Split by newlines and filter out empty lines
this.emojis = text.split("\n").filter((emoji) => emoji.trim());
if (this.emojis.length === 0) {
throw new Error("No emojis loaded");
}
} catch (error) {
console.error("Error loading emojis:", error);
// Fallback emojis in case the file can't be loaded
this.emojis = ["π", "π", "π", "π", "π", "π
", "π", "π€£", "π₯²", "π₯Ή", "βΊοΈ"];
}
}
createEmoji() {
const emoji = document.createElement("div");
const randomEmoji = this.emojis[Math.floor(Math.random() * this.emojis.length)];
emoji.textContent = randomEmoji;
emoji.style.cssText = `
position: absolute;
font-size: ${20 + Math.random() * 30}px;
opacity: ${0.3 + Math.random() * 0.7};
transform: rotate(${Math.random() * 360}deg);
user-select: none;
`;
// Start position (right side of screen, random height)
emoji.style.left = "100%";
emoji.style.top = `${Math.random() * 100}%`;
// Animation properties
emoji.speedX = 1 + Math.random() * 2;
emoji.speedY = (Math.random() - 0.5) * 0.5;
emoji.rotation = (Math.random() - 0.5) * 2;
this.container.appendChild(emoji);
this.emojiElements.push(emoji);
return emoji;
}
createEmojis() {
// Create initial set of emojis
for (let i = 0; i < 20; i++) {
const emoji = this.createEmoji();
// Distribute initial positions across the screen
emoji.style.left = `${Math.random() * 100}%`;
}
}
animate() {
const updateEmoji = (emoji) => {
const rect = emoji.getBoundingClientRect();
const left = parseFloat(emoji.style.left);
const top = parseFloat(emoji.style.top);
const rotation = parseFloat(emoji.style.transform.replace(/[^0-9-]+/g, "")) || 0;
// Update position
emoji.style.left = `${left - emoji.speedX}%`;
emoji.style.top = `${top + emoji.speedY}%`;
emoji.style.transform = `rotate(${rotation + emoji.rotation}deg)`;
// If emoji is off screen, reset it
if (rect.right < 0) {
emoji.style.left = "100%";
emoji.style.top = `${Math.random() * 100}%`;
}
};
const animate = () => {
this.emojiElements.forEach(updateEmoji);
requestAnimationFrame(animate);
};
animate();
}
}
// Initialize the emoji background
window.addEventListener("DOMContentLoaded", () => {
new EmojiBackground();
});