-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
162 lines (134 loc) · 3.52 KB
/
script.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
// init stuff here
let canv;
let running = false;
let started = false;
let timeStart, timeLeft;
const timeStep = 100;
let canvH = 400;
let canvW = 400;
let intervalFunction;
loadFinished = () => {
_canv = document.getElementById('canvas');
canv = _canv.getContext('2d');
canvH = _canv.height;
canvW = _canv.width;
canv.font = "40px sans-serif";
}
/**
* This function sets up the timer and starts it. <br>
* The Value is directly gotten from the html-form. <br>
* Could be better, but for now it works...
*/
function startTimer() {
// clear all running stuff
clear();
// get timer value
let getValue = parseInt(document.getElementById('time').value);
if (isNaN(getValue)) {
alert("Something went wrong...");
return;
}
if (1 > getValue || 60 < getValue) {
alert("Value not fitting!");
return;
}
// set up timer
timeStart = getValue;
// timeLeft = left time in ms
timeLeft = timeStart * 60 * 1000;
timeStart = timeLeft;
intervalFunction = setInterval(update, timeStep);
started = true;
running = true;
}
function pause() {
if(started && running) {
clearInterval(intervalFunction);
running = false;
}
}
function resume() {
if(started && !running) {
intervalFunction = setInterval(update, timeStep);
running = true;
}
}
function clear() {
clearInterval(intervalFunction)
running = false;
timeLeft = undefined;
timeStart = undefined;
}
function update() {
// console.log(timeLeft);
timeLeft -= (timeStep);
if (timeLeft <= 0) {
clearInterval(intervalFunction);
canv.clearRect(0,0,canvW, canvH);
alert("Fertig");
return;
}
render();
}
function render() {
// clear rect
canv.clearRect(0, 0, canvH, canvW);
// draw rectangle for remaining time
let edge = canvW * (timeLeft / timeStart);
percTime = timeLeft/timeStart;
canv.fillStyle = "#ccdddd";
drawPartCirc(150, (150 + percTime * 240)%360)
g = Math.floor(255*percTime);
gs = (g<16 ? "0"+g.toString(16) : g.toString(16));
r = Math.floor(255*(1 - percTime));
rs = (r<16 ? "0"+r.toString(16) : r.toString(16));
s = "#" + rs + gs+ "00"
canv.fillStyle = "#" + rs + gs +"00";
drawPartCirc((150 + percTime * 240)%360, 30)
canv.fillText(getTimeString(timeLeft/1000), canvW /2 - 50 , canvH/2)
canv.strokeStyle="#000000";
canv.strokeText(getTimeString(timeLeft/1000), canvW /2 - 50 , canvH/2)
}
function getTimeString(time) {
min = Math.floor(time/60);
if (min<10) {
res = "0" + min.toString();
} else {
res = min.toString();
}
sec = Math.floor(time%60);
if (sec<10) {
res += ":0" + sec.toFixed();
} else {
res += ":" + sec.toFixed();
}
return res;
}
function drawPartCirc(beginAngle, endAngle) {
canv.beginPath();
canv.moveTo(
canvW/2 - (canvW/5 * Math.cos((180 - beginAngle) / 360 * 2 * Math.PI)),
canvH/2 + (canvH/5 * Math.sin((180 - beginAngle) / 360 * 2 * Math.PI))
);
canv.lineTo(
canvW/2 - (canvW/4 * Math.cos((180-beginAngle) / 360 * 2 * Math.PI)),
canvH/2 + (canvH/4 * Math.sin((180-beginAngle) / 360 * 2 * Math.PI))
);
canv.arc(canvW/2, canvH/2, canvW/4,
(beginAngle / 360) * 2 * Math.PI,
(endAngle / 360) * 2 * Math.PI);
canv.lineTo(
canvW/2 + (canvW/5 * Math.cos(endAngle / 360 * 2 * Math.PI)),
canvH/2 + (canvH/5 * Math.sin(endAngle / 360 * 2 * Math.PI))
);
canv.arc(canvW/2, canvH/2, canvW/5,
(endAngle / 360) * 2 * Math.PI,
(beginAngle / 360) * 2 * Math.PI, true);
canv.fill();
canv.stroke();
}
function test() {
canv.moveTo(canvW/2,0)
canv.lineTo(canvW/2, canvH)
canv.stroke();
}