-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRevealingText.js
More file actions
56 lines (49 loc) · 1.4 KB
/
RevealingText.js
File metadata and controls
56 lines (49 loc) · 1.4 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
class RevealingText {
constructor(config) {
// existing element that we'll pass in
this.element = config.element;
this.text = config.text;
this.speed = config.speed || 60;
//two bits of state
this.timeout = null;
//done revealing the message
this.isDone = false;
}
revealOneCharacter(list) {
//grab and remove first character
const next = list.splice(0, 1)[0];
next.span.classList.add("revealed");
if (list.length > 0) {
this.timeout = setTimeout(() => {
this.revealOneCharacter(list);
}, next.delayAfter);
} else {
this.isDone = true;
}
}
//changes all characters to visible
warpToDone() {
clearTimeout(this.timeout);
this.isDone = true;
this.element.querySelectorAll("span").forEach((s) => {
s.classList.add("revealed");
});
}
init() {
// splitting the characters into separate elements
let characters = [];
this.text.split("").forEach((character) => {
//Create each span, add to element in DOM
let span = document.createElement("span");
span.textContent = character;
this.element.appendChild(span);
//Add this span to our internal state Array
characters.push({
span,
//if character is equal to a space don't delay
delayAfter: character === " " ? 0 : this.speed,
});
});
this.revealOneCharacter(characters);
}
}