Skip to content

Commit 35c7acd

Browse files
committed
add finished speech text reader
1 parent 75bd797 commit 35c7acd

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

71-speech text reader/script.js

+50
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,57 @@ function createBox(item) {
6464
<img src='https://github.com/bradtraversy/vanillawebprojects/blob/master/speech-text-reader/img/${image}.jpg?raw=true' alt="${text}"/>
6565
<p class="info">${text}</p>
6666
`;
67+
box.addEventListener("click", () => handleSpeech(text, box));
6768
main.appendChild(box);
6869
}
6970

7071
data.forEach(createBox);
72+
73+
let voices = [];
74+
75+
function getVoices() {
76+
voices = speechSynthesis.getVoices();
77+
voices.forEach((voice) => {
78+
const option = document.createElement("option");
79+
option.value = voice.name;
80+
option.innerText = `${voice.name} ${voice.lang}`;
81+
voicesSelect.appendChild(option);
82+
});
83+
}
84+
85+
function handleSpeech(text, box) {
86+
setTextMessage(text);
87+
speakText();
88+
box.classList.add("active");
89+
setTimeout(() => box.classList.remove("active"), 800);
90+
}
91+
92+
const message = new SpeechSynthesisUtterance();
93+
94+
function setTextMessage(text) {
95+
message.text = text;
96+
}
97+
98+
function speakText() {
99+
speechSynthesis.speak(message);
100+
}
101+
102+
function setVoice(e) {
103+
message.voice = voices.find((voice) => voice.name === e.target.value);
104+
}
105+
106+
// Event Listeners
107+
toggleButton.addEventListener("click", () => {
108+
document.getElementById("text-box").classList.toggle("show");
109+
});
110+
closeButton.addEventListener("click", () => {
111+
document.getElementById("text-box").classList.remove("show");
112+
});
113+
speechSynthesis.addEventListener("voiceschanged", getVoices);
114+
voicesSelect.addEventListener("change", setVoice);
115+
readButton.addEventListener("click", () => {
116+
setTextMessage(textarea.value);
117+
speakText();
118+
});
119+
120+
getVoices();

71-speech text reader/style.css

+13-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ body {
2222

2323
h1 {
2424
text-align: center;
25+
color: var(--secondary-color);
2526
}
2627

2728
.container {
@@ -37,10 +38,14 @@ h1 {
3738
font-size: 1rem;
3839
font-family: inherit;
3940
font-weight: bold;
40-
padding: 0.5rem 1rem;
41+
padding: 0.75rem 2rem;
4142
cursor: pointer;
4243
}
4344

45+
.btn:hover {
46+
opacity: 0.9;
47+
}
48+
4449
.btn:active {
4550
transform: scale(0.98);
4651
}
@@ -69,21 +74,25 @@ h1 {
6974
transition: all 1s ease-in-out;
7075
}
7176

77+
.text-box.show {
78+
transform: translate(-50%, 0);
79+
}
80+
7281
.text-box select {
7382
background-color: var(--main-color);
7483
color: var(--light-color);
7584
border-radius: var(--border-radius);
7685
border: 0;
7786
font-size: 0.75rem;
78-
height: 30px;
87+
padding: 0.75rem 1rem;
7988
width: 100%;
8089
}
8190

8291
.text-box textarea {
83-
border: 1px #dadada solid;
92+
border: 1px var(--border-color) solid;
8493
border-radius: var(--border-radius);
8594
font-size: 1rem;
86-
padding: 0.5rem;
95+
padding: 0.75rem 1rem;
8796
margin: 1rem 0;
8897
height: 150px;
8998
width: 100%;

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
| 68 | [Music Player](https://github.com/solygambas/html-css-fifty-projects/tree/master/68-music%20player) | [Live Demo](https://codepen.io/solygambas/full/LYbaZNG) |
7777
| 69 | [Infinite Scroll Posts](https://github.com/solygambas/html-css-fifty-projects/tree/master/69-infinite%20scroll%20posts) | [Live Demo](https://codepen.io/solygambas/full/qBqvyEB) |
7878
| 70 | [Typing Game](https://github.com/solygambas/html-css-fifty-projects/tree/master/70-typing%20game) | [Live Demo](https://codepen.io/solygambas/full/wvoOQvq) |
79-
| 71 | [Speech Text Reader](https://github.com/solygambas/html-css-fifty-projects/tree/master/71-speech%20text%20reader) | [Live Demo](#) |
79+
| 71 | [Speech Text Reader](https://github.com/solygambas/html-css-fifty-projects/tree/master/71-speech%20text%20reader) | [Live Demo](https://codepen.io/solygambas/full/QWGPLVM) |
8080

8181
This repository is mostly based on 2 courses by Brad Traversy (2020):
8282

0 commit comments

Comments
 (0)