-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
executable file
·525 lines (441 loc) · 12.5 KB
/
index.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>canvas</title>
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=yes" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<style type="text/css">
body {background-color: #000; overflow: hidden; font-family: sans-serif;}
#control-panel {
color: #fff;
background-color: #000;
position: absolute;
z-index: 1000;
}
#control-panel:hover {
background-color: #fff;
color: #000;
transition: background-color .5s, color .5s;
}
#handle {
padding: 1em 1em 1em 1em;
margin: 0em;
}
.inner {
padding: 1em;
margin: 1em;
display: none;
}
#control-panel:hover .inner {
display: block;
}
#control-panel label {
display: block;
}
#control-panel input {
display: block;
}
#control-panel.very-visible {
color: #000;
background-color: #fff;
}
#control-panel.very-visible .inner {
display: block;
}
</style>
<script type="text/javascript">
// objects
var global_canvas = null;
// flags
var global_shutdown = false;
var global_focused = true;
var color_wheel = [
['blue', 'orange'],
['yellow', 'purple'],
['green', 'red'],
['red', 'blue'],
['purple', 'orange']
];
function get_color_set(mag) {
var m = Math.max(1, mag);
var placevalue = Math.pow(10, m);
var placeholder = Math.pow(10, m-1);
var index = (Math.floor((state.end_time / placeholder)) % placevalue) % color_wheel.length;
var pair = color_wheel[index];
return pair;
}
var state = {
duration_time: null, // minutes
end_time: null, // date epoch
};
function get_query_arguments() {
var qs = (function(a) {
if (a == "") return {};
var b = {};
for (var i = 0; i < a.length; ++i)
{
var p=a[i].split('=', 2);
if (p.length == 1)
b[p[0]] = "";
else
b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}
return b;
})(window.location.hash.substr(1).split('&'));
return qs;
}
function linear_easing(t, b, c, d) { return c*t/d + b; }
function draw() {
var base_color = '#000';
var base_radius = get_radius();
var minute_circle = new fabric.Circle({
radius: base_radius,
angle: -90,
startAngle: 0,
endAngle: .01,
stroke: base_color,
strokeWidth: 25,
strokeLineCap: 'round',
strokeLineJoin: 'bevil',
fill: 'transparent',
});
var duration_circle = new fabric.Circle({
radius: base_radius-35,
angle: -90,
startAngle: 0,
endAngle: .05,
stroke: base_color,
strokeWidth: 40,
strokeLineCap: 'round',
strokeLineJoin: 'bevil',
fill: 'rgba(0,0,0,0)'
});
var minute_circle_groove = new fabric.Circle({
radius: base_radius,
angle: 0,
stroke: 'white',
strokeWidth: 25,
strokeLineCap: 'round',
strokeLineJoin: 'bevil',
fill: 'transparent',
opacity: .2
});
var duration_circle_groove = new fabric.Circle({
radius: base_radius-35,
angle: 0,
stroke: 'white',
strokeWidth: 40,
strokeLineCap: 'round',
strokeLineJoin: 'bevil',
fill: 'transparent',
opacity: .2
});
var track_circle = new fabric.Circle({
radius: base_radius+30,
angle: 0,
stroke: 'white',
strokeWidth: 1,
strokeLineCap: 'square',
fill: 'transparent',
opacity: .1
});
var track_indicator = new fabric.Circle({
radius: 5,
angle: 0,
stroke: '#999',
strokeWidth: 1,
strokeLineCap: 'square',
fill: 'white',
opacity: .15,
top: 100,
left: 100,
originX: 'center',
originY: 'center',
});
var track_indicator_clone = track_indicator.clone();
track_indicator_clone.set({
top: 100,
left: 100
});
var track_group = new fabric.Group([ track_circle, track_indicator, track_indicator_clone ], {
angle: 0,
originX: 'center',
originY: 'center',
});
global_canvas.centerObject(track_group);
track_indicator.set({
top: track_circle.get('top'),
left: 0
});
track_indicator_clone.set({
top: track_circle.get('height') + track_circle.get('top'),
left: 0
});
var minute_colors = get_color_set(1); // 1-rightmost 123x
var duration_colors = get_color_set(2); // 2-rightmost 12x4
minute_circle.setGradient('stroke', {
x1: -minute_circle.width / 2,
y1: 0,
x2: minute_circle.width / 2,
y2: 0,
colorStops: {
0: minute_colors[0],
1: minute_colors[1]
}
});
duration_circle.setGradient('stroke', {
x1: -duration_circle.width / 2,
y1: 0,
x2: duration_circle.width / 2,
y2: 0,
colorStops: {
0: duration_colors[0],
1: duration_colors[1]
}
});
minute_circle_groove.setGradient('stroke', {
x1: -minute_circle_groove.width / 2,
y1: 0,
x2: minute_circle_groove.width / 2,
y2: 0,
colorStops: {
0: minute_colors[0],
1: minute_colors[1]
}
});
duration_circle_groove.setGradient('stroke', {
x1: -duration_circle_groove.width / 2,
y1: 0,
x2: duration_circle_groove.width / 2,
y2: 0,
colorStops: {
0: duration_colors[0],
1: duration_colors[1]
}
});
var text = new fabric.Text('...', {
left: 100,
top: 100,
fill: 'white',
textAlign: 'center',
fontFamily: 'sans-serif',
fontSize: '32'
});
global_canvas.centerObject(text);
global_canvas.add(text);
global_canvas.bringToFront(text);
global_canvas.centerObject(minute_circle);
global_canvas.centerObject(duration_circle);
global_canvas.centerObject(minute_circle_groove);
global_canvas.centerObject(duration_circle_groove);
global_canvas.add(minute_circle);
global_canvas.add(duration_circle);
global_canvas.add(minute_circle_groove);
global_canvas.add(duration_circle_groove);
global_canvas.sendToBack(minute_circle_groove);
global_canvas.sendToBack(duration_circle_groove);
global_canvas.centerObject(track_group);
global_canvas.add(track_group);
var twopi = (2*Math.PI);
global_canvas.renderAll();
var timeout = false;
var accepted = false;
function text_tick() {
var time = moment.duration(get_time_remaining());
var string = time.humanize(true);
var output = 'done\n' + string;
text.set('text', output);
document.title = output + ' - canvas countdown';
global_canvas.centerObject(text);
console.log('text_tick completed');
}
var minute_completed = get_minute_completed();
minute_circle.set('endAngle', minute_completed * twopi);
function minute_tick() {
if (global_shutdown) {return;}
text_tick();
minute_circle.animate('endAngle', twopi, {
duration: (1000 * 59),
easing: linear_easing,
onChange: global_canvas.renderAll.bind(global_canvas),
onComplete: function() {
minute_circle.animate('opacity', 0, {
duration: 1000,
easing: linear_easing,
onChange: global_canvas.renderAll.bind(global_canvas),
onComplete: function() {
console.log('minute_tick completed');
minute_circle.set('opacity', 1);
minute_circle.set('endAngle', 0);
if (global_shutdown == false) {
minute_tick();
}
}
});
}
});
}
minute_tick();
function track_timeout() {
if (timeout == true) {
console.log('track_timeout completed');
track_circle.set({'stroke': 'red', 'opacity': .2});
track_indicator.set({'fill': 'red', 'opacity': .25});
track_indicator_clone.set({'fill': 'red', 'opacity': .25});
if (timeout == true && accepted == false) {
minute_circle.set('flipY', true);
accepted = true;
}
}
}
function track_tick() {
track_group.animate('angle', 360, {
duration: 2000,
onChange: global_canvas.renderAll.bind(global_canvas),
easing: linear_easing,
onComplete: function() {
console.log('track_tick completed');
track_group.set('angle', 0);
if (global_shutdown == false) {
track_tick();
}
}
});
}
track_tick();
var remaining = get_time_remaining();
text_tick();
var completed = get_duration_completed();
duration_circle.set('endAngle', completed * twopi);
duration_circle.animate('endAngle', twopi, {
duration: (remaining),
easing: linear_easing,
onChange: global_canvas.renderAll.bind(global_canvas),
onComplete: function() {
timeout = true;
track_timeout();
text_tick();
console.log('duration_circle completed');
}
});
global_canvas.selection = false;
global_canvas.forEachObject(function(o) {
o.selectable = false;
});
}
function get_radius() {
var min = Math.min(window.innerWidth, window.innerHeight);
var value = Math.floor(min / 2.718) - 60;
var lowest = 200;
var radius = Math.max(value, lowest);
console.log('radius calculated: %o; based on min w/h = %o | scaled = %o | lowest = %o', radius, min, value, lowest);
return radius;
}
function window_adapt() {
global_canvas.setWidth(window.innerWidth);
global_canvas.setHeight(window.innerHeight);
global_canvas.clear();
}
window.addEventListener('resize', function() {
console.log('window sized changed; adjusting')
window_adapt();
draw();
}, false);
function get_time_remaining() {
var now = (new Date().getTime());
var diff = state.end_time - now;
return diff;
}
function get_duration_completed() {
var now = (new Date().getTime());
var end = (state.end_time);
var d = 1 - ((end - now)/(state.duration * 60 * 1000))
var completed = d;
console.log("duration completed = %o%; based on %o | %o", Math.round(100 * completed), now, state.end_time);
return completed;
}
function get_minute_completed() {
var now = Math.floor((new Date().getTime()) / 1000);
var mod = now % 100; // gets rightmost two digits
return mod / 100;
}
function clock_initialization(query) {
state.duration = parseInt(query.d);
state.end_time = parseInt(query.e);
}
function shutdown_initialization(delay) {
function shutdown() {
var svg = global_canvas.toSVG();
global_canvas.dispose();
$('#container').html(svg);
global_shutdown = true;
}
function abort() {
var previous = global_focused;
var focused = document.hasFocus();
if (previous == false && focused == true) {
location.reload();
}
if ( global_shutdown == false && focused == false ) {
console.log('window was idle; shutting down - please refresh');
shutdown();
}
global_focused = focused;
console.log('window focused = %o', focused);
}
setInterval(abort, delay);
}
function start() {
global_canvas = new fabric.StaticCanvas('canvas');
var query = get_query_arguments();
if (query.e && query.d) {
console.log('query arguments set: %o', query);
if (query.s && query.s == '0') {
console.log('shutdown to svg disabled');
} else if (query.s) {
var _customized = parseFloat(query.s) * 60 * 1000;
shutdown_initialization(_customized);
console.log('shutdown to svg enabled at customized timeout: %o', _customized)
} else {
var _default = 2 * 60 * 1000;
console.log('shutdown to svg enabled at default timeout: %o', _default);
shutdown_initialization(_default);
}
clock_initialization(query);
window_adapt();
draw();
} else {
$('#control-panel').addClass('very-visible');
}
$('#save').on('click', function(event){
var duration = $('#duration').val();
var timeout = $('#timeout').val();
var hash = 'd=' + duration + '&e=' + (new Date().getTime() + (duration * 60 * 1000)) + '&s=' + timeout;
window.location.hash = hash;
location.reload();
});
}
window.onload = start;
</script>
</head>
<body>
<div id="control-panel">
<h4 id="handle">Control Panel</h4>
<div class="inner">
<fieldset>
<label>Duration: </label>
<input type="number" id="duration" value="60" placeholder="minutes" />
<label>Idle Timout: </label>
<input type="number" id="timeout" value="0" placeholder="minutes (or 0 for never)" />
<input type="button" id="save" value="Save" />
</fieldset>
</div>
</div>
<div id="container">
<canvas id="canvas" width="250" height="250"></canvas>
</div>
</body>
</html>