-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwobblyText.js
69 lines (50 loc) · 1.63 KB
/
wobblyText.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
let speed = 70;
let height = 0.3;
let href = "";
let wobblyCharsArray = []
wobblyTexts = document.getElementsByClassName('wobble')
for (let textI = 0; textI < wobblyTexts.length; textI++) {
let wobbleElement = wobblyTexts[textI];
let wobblyString = wobbleElement.textContent;
while (wobbleElement.firstChild) {
wobbleElement.removeChild(wobbleElement.firstChild);
}
let consecutiveSpaces = 1;
for (let i = 0; i < wobblyString.length; i++) {
let atext = wobblyString.charAt(i);
if (wobblyString.charAt(i) == ' ') {
consecutiveSpaces++;
wobblyCharsArray[i] = null;
continue
}
else {
atext = ' '.repeat(consecutiveSpaces) + atext;
consecutiveSpaces = 0;
}
let wobblyCharacter = document.createElement("span");
wobblyCharacter.style.position = "relative";
wobblyCharacter.textContent = atext;
if (href) {
wobblyCharacter.style.cursor = "pointer";
wobblyCharacter.onclick = function () {
top.location.href = href;
};
}
wobblyCharsArray[i] = wobblyCharacter;
wobbleElement.appendChild(wobblyCharacter);
}
}
requestAnimationFrame(animateWobble);
let elapsed = 0;
let lastTimestamp = 0;
function animateWobble(timestamp) {
elapsed += (timestamp - lastTimestamp) * speed * 0.0002;
// Animate each character's vertical position based on a sine wave
for (let i = 0; i < wobblyCharsArray.length; i++) {
let wobblyCharacter = wobblyCharsArray[i];
if (!wobblyCharacter) continue;
wobblyCharacter.style.top = height * Math.sin(i + elapsed) + "vw";
}
lastTimestamp = timestamp;
requestAnimationFrame(animateWobble);
}