This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
127 lines (112 loc) · 3.18 KB
/
timer.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
/*
#cClock {
position: fixed;
top: 0px;
left: 0px;
opacity: 0.25;
z-index: 1000;
}
#cClock:hover {
opacity: 1.0;
}
#cClock.hidden {
display: none;
}
window.timerConfig = window.timerConfig || {
settings : {
date: new Date('Sept 18 2011 16:22'),
minutes: 2,
warnAt: 1
}
};
*/
var PresentationTimer = function(minutes, startImmediately) {
var startTime;
var length, warnAt;
var stopTime;
if (minutes) {
length = minutes;
warnAt = 1;
} else {
length = timerConfig.settings.minutes;
startTime = timerConfig.settings.date;
warnAt = timerConfig.settings.warnAt;
}
this.start = function() {
if (!startTime) {
startTime = new Date();
}
stopTime = new Date((startTime.getTime() + (length * 60 * 1000)));
insertElement();
//console.log("starting timer", length, startTime, stopTime);
setTimeout(timerTick, 1000);
}
var insertElement = function() {
var body = document.getElementsByTagName("body")[0];
body.insertAdjacentHTML("afterbegin", '<canvas id="cClock" width="30" height="30"></canvas>');
}
var timerTick = function() {
var currentTime = new Date();
var tickLength = 1000;
var minLeft;
if (currentTime < startTime) {
//console.log("before");
minLeft = (startTime.getTime() - currentTime.getTime()) / 1000 / 60;
if (minLeft < 60) {
drawClock(Math.ceil(minLeft), "#000");
} else {
tickLength = 60000;
}
setTimeout(timerTick, tickLength);
} else if (currentTime > stopTime) {
//console.log("after");
var minOver = (currentTime.getTime() - stopTime.getTime()) / 1000 / 60;
if (minOver <= 10) {
drawClock(Math.ceil(minOver), "#f00");
setTimeout(timerTick, 15000);
} else {
showClock(false);
}
} else {
//console.log("during");
minLeft = (stopTime.getTime() - currentTime.getTime()) / 1000 / 60;
var textColor = "#000";
if (minLeft < warnAt) {
textColor = "#900";
}
drawClock(Math.ceil(minLeft), textColor);
setTimeout(timerTick, tickLength);
}
}
var drawClock = function(min, color) {
var canvas = document.getElementById("cClock");
var ctx = canvas.getContext('2d');
ctx.clearRect(0,0,30,30);
ctx.lineWidth = 3;
ctx.strokeStyle = color;
ctx.beginPath();
ctx.arc(15, 15, 10, toRadians(-90), toRadians((((min-60)/60)*360)-90), true);
ctx.stroke();
ctx.fillStyle = color;
ctx.font="10px sans-serif"
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(min, 15, 15);
}
var showClock = function(visible) {
var canvas = document.getElementById("cClock");
if (visible) {
canvas.classList.remove("hidden");
} else {
canvas.classList.add("hidden");
}
}
var toRadians = function(val) {
return (Math.PI / 180) * val;
}
if (startImmediately) {
startTime = new Date();
start();
}
return this;
}