-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht3timer-dev.js
428 lines (377 loc) · 12.1 KB
/
t3timer-dev.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Key codes
const BUTTON_OK = 13;
const BUTTON_LEFT = 37;
const BUTTON_UP = 38;
const BUTTON_RIGHT = 39;
const BUTTON_DOWN = 40;
const BUTTON_A = 65;
const BUTTON_B = 66;
const BUTTON_C = 67;
const BUTTON_D = 68;
const BUTTON_E = 69;
const BUTTON_F = 70;
const BUTTON_0 = 48;
const BUTTON_1 = 49;
const BUTTON_2 = 50;
const BUTTON_3 = 51;
const BUTTON_4 = 52;
const BUTTON_5 = 53;
const BUTTON_6 = 54;
const BUTTON_7 = 55;
const BUTTON_8 = 56;
const BUTTON_9 = 57;
// States
const CLOCK_SPLASH = 0;
const CLOCK_CONFIG = 1;
const CLOCK_PAUSED = 2;
const CLOCK_STARTED = 3;
const CLOCK_WALLCLOCK = 4;
// Default values
const DEFAULT_MINUTES = 15;
const DEFAULT_SECONDS = 0;
// Offset constants
const DIGIT_TENMINUTES = 0;
const DIGIT_MINUTES = 1;
const DIGIT_TENSECONDS = 2;
const DIGIT_SECONDS = 3;
// Event handlers
function init()
{
new logoanimation().animate();
clock = new clockdisplay(DEFAULT_MINUTES, DEFAULT_SECONDS, CLOCK_SPLASH);
clock.animate();
document.documentElement.addEventListener("keydown", keyDownHandler, false);
playaudio("beep");
}
function playaudio(id)
{
var elm = document.getElementById(id);
if (elm != undefined && elm instanceof HTMLAudioElement)
elm.play();
}
keyPressInProgress = false;
function keyDownHandler(evt)
{
// This is a keyDown event. Therefore, we need to limit incoming events...
if (!keyPressInProgress)
{
keyPressInProgress = true;
setTimeout(function() { keyPressInProgress = false;}, 200);
playaudio("beep");
switch(clock.state)
{
case CLOCK_SPLASH: handlesplashkeys(evt.keyCode); break;
case CLOCK_CONFIG: handleconfigkeys(evt.keyCode); break;
case CLOCK_PAUSED: handlepausedkeys(evt.keyCode); break;
case CLOCK_STARTED: handlestartedkeys(evt.keyCode); break;
case CLOCK_WALLCLOCK: handlewallclockkeys(evt.keyCode); break;
default: break;
}
}
}
// User input
function handlesplashkeys(code)
{
if (code == BUTTON_D)
enterTimer();
else if (code == BUTTON_B)
enterWallclock();
}
function handleconfigkeys(code)
{
switch(code)
{
case BUTTON_C: enterSplash(); break;
case BUTTON_D: startClock(); break;
case BUTTON_UP: clock.up(); break;
case BUTTON_DOWN: clock.down(); break;
case BUTTON_LEFT: clock.left(); break;
case BUTTON_RIGHT: clock.right(); break;
case BUTTON_0: clock.setSelectedDigit(0); clock.right(); break;
case BUTTON_1: clock.setSelectedDigit(1); clock.right(); break;
case BUTTON_2: clock.setSelectedDigit(2); clock.right(); break;
case BUTTON_3: clock.setSelectedDigit(3); clock.right(); break;
case BUTTON_4: clock.setSelectedDigit(4); clock.right(); break;
case BUTTON_5: clock.setSelectedDigit(5); clock.right(); break;
case BUTTON_6: clock.setSelectedDigit(6); clock.right(); break;
case BUTTON_7: clock.setSelectedDigit(7); clock.right(); break;
case BUTTON_8: clock.setSelectedDigit(8); clock.right(); break;
case BUTTON_9: clock.setSelectedDigit(9); clock.right(); break;
default: break;
}
}
function handlepausedkeys(code)
{
if (code == BUTTON_C)
showConfig();
else if (code == BUTTON_D)
startClock();
}
function handlestartedkeys(code)
{
if (code == BUTTON_C)
showConfig();
else if (code == BUTTON_D)
pauseClock();
}
function handlewallclockkeys(code)
{
if (code == BUTTON_C)
enterSplash();
}
function setInstructions(newid)
{
document.getElementById("instructions").setAttribute("xlink:href", "#" + newid);
}
function enterSplash()
{
clock.state = CLOCK_SPLASH;
zoomin();
}
function enterTimer()
{
showConfig();
clock.render();
zoomout();
}
function enterWallclock()
{
setInstructions("wallclockinstructions");
clock.state = CLOCK_WALLCLOCK;
clock.setWallclockTime();
clock.render();
zoomout();
}
function showConfig()
{
setInstructions("configinstructions");
clock.state = CLOCK_CONFIG;
clock.reset();
}
function startClock()
{
setInstructions("startedinstructions");
clock.state = CLOCK_STARTED;
}
function pauseClock()
{
setInstructions("pausedinstructions");
clock.state = CLOCK_PAUSED;
}
// The clock object
function clockdisplay(defaultmins, defaultsecs, initialstate)
{
this.totalminutes = defaultmins;
this.totalseconds = defaultsecs;
this.state = initialstate;
this.newsecond = true;
this.time = new Date();
this.reset();
}
clockdisplay.prototype.reset = function()
{
this.timeminutes = this.totalminutes;
this.timeseconds = this.totalseconds;
this.showblinking = true;
this.selectedDigit = DIGIT_TENMINUTES;
}
clockdisplay.prototype.up = function()
{
this.setSelectedDigit(this.getSelectedDigit() + 1);
}
clockdisplay.prototype.down = function()
{
this.setSelectedDigit(this.getSelectedDigit() - 1);
}
clockdisplay.prototype.left = function()
{
this.selectedDigit = (this.selectedDigit + 3) % 4;
this.render();
}
clockdisplay.prototype.right = function()
{
this.selectedDigit = (this.selectedDigit + 1) % 4;
this.render();
}
clockdisplay.prototype.getSelectedDigit = function()
{
switch(this.selectedDigit)
{
case DIGIT_TENMINUTES: return Math.floor(this.timeminutes / 10);
case DIGIT_MINUTES: return this.timeminutes % 10;
case DIGIT_TENSECONDS: return Math.floor(this.timeseconds / 10);
case DIGIT_SECONDS: return this.timeseconds % 10;
default: return 0;
}
}
clockdisplay.prototype.setSelectedDigit = function(newdigit)
{
switch(this.selectedDigit)
{
case DIGIT_TENMINUTES: this.timeminutes = this.totalminutes = ((newdigit + 10) % 10) * 10 + (this.totalminutes % 10); break;
case DIGIT_MINUTES: this.timeminutes = this.totalminutes = ((newdigit + 10) % 10) + (this.totalminutes - this.totalminutes % 10); break;
case DIGIT_TENSECONDS: this.timeseconds = this.totalseconds = ((newdigit + 6) % 6) * 10 + (this.totalseconds % 10); break;
case DIGIT_SECONDS: this.timeseconds = this.totalseconds = ((newdigit + 10) % 10) + (this.totalseconds - this.totalseconds % 10); break;
default: break;
}
this.render();
}
clockdisplay.prototype.animate = function()
{
var c = this;
var newtime = new Date();
var timediff = newtime - this.time;
this.time = newtime;
if (this.state == CLOCK_WALLCLOCK)
window.setTimeout(function(){c.animate();}, 100)
else
window.setTimeout(function(){c.animate();}, this.newsecond ? 500: 1000 - timediff)
this.newsecond = !this.newsecond;
this.render();
}
clockdisplay.prototype.render = function()
{
var separator = document.getElementById("separator");
var progressbar = document.getElementById("progressbar");
var tenminutes = document.getElementById("tenminutes");
var minutes = document.getElementById("minutes");
var tenseconds = document.getElementById("tenseconds");
var seconds = document.getElementById("seconds");
minutes.textContent = this.timeminutes % 10;
tenminutes.textContent = Math.floor(this.timeminutes / 10);
var bboxmin = minutes.getBBox();
tenminutes.setAttribute("x", bboxmin.x);
tenseconds.textContent = Math.floor(this.timeseconds / 10);
seconds.textContent = this.timeseconds % 10;
var bboxsec = tenseconds.getBBox();
seconds.setAttribute("x", bboxsec.x + bboxsec.width);
if (this.state == CLOCK_WALLCLOCK)
{
var secs = this.time.getSeconds();
progressbar.setAttribute("width", (secs / 60) * 1240);
}
else
{
var alltime = this.totalminutes * 60 + this.totalseconds;
var elapsedtime = this.timeminutes * 60 + this.timeseconds;
if (this.totalminutes * 60 + this.totalseconds != 0 && alltime >= elapsedtime)
progressbar.setAttribute("width", ((alltime - elapsedtime) / alltime) * 1240);
else
progressbar.setAttribute("width", 0);
}
if (this.state == CLOCK_STARTED)
{
this.showblinking = !this.newsecond;
setVisible(tenminutes, true);
setVisible(minutes, true);
setVisible(tenseconds, true);
setVisible(seconds, true);
setVisible(separator, this.showblinking);
if (this.newsecond)
this.timeseconds--;
if (this.timeseconds < 0)
{
this.timeseconds = 59;
this.timeminutes--;
}
if (this.timeminutes < 0)
{
this.state = CLOCK_PAUSED;
this.timeminutes = 0;
this.timeseconds = 0;
}
if (this.timeminutes == 5 && this.timeseconds == 0 && this.newsecond)
playaudio("5minutes");
}
else if (this.state == CLOCK_CONFIG)
{
this.showblinking = !this.showblinking;
setVisible(tenminutes, this.showblinking || this.selectedDigit != 0);
setVisible(minutes, this.showblinking || this.selectedDigit != 1);
setVisible(tenseconds, this.showblinking || this.selectedDigit != 2);
setVisible(seconds, this.showblinking || this.selectedDigit != 3);
setVisible(separator, true);
}
else if (this.state == CLOCK_PAUSED)
{
this.showblinking = !this.showblinking;
setVisible(tenminutes, this.showblinking);
setVisible(minutes, this.showblinking);
setVisible(tenseconds, this.showblinking);
setVisible(seconds, this.showblinking);
setVisible(separator, this.showblinking);
if (this.timeminutes == 0 && this.timeseconds == 0)
playaudio("timesup");
}
else if (this.state == CLOCK_WALLCLOCK)
{
this.showblinking = !this.newsecond;
setVisible(tenminutes, true);
setVisible(minutes, true);
setVisible(tenseconds, true);
setVisible(seconds, true);
setVisible(separator, this.time.getSeconds() % 2 == 0);
this.setWallclockTime();
}
else
{
setVisible(tenminutes, true);
setVisible(minutes, true);
setVisible(tenseconds, true);
setVisible(seconds, true);
setVisible(separator, true);
}
}
clockdisplay.prototype.setWallclockTime = function()
{
this.timeminutes = this.time.getHours();
this.timeseconds = this.time.getMinutes();
}
function setVisible(domobj, visible)
{
domobj.setAttribute("style", visible ? "visibility:visible;" : "visibility:hidden;");
}
// Animations
function zoomout()
{
zoom(3, 3, -2600, -1550, 1, 1, 0, 0, 0, 30);
}
function zoomin()
{
zoom(1, 1, 0, 0, 3, 3, -2600, -1550, 0, 30);
}
function zoom(fromzoomx, fromzoomy, fromoffsetx, fromoffsety, tozoomx, tozoomy, tooffsetx, tooffsety, step, totalsteps)
{
step++;
var canvas = document.getElementById("canvas");
var newfraction = step / totalsteps;
var oldfraction = 1.0 - newfraction;
canvas.setAttribute("transform", "matrix(" + (oldfraction * fromzoomx + newfraction * tozoomx) + ", 0, 0, " + (oldfraction * fromzoomy + newfraction * tozoomy) + ", " + (oldfraction * fromoffsetx + newfraction * tooffsetx) + ", " + (oldfraction * fromoffsety + newfraction * tooffsety) + ")");
if (step < totalsteps)
window.setTimeout(function(){zoom(fromzoomx, fromzoomy, fromoffsetx, fromoffsety, tozoomx, tozoomy, tooffsetx, tooffsety, step, totalsteps);}, 10);
}
function logoanimation()
{
this.logo = document.getElementById("animatedlogo");
var bbox = this.logo.getBBox();
var x = parseInt(this.logo.getAttribute("x"), 10);
var y = parseInt(this.logo.getAttribute("y"), 10);
this.centerx = bbox.x + bbox.width / 2;
this.centery = bbox.y + bbox.height / 2;
// Chrome and Firefox do not have the same definition of BBox as Opera do. Therefore, we need a little hack here...
if (bbox.x < x && bbox.y < y)
{
this.centerx += x;
this.centery += y;
}
this.angle = 0;
this.anglestep = 1;
this.timestep = 25;
}
logoanimation.prototype.animate = function()
{
var animation = this;
window.setTimeout(function(){animation.animate();}, this.timestep);
this.logo.setAttribute ("transform", "rotate(" + this.angle + " " + this.centerx + " " + this.centery + ")");
this.angle = (this.angle + this.anglestep) % 360;
}