added typewriter effect

This commit is contained in:
Felix Baumgärtner 2024-08-06 17:55:14 +02:00
parent b7e037eb6a
commit 686608ba9f
5 changed files with 70 additions and 5 deletions

View File

@ -76,7 +76,7 @@ class Overworld {
{ who: "hero", type: "walk", direction: "down" }, { who: "hero", type: "walk", direction: "down" },
{ who: "npc1", type: "walk", direction: "right" }, { who: "npc1", type: "walk", direction: "right" },
{ who: "hero", type: "stand", direction: "left", time: 200 }, { who: "hero", type: "stand", direction: "left", time: 200 },
{ type: "textMessage", text: "Hi!" }, { type: "textMessage", text: "Hi! Lorem ipsum" },
]) ])
} }
} }

47
RevealingText.js Normal file
View File

@ -0,0 +1,47 @@
class RevealingText {
constructor(config) {
this.element = config.element;
this.text = config.text;
this.speed = config.speed || 60;
this.timeout = null;
this.isDone = false;
}
revealOneCharacter(list) {
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;
}
}
warpToDone() {
clearTimeout(this.timeout);
this.isDone = true;
this.element.querySelectorAll("span").forEach(s => {
s.classList.add("revealed");
})
}
init() {
let characters = [];
this.text.split("").forEach(character => {
let span = document.createElement("span");
span.textContent = character;
this.element.appendChild(span);
characters.push({
span,
delayAfter: character === " " ? 0 : this.speed
})
});
this.revealOneCharacter(characters);
}
}

View File

@ -10,27 +10,38 @@ class TextMessage {
this.element.classList.add("TextMessage"); this.element.classList.add("TextMessage");
this.element.innerHTML = (` this.element.innerHTML = (`
<p class="TextMessage_p">${this.text}</p> <p class="TextMessage_p"></p>
<button class="TextMessage_button">Next</button> <button class="TextMessage_button">Next</button>
`); `);
// typewriter effect
this.revealingText = new RevealingText({
element: this.element.querySelector(".TextMessage_p"),
text: this.text
})
this.element.querySelector("button").addEventListener("click", () => { this.element.querySelector("button").addEventListener("click", () => {
this.done(); this.done();
}); });
this.actionListener = new KeyPressListener("Enter", () => { this.actionListener = new KeyPressListener("Enter", () => {
this.actionListener.unbind();
this.done(); this.done();
}) })
} }
done() { done() {
if (this.revealingText.isDone) {
this.element.remove(); this.element.remove();
this.actionListener.unbind();
this.onComplete(); this.onComplete();
} else {
this.revealingText.warpToDone();
}
} }
init(container) { init(container) {
this.createElement(); this.createElement();
container.appendChild(this.element); container.appendChild(this.element);
this.revealingText.init();
} }
} }

View File

@ -22,6 +22,7 @@
<script src="/OverworldEvent.js"></script> <script src="/OverworldEvent.js"></script>
<script src="/TextMessage.js"></script> <script src="/TextMessage.js"></script>
<script src="/KeyPressListener.js"></script> <script src="/KeyPressListener.js"></script>
<script src="/RevealingText.js"></script>
<script src="/init.js"></script> <script src="/init.js"></script>
</body> </body>
</html> </html>

View File

@ -10,6 +10,12 @@
border-top: 1px solid var(--menu-border-color); border-top: 1px solid var(--menu-border-color);
color: var(--menu-font-color); color: var(--menu-font-color);
} }
.TextMessage span {
opacity: 0;
}
.TextMessage span.revealed {
opacity: 1;
}
.TextMessage_p { .TextMessage_p {
margin: 0; margin: 0;