-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamtimer.html
357 lines (319 loc) · 14.9 KB
/
examtimer.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Exam timer</title>
<script>
// Usage: See README.md or https://github.com/doersino/exam-timer/.
var duration = 90; // in minutes
var remainingLabel = "m";
var remainingAboveContent = "";
var remainingBelowContent = "REMAINING";
var startContent = "START";
var endContent = "END";
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html, body {
background-color: black;
font-family: Helvetica, Arial, sans-serif;
font-size: 28px;
color: white;
}
body {
display: flex;
}
.infos {
flex: 0.6;
padding: 1.5rem;
height: 100vh;
overflow-y: scroll;
background-color: #333;
}
header {
margin-bottom: 0.666em;
}
h1 {
font-size: 1.5em;
}
h2 {
font-size: 1em;
}
ul {
list-style: none;
}
li:before {
content: "–";
padding-right: 0.5em;
}
li {
padding-left: 1em;
text-indent: -1em;
line-height: 1.5em;
margin-bottom: 0.5em;
}
.timer {
flex: 0.4;
height: 100vh;
padding: 1.5rem;
}
svg {
width: 100%;
height: 100%;
font-family: Helvetica, Arial, sans-serif;
letter-spacing: 1px;
}
</style>
</head>
<body>
<section class="infos">
<span class="before">Welcome to the</span>
<header>
<h1>Studiology 101 exam</h1>
<h2>for Subheading Science students</h2>
</header>
<ul>
<li class="before"><strong>On your desks</strong>: your student id, a pen, snacks/beverages/liquor.</li>
<li class="before">Put your phones, smartwatches and tricorders away.</li>
<li class="before during homestretch">Unsuccessful cheating attempts will result in a failing grade and gossip in the faculty lounge. Successful cheating is rewarded with good grades.</li>
<li class="before">Good luck. <strong>You'll need it.</strong></li>
<li class="during">If you <strong>require more sheets of paper</strong>, raise your hand. Our paper butler ("teaching assistant" is so old-fashioned!) will at once present you with a selection of the finest paper and paper byproducts.</li>
<li class="homestretch">Whoever noisily packs their belongings, lazily shuffles to the front to hand in their exam, and sprints out of the room during the final 10 minutes will be cursed by the remaining unfortunates.</li>
<li class="after">It's gone. It's done.</li>
<li class="after">Yes, Mister Frodo. It's over now.</li>
<li class="after">I can see the Shire... the Brandywine River... Bag End... Gandalf's fireworks... the lights in the Party Tree.</li>
</ul>
</section>
<section class="timer">
<svg id="timer">
<path id="spinner-background" fill="none" stroke="#555" />
<path id="spinner" fill="none" stroke="white" />
<text id="remaining" fill="white" text-anchor="middle" alignment-baseline="middle"></text>
<text id="remaining-above" fill="white" text-anchor="middle" alignment-baseline="middle"></text>
<text id="remaining-below" fill="white" text-anchor="middle" alignment-baseline="middle"></text>
<path id="start-end-circle" fill="none" stroke="none" />
<text id="start-end" fill="white" text-anchor="middle">
<textPath id="start-end-path" xlink:href="#start-end-circle"></textPath>
</text>
</svg>
</section>
<script>
// via https://stackoverflow.com/a/18473154
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
let angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;
return {
x: centerX + (radius * Math.cos(angleInRadians)),
y: centerY + (radius * Math.sin(angleInRadians))
};
}
// via https://stackoverflow.com/a/18473154
function describeArc(x, y, radius, startAngle, endAngle) {
let start = polarToCartesian(x, y, radius, endAngle);
let end = polarToCartesian(x, y, radius, startAngle);
let largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
let d = [
"M", start.x, start.y,
"A", radius, radius, 0, largeArcFlag, 0, end.x, end.y
].join(" ");
return d;
}
// via https://github.com/doersino/markdeep-slides/blob/master/markdeep-slides/markdeep-slides.js
function toggleFullscreen() {
var fullscreen = !!(window.fullScreen || document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement);
if (fullscreen) {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
}
} else {
var root = document.documentElement;
if (root.requestFullscreen) {
root.requestFullscreen();
} else if (root.mozRequestFullScreen) {
root.mozRequestFullScreen();
} else if (root.webkitRequestFullscreen) {
root.webkitRequestFullscreen();
} else if (root.msRequestFullscreen) {
root.msRequestFullscreen();
}
}
}
var started = false;
var s;
var e;
var paused = false;
var p;
var pPrev = 0;
var pCurr = 0;
var pTotal = 0;
var eWithPauses;
// resume timer after a reload
if (window.location.hash) {
let loc = window.location.hash.substring(1);
let [rawS, rawPPrev] = loc.split("+");
s = new Date(parseInt(rawS) * 1000);
pPrev = parseInt(rawPPrev * 1000);
e = new Date(s.getTime() + duration*60000);
started = true;
}
// handle key presses
document.body.onkeyup = function(event) {
// space bar => start timer or pause/unpause
if (event.keyCode == 32) {
if (!started) {
s = new Date();
e = new Date(s.getTime() + duration*60000);
started = true;
window.location.hash = Math.round(s.getTime() / 1000) + "+" + pTotal;
} else {
if (!paused) {
p = new Date();
} else {
pPrev += pCurr;
pCurr = 0;
}
paused = !paused;
}
}
// f key => toggle fullscreen
if (event.keyCode == 70) {
toggleFullscreen();
}
};
// hide cursor once it hasn't been moved for 2s
// based on https://stackoverflow.com/a/31798987
var cursorTimeout;
document.body.onmousemove = function() {
if (document.body.style.cursor != "none") {
if (cursorTimeout) {
clearTimeout(cursorTimeout);
}
} else {
document.body.style.cursor = "auto";
}
cursorTimeout = setTimeout(function() {
document.body.style.cursor = "none";
}, 2000);
};
// run the following 10 times a second. yes, much of this could be run
// only once, but none of it is massively resource-intensive and keeping
// it this way automatically adjusts the size of everything in case the
// browser window is resized or the scaling factor is changed while the
// timer is started
setInterval(function() {
let w = document.getElementById("timer").clientWidth;
let h = document.getElementById("timer").clientHeight;
let size = Math.min(h, w);
// compute line thicknesses and font sizes
let spinnerArcThickness = size / 25;
let spinnerBackgroundArcThickness = size / 60;
let remainingFontSize = size / 5;
let remainingAboveFontSize = size / 15;
let remainingBelowFontSize = size / 15;
let startEndFontSize = size / 25;
// define spinner/arc position
let spinnerRadius = size / 2 - spinnerArcThickness / 2;
let spinnerX = w / 2;
let spinnerY = h / 2;
// define position of labels
let remainingY = spinnerY;
let remainingAboveY = remainingY - .7 * (remainingFontSize + remainingAboveFontSize);
let remainingBelowY = remainingY + .6 * (remainingFontSize + remainingBelowFontSize);
// weird: the spacing's off if this ^ is the same as the other one
// shift labels if only one is set
if (remainingAboveContent && !remainingBelowContent) {
remainingY += .25 * (remainingFontSize + remainingAboveFontSize);
remainingAboveY += .25 * (remainingFontSize + remainingAboveFontSize);
}
if (!remainingAboveContent && remainingBelowContent) {
remainingY -= .25 * (remainingFontSize + remainingAboveFontSize);
remainingBelowY -= .25 * (remainingFontSize + remainingAboveFontSize);
};
// compute radius of arc on which start and end times will be placed
let startEndCircleRadius = spinnerRadius - startEndFontSize;
// apply computed values
document.getElementById("spinner").setAttribute("stroke-width", spinnerArcThickness);
document.getElementById("spinner-background").setAttribute("stroke-width", spinnerBackgroundArcThickness);
document.getElementById("remaining").setAttribute("font-size", remainingFontSize);
document.getElementById("remaining").setAttribute("x", spinnerX);
document.getElementById("remaining").setAttribute("y", remainingY);
document.getElementById("remaining-above").setAttribute("font-size", remainingAboveFontSize);
document.getElementById("remaining-above").innerHTML = remainingAboveContent;
document.getElementById("remaining-above").setAttribute("x", spinnerX);
document.getElementById("remaining-above").setAttribute("y", remainingAboveY);
document.getElementById("remaining-below").setAttribute("font-size", remainingBelowFontSize);
document.getElementById("remaining-below").innerHTML = remainingBelowContent;
document.getElementById("remaining-below").setAttribute("x", spinnerX);
document.getElementById("remaining-below").setAttribute("y", remainingBelowY);
document.getElementById("start-end-circle").setAttribute("d", describeArc(spinnerX, spinnerY, startEndCircleRadius, 0, 359.999));
document.getElementById("start-end").setAttribute("font-size", startEndFontSize);
document.getElementById("start-end").setAttribute("x", Math.PI * startEndCircleRadius);
if (!started) {
document.getElementById("timer").style.opacity = 0.5;
// if timer not yet started: show total time
document.getElementById("spinner").setAttribute("d", describeArc(spinnerX, spinnerY, spinnerRadius, 0, 359.999));
document.getElementById("spinner-background").setAttribute("d", describeArc(spinnerX, spinnerY, spinnerRadius, 0, 359.999));
document.getElementById("remaining").innerHTML = duration + remainingLabel;
} else {
// handle pauses
if (paused) {
pCurr = (new Date()).getTime() - p.getTime();
history.replaceState({}, '', '#' + Math.round(s.getTime() / 1000) + "+" + Math.round(pTotal / 1000));
}
pTotal = pPrev + pCurr;
eWithPauses = new Date(e.getTime() + pTotal);
// if timer started: show start and end times and update timer
document.getElementById("start-end-path").innerHTML = startContent + ": " + s.toTimeString().substring(0,5) + " – " + endContent + ": " + eWithPauses.toTimeString().substring(0,5);
// tick, tock, tick, tock, ...
let remaining = Math.max(eWithPauses.getTime() - (new Date()).getTime(), 0);
let remainingDeg = remaining / (duration * 60 * 1000) * 360;
if (!paused && remaining > 0) {
document.getElementById("timer").style.opacity = 1.0;
} else {
document.getElementById("timer").style.opacity = 0.5;
}
document.getElementById("spinner").setAttribute("d", describeArc(spinnerX, spinnerY, spinnerRadius, 360 - remainingDeg, 360));
document.getElementById("spinner-background").setAttribute("d", describeArc(spinnerX, spinnerY, spinnerRadius, 0, 359.999));
document.getElementById("remaining").innerHTML = Math.ceil(remaining / (60 * 1000)) + remainingLabel;
}
// show and hide instructions depending on state
let state;
if (!started) {
state = "before";
} else {
var remaining = Math.max(eWithPauses.getTime() - (new Date()).getTime(), 0);
if (remaining > 10 * 60 * 1000) {
state = "during";
} else if (remaining > 0) {
state = "homestretch";
} else {
state = "after";
}
}
let otherStates = ["before", "during", "homestretch", "after"];
otherStates.splice(otherStates.indexOf(state), 1);
// inactive states
for (o in otherStates) {
let elements = document.getElementsByClassName(otherStates[o]);
for (let i = 0; i < elements.length; i++) {
elements[i].style.display = "none";
}
}
// active states
let elements = document.getElementsByClassName(state);
for (let i = 0; i < elements.length; i++) {
elements[i].style.display = "block";
}
}, 100);
</script>
</body>
</html>