-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
255 lines (236 loc) · 8 KB
/
Copy pathindex.html
File metadata and controls
255 lines (236 loc) · 8 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Scales</title>
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<style>
body {
background-color: #333;
color: #ddd;
font-family: Helvetica, Arial, sans-serif;
font-size: 1.5em;
text-align: center;
}
ul {
list-style-type: none;
padding: 0;
margin: 1em 0 0;
}
li {
line-height: 1.5em;
padding: auto 0;
margin: auto 0;
}
button {
margin-top: 1em;
padding: .3em 1em;
border-radius: .5em;
box-sizing: border-box;
border-width: 0;
font-size: .75em;
color: #333;
}
button#shuffle {
font-size: 1em;
}
#timer {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
}
.green {
background-color: #009779;
}
.orange {
background-color: #fd950d;
}
#clear-button {
background-color: #D35943;
}
#display-timer {
font-size: .6em;
color: #aaa;
border-bottom: 1px solid #444;
margin: 0 auto .5em;
width: 300px;
}
.arrow {
font-size: .5em;
}
p.stopwatch {
margin: 0;
padding: 0;
}
#bottomLine {
border-bottom: 1px solid #444;
width: 300px;
margin: 0.5em auto 0;
}
.highlight {
color: yellow;
}
li.highlight:focus-visible {
outline: none;
}
/* yeah i hate it too */
.hide { display: none !important }
.show { display: block !important }
.show-flex { display: flex !important }
</style>
</head>
<body>
<div id="content">
<div id="display-timer">
<div id="show-timer" class="show">Show Timer <span class="arrow">▼</span></div>
<div id="hide-timer" class="hide">Hide Timer <span class="arrow">▲</span></div>
</div>
<div id="timer" class="hide">
<div id="time">
<div class="control-buttons-wrapper">
<p class="stopwatch">00:00:00</p>
<button id="main-button" class="control-buttons green">Start</button>
<button id="clear-button" class="control-buttons">Clear</button>
</div>
</div>
<div id="bottomLine"></div>
</div>
<ul id="keys"></ul>
<div id="controls">
<button id="shuffle" type="button">Shuffle</button>
</div>
</div>
</body>
<script>
const keys = [ 'C#/Db', 'C', 'Eb/D#', 'E', 'B/Cb', 'A', 'D', 'Ab/G#', 'Bb/A#', 'F', 'G', 'F#/Gb' ];
const reload = document.querySelector("#shuffle");
const shuffle = (array) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[ array[ i ], array[ j ] ] = [ array[ j ], array[ i ] ];
}
return array;
};
const doShuffle = (keys) => {
let it = 0;
for (const key of shuffle(keys)) {
const node = document.createElement("li");
node.role = "menuitem";
node.tabIndex = it === 0 ? "0" : "-1";
if (it === 0) node.classList.add('highlight');
const textnode = document.createTextNode(key);
node.appendChild(textnode);
document.getElementById("keys").appendChild(node);
it++;
}
}
window.addEventListener("load", (event) => {
doShuffle(keys);
// highlighting options
document.addEventListener('keydown', (e) => {
const items = document.querySelectorAll('#keys li');
let currentIndex = Array.from(items).findIndex(item => item.tabIndex === 0);
let nextIndex = currentIndex;
if (e.key === 'ArrowUp') {
nextIndex = Math.max(0, currentIndex - 1); // Prevent going above the first item
e.preventDefault(); // Prevent default browser scrolling
} else if (e.key === 'ArrowDown') {
nextIndex = Math.min(items.length - 1, currentIndex + 1); // Prevent going below the last item
e.preventDefault();
}
if (nextIndex !== currentIndex) {
// Remove highlight/focus from current item
items[ currentIndex ].classList.remove('highlight');
items[ currentIndex ].tabIndex = -1;
// Add highlight/focus to the next item
items[ nextIndex ].classList.add('highlight');
items[ nextIndex ].tabIndex = 0;
items[ nextIndex ].focus(); // Programmatically set focus
}
});
handleMouseClick();
});
reload.addEventListener("click", () => {
document.getElementById("keys").innerHTML = "";
doShuffle(keys);
handleMouseClick();
});
// show/hide timer
const showTimer = document.getElementById('show-timer');
const hideTimer = document.getElementById('hide-timer');
let timer = document.getElementById('timer')
showTimer.addEventListener('click', function () {
showTimer.classList.replace('show','hide');
hideTimer.classList.replace('hide','show');
timer.classList.replace('hide','show-flex');
});
hideTimer.addEventListener('click', function () {
showTimer.classList.replace('hide','show');
hideTimer.classList.replace('show','hide');
timer.classList.replace('show-flex','hide');
});
// timer https://github.com/chrislemus/stopwatch-using-javascript
const time = document.querySelector('.stopwatch')
const mainButton = document.querySelector('#main-button')
const clearButton = document.querySelector('#clear-button')
const stopwatch = {elapsedTime: 0}
mainButton.addEventListener('click', () => {
if (mainButton.innerHTML === 'Start') {
startStopwatch();
mainButton.innerHTML = 'Stop';
mainButton.classList.replace('green','orange');
} else {
stopwatch.elapsedTime += Date.now() - stopwatch.startTime
clearInterval(stopwatch.intervalId);
mainButton.innerHTML = 'Start';
mainButton.classList.replace('orange', 'green');
}
})
clearButton.addEventListener('click', () => {
stopwatch.elapsedTime = 0
stopwatch.startTime = Date.now()
displayTime(0, 0, 0, 0)
})
function startStopwatch() {
//reset start time
stopwatch.startTime = Date.now();
//run `setInterval()` and save id
stopwatch.intervalId = setInterval(() => {
//calculate elapsed time
const elapsedTime = Date.now() - stopwatch.startTime + stopwatch.elapsedTime
//calculate different time measurements based on elapsed time
const milliseconds = parseInt((elapsedTime % 1000) / 10)
const seconds = parseInt((elapsedTime / 1000) % 60)
const minutes = parseInt((elapsedTime / (1000 * 60)) % 60)
//display time
displayTime(minutes, seconds, milliseconds)
}, 100);
}
function displayTime(minutes, seconds, milliseconds) {
const leadZeroTime = [ minutes, seconds, milliseconds ].map(time => time < 10 ? `0${time}` : time)
time.innerHTML = leadZeroTime.join(':')
}
function handleMouseClick() {
// Add click handling for mouse users
document.querySelectorAll('#keys li').forEach((item, index) => {
item.addEventListener('click', () => {
// Remove previous highlights
document.querySelectorAll('#keys li').forEach(li => {
li.classList.remove('highlight');
li.tabIndex = -1;
});
// Add highlight to clicked item
item.classList.add('highlight');
item.tabIndex = 0;
item.focus();
});
});
}
</script>
</html>