-
Notifications
You must be signed in to change notification settings - Fork 0
/
font.ts
110 lines (104 loc) · 3.5 KB
/
font.ts
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
let container: HTMLDivElement = document.createElement("div");
container.setAttribute("class", "container");
let fontToGuess: number = Math.floor(Math.random() * 5);
let fonts: string[] = [
"Arial",
"Verdana",
"Georgia",
"Times New Roman",
"Courier New",
"Tahoma",
"Helvetica",
"Courier",
"Arial Black",
"Comic Sans MS",
"Trebuched MS",
"Garamond",
"Courier New",
"Brush Script MT",
"Luminari",
"Bradley Hand",
"Monaco",
"Lucida",
"Baskerville",
"Palatino",
"Gill Sans",
"Impact",
"Calibri",
"Cambria",
"Candara",
"Century Gothic",
"Consolas",
];
let guessingFonts: string[] = generateRandomFonts();
let gameWin: boolean = false;
let element: HTMLParagraphElement | HTMLButtonElement = document.createElement("p");
element.setAttribute("id", "guessingFont");
element.innerHTML = guessingFonts[fontToGuess];
container.appendChild(element);
let fontDiv: HTMLDivElement = document.createElement("div");
fontDiv.setAttribute("class", "fontContainer");
for (let i = 0; i < 5; i++) {
let text: HTMLParagraphElement = document.createElement("p");
text.setAttribute("class", "font");
text.innerHTML = "Sample";
text.style.fontFamily = guessingFonts[i];
text.addEventListener("click", () => {
handle(text);
});
fontDiv.appendChild(text);
}
container.appendChild(fontDiv);
element = document.createElement("p");
element.setAttribute("id", "text");
element.innerHTML = "Guess the correct font";
container.appendChild(element);
document.body.appendChild(container);
function handle(selected: HTMLParagraphElement): void {
let selectedFont: string = selected.style.fontFamily;
if (selectedFont == guessingFonts[fontToGuess] || selectedFont.substring(1, selectedFont.length - 1) == guessingFonts[fontToGuess]) {
if (!gameWin) {
for (let i: number = 0; i < fontDiv.children.length; i++) {
let child: any = fontDiv.children[i];
if (child !== selected) {
child.style.color = "white";
}
}
element = document.createElement("p");
element.setAttribute("id", "win");
element.innerHTML = "You're correct!";
container.replaceChild(element, container.childNodes[2]);
let a: HTMLAnchorElement = document.createElement("a");
a.setAttribute("href", "https://timhaj.github.io/WhatTheStyle/font.html");
element = document.createElement("button");
element.setAttribute("id", "btn");
element.innerHTML = "NEW GAME";
a.appendChild(element);
container.appendChild(a);
gameWin = true;
}
}
else {
if (!gameWin && selected.style.color != "white") {
selected.style.color = "white";
element = document.createElement("p");
element.setAttribute("id", "incorrect");
element.innerHTML = "Incorrect, that font was: <span style=\"font-family:" + selectedFont + "\">" + selectedFont + "</span>";
container.replaceChild(element, container.childNodes[2]);
}
}
}
function generateRandomFonts(): string[] {
let arr: string[] = [];
for (let i: number = 0; arr.length < 5;) {
let selected: string = fonts[Math.floor(Math.random() * fonts.length)];
if (arr.indexOf(selected) >= 0) {
continue;
}
else {
arr[i] = selected;
i++;
}
}
return arr;
}