-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
112 lines (91 loc) · 2.76 KB
/
index.html
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE html>
<head>
<link rel="stylesheet" href="./main.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Lato:wght@300&family=Mohave:wght@500&display=swap" rel="stylesheet">
</head>
<body>
<main>
<header>
<h1>
pictionary
</h1>
</header>
<section class="board">
<div class="timer">
XX
</div>
<div class="word-container">
<div class="word">
XXX
</div>
</div>
<button disabled class="newWord">
New word
</button>
</section>
<script>
const MAX_DELAY = 30;
const setElemInnerHTML = ({ selector, value}) => {
const elem = document.querySelector(selector);
if (!elem) {
return;
}
elem.innerHTML = value;
};
const setElemClass = ({ selector, value}) => {
const elem = document.querySelector(selector);
if (!elem) {
return;
}
elem.classList.add(value);
};
const setWord = (content) => {
const elem = document.querySelector(".word");
if(!elem) {
return;
}
let len = Math.min(content.length, 12);
len = Math.max(len, 1);
elem.style.setProperty('--word-scale', `${20.0/len}rem`);
elem.innerHTML = content;
}
const startWordPrepare = (words, setTimer) => {
const pickedWord = words?.[Math.floor(Math.random()*words.length)] || "XXX";
setWord(pickedWord);
setElemClass({ selector: ".timer", value: "blue" });
setTimer({ after: () => startWordCountdown(pickedWord, setTimer), delay: 3});
};
const startWordCountdown = (pickedWord, setTimer) => {
setWord("XXX");
setTimer({ after: () => wordReveal(pickedWord), delay: MAX_DELAY});
};
const wordReveal = (pickedWord) => {
setWord(pickedWord);
setElemInnerHTML({ selector: ".timer", value: "TIME OUT" });
};
const setTimerCurr = (timeoutId) => ({ after, delay})=> {
const timer = document.querySelector(".timer");
if (delay === 0) {
return after();
}
setElemInnerHTML({ selector: ".timer", value: `${delay}`});
const delayPerc = delay * 100.0 / MAX_DELAY;
timer.style.setProperty("--percent", `${delayPerc}%`);
clearTimeout(timeoutId.pop());
timeoutId[0] = setTimeout(() => setTimerCurr(timeoutId)({ after, delay: delay - 1}), 1000);
};
document.addEventListener("DOMContentLoaded", async () => {
let newWord = document.querySelector("button");
let timeoutId = [];
const response = await fetch('./words.json');
const { words } = await response.json();
newWord.addEventListener("click", () => {
startWordPrepare(words, setTimerCurr(timeoutId));
}, { passive: true });
newWord.removeAttribute("disabled");
});
</script>
</main>
</body>