-
Notifications
You must be signed in to change notification settings - Fork 0
/
particles.js
175 lines (158 loc) · 3.47 KB
/
particles.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
particlesJS('particles-js', {
particles: {
number: {
value: 150,
density: {
enable: true,
value_area: 800
}
},
color: {
value: '#fff'
},
shape: {
type: 'circle',
stroke: {
width: 0,
color: '#fff'
},
polygon: {
nb_sides: 5
},
image: {
src: 'https://cdn.freebiesupply.com/logos/large/2x/slack-logo-icon.png',
width: 100,
height: 100
}
},
opacity: {
value: 0.2,
random: false,
anim: {
enable: false,
speed: 1,
opacity_min: 0.1,
sync: false
}
},
size: {
value: 4,
random: true,
anim: {
enable: false,
speed: 10,
size_min: 10,
sync: false
}
},
line_linked: {
enable: false,
distance: 150,
color: '#808080',
opacity: 0.4,
width: 1
},
move: {
enable: true,
speed: 5,
direction: 'none',
random: false,
straight: false,
out_mode: 'out',
bounce: false,
attract: {
enable: false,
rotateX: 600,
rotateY: 1200
}
}
},
interactivity: {
detect_on: 'window',
events: {
onhover: {
enable: false,
mode: 'repulse'
},
onclick: {
enable: false,
mode: 'push'
}
},
modes: {
'repulse' : {
distance: 70,
duration: 0.4
},
'push' : {
particles_nb: 4
}
}
},
retina_detect: true
});
const allElements = document.querySelectorAll('.animated-text');
// It checks if there is at least one element
if (allElements.length > 0) {
//It runs the script for each found element
allElements.forEach((element) => {
const txtElement = element,
wordsList = txtElement.getAttribute('data-words'),
words = wordsList.split(', '); // It makes an array of words from data attribute
let wordsCount = 0;
entry();
// Initial function
function entry() {
if (wordsCount < words.length) {
// It runs the code for each word
let word = words[wordsCount],
txtArr = word.split(''), // It makes an array of letters in the word
count = 0;
txtElement.textContent = ''; // It removes the previous text from the element
// For each letter in the array
txtArr.forEach((letter) => {
// It replaces the empty space for the "non-break-space" HTML...
// ... This is needed to separate the words properly
let _letter = letter === ' ' ? ' ' : letter;
// It wraps every letter with a "span" and puts all they back to the element
txtElement.innerHTML += `<span>${_letter}</span>`;
});
let spans = txtElement.childNodes;
// It sets the interval between each letter showing
const letterInterval = setInterval(activeLetter, 70);
function activeLetter() {
spans[count].classList.add('active');
count++;
if (count === spans.length) {
clearInterval(letterInterval);
// It waits 4 seconds to start erasing the word
setTimeout(() => {
eraseText();
}, 600);
}
}
function eraseText() {
// It sets the interval between each letter hiding
let removeInterval = setInterval(removeLetter, 40);
count--;
function removeLetter() {
spans[count].classList.remove('active');
count--;
if (count === -1) {
clearInterval(removeInterval);
wordsCount++;
// After removing the last letter, call the initial function again
entry();
}
}
}
} else {
// If the code reaches the last word
// It resets the words counter...
wordsCount = 0;
// ...and calls the initial function again.
entry();
}
}
});
}