-
Notifications
You must be signed in to change notification settings - Fork 0
/
nokeys.js
127 lines (103 loc) · 3.84 KB
/
nokeys.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var letters = ["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M"];
var letters_used;
var score;
$(document).ready(function(){
restart();
angleKeyboard();
$(document).keypress(function(e) {
if ((e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 97 && e.keyCode <= 122)) {
var letter = String.fromCharCode(e.which).toUpperCase();
pressedLetter(letter);
} else if (e.keyCode == 13) {
hitEnter($("#word").html());
} else if (e.keyCode == 32) {
restart();
}
});
$(document).on('click', '.key', function() {
var key = $(this).children().first().attr('id');
if (key == 'backspace') {
hitDelete($("#word").html());
} else if (key == 'enter') {
hitEnter($("#word").html());
} else {
pressedLetter(key);
}
});
$(document).keyup(function(e) {
if (e.keyCode == 8) {
hitDelete($("#word").html());
}
});
$(window).resize(function() {
angleKeyboard();
});
});
function angleKeyboard() {
// 1110 --> 20deg
// 425 --> 0deg
let keyboard = document.getElementById('keyboard');
let width = screen.width;
let degree = Math.min(20, width/45-10);
keyboard.style.transform = 'perspective(500px) rotateX('+ degree+ 'deg)';
}
function pressedLetter(letter) {
$("#" + letter).removeClass('pressed-backspace');
$("#" + letter).addClass('pressed-letter');
if (!letters_used.includes(letter)) $("#word").append(letter);
}
function hitEnter(word) {
let is_word = (words.includes(word))
$("#word").empty();
for (var i = 0; i <= word.length; i++) {
var letter = word.charAt(i);
if (is_word) {
if (!letters_used.includes(word.charAt(i)) && word.charAt(i) != '') {
letters_used.push(word.charAt(i));
}
$("#" + word.charAt(i)).removeClass('pressed-letter');
$("#" + word.charAt(i)).addClass('remove-letter');
} else {
$("#" + word.charAt(i)).removeClass('pressed-letter');
$("#" + word.charAt(i)).addClass('pressed-backspace');
}
}
score = 26 - letters_used.length;
if (score == 0) {
$(".points").html("nice");
} else {
$(".points").html(score + " letters");
}
}
function hitDelete(word) {
let letter = word.at(-1);
$("#word").html(word.slice(0, -1));
if (!$("#word").html().includes(letter)) {
$("#" + letter).removeClass('pressed-letter');
$("#" + letter).addClass('pressed-backspace');
}
}
function restart() {
let row = ".top";
$(row).empty();
for (let i = 0; i < 26; i++) {
if (i == 10 || i == 19) {
if (i == 10) row = ".middle";
else row = ".bottom";
$(row).empty();
}
$(row).append("<div class = 'key'><div id = " + letters[i] + " class = 'letter'> " + letters[i] + " </div></div>");
}
$(row).prepend("<div class = 'key'><div id = 'enter'>ENTER</div></div>");
$(row).append("<div class = 'key'><div id = 'backspace'><i class='material-icons'>backspace</i></div></div>");
// $('.keyboard-functions').empty();
// $('.keyboard-functions').append("<div class = 'key'><div id = 'enter'>ENTER</div></div>");
// $('.keyboard-functions').append("<div class = 'key'><div id = 'spacebar'>RESET</div></div>")
// $('.keyboard-functions').append("<div class = 'key'><div id = 'backspace'><i class='material-icons' style='font-size:1rem'>backspace</i></div></div>");
score = 26;
letters_used = [];
words = word_list.slice();
$(".points").remove();
$("#score").append("<div class = 'points'>" + score + " letters</div>")
$("#word").empty();
}