-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRevealingText.js
More file actions
50 lines (42 loc) · 1.77 KB
/
RevealingText.js
File metadata and controls
50 lines (42 loc) · 1.77 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
class RevealingText {
constructor(config) {
this.element = config.element; // this is the div - the container we want to write/type INTO - we fill with <span> text
this.text = config.text;
this.speed = config.speed || 70;
this.timeout = null;
this.isDone = false;
}
revealOneCharacter(list) {
const next = list.splice(0,1)[0] // bc list is an array of {}s, the first element is the span content
// by js syntax, its splice(start, deletecount, item (to add?)) - so you delete the first item, but you save it into "next"
next.span.classList.add("revealed");
if (list.length > 0) {
this.timeout = setTimeout(() => {
this.revealOneCharacter(list)
}, next.delayAfter)
}
else {
this.isDone = true;
}
}
warpToDone() { // if space is pressed in the middle of the text message, then we want to finish showing the message
clearTimeout(this.timeout);
this.isDone = true;
this.element.querySelectorAll("span").forEach(s => {
s.classList.add("revealed");
})
}
init() {
let characters = [];
this.text.split("").forEach(character => { // will split the text into individual characters
let span = document.createElement("span");
span.textContent = character;
this.element.appendChild(span); // adds the newly made span character (individual characters) to the text box
characters.push({
span,
delayAfter: character === " " ? 0 : this.speed, // will be no delay if there is a " " character
})
})
this.revealOneCharacter(characters);
}
}