/
clock.js
258 lines (210 loc) · 9.37 KB
/
clock.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
import { map } from "./utilities.js";
export const clockObj = (
() => {
const quoteBox = document.createElement("div");
const quoteContainer = document.createElement("div");
const quote = document.createElement("h2");
const quoteLink = document.createElement("a");
const getAnother = document.createElement("button");
const timeWarper = document.createElement("div");
const time = document.createElement("div");
const day = document.createElement("div");
const divButts = document.createElement("div");
const appearButt = document.createElement("button");
const formatButt = document.createElement("button");
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const hand = document.createElement("img");
const minWidth = 500;
let clockKiller, basedFormat, analog, darkMode;
getAnother.classList.add("change-quote");
quoteBox.classList.add("quote-box");
quoteContainer.classList.add("quote-container");
quoteLink.target = "_blank";
quoteBox.append(quote);
quoteBox.append(quoteLink);
quoteContainer.append(quoteBox);
quoteContainer.append(getAnother);
getAnother.addEventListener("click", get_quote);
time.classList.add("time");
day.classList.add("day");
timeWarper.classList.add("time-warper");
timeWarper.append(time);
timeWarper.append(day);
timeWarper.append(divButts);
divButts.classList.add("butts-container");
appearButt.classList.add("toggle-analog-digital");
formatButt.classList.add("toggle-hour-format");
appearButt.innerText = "Toggle Analog";
formatButt.innerText = "12 hour format";
divButts.append(appearButt);
divButts.append(formatButt);
hand.src = "images/hand.png";
basedFormat = true;
analog = false;
darkMode = false;
formatButt.addEventListener("click", () => {
basedFormat = !basedFormat;
formatButt.innerText = `${basedFormat ? 12 : 24} hour format`;
});
get_quote();
function get_quote() {
const iconCanvas = document.createElement("canvas");
const iconContext = iconCanvas.getContext("2d");
const subs = ["todayilearned", "showerthoughts", "lifeprotips"];
const intervals = ["month", "year", "all"];
let sub, interval, animeKiller, dt, color;
dt = 0;
color = 0;
iconCanvas.width = 115;
iconCanvas.height = 115;
quote.innerHTML = '';
quote.append(iconCanvas);
quoteLink.innerText = '';
function loading_icon() {
let circles, distance, angle, angleInc, minRad, maxRad;
circles = 20;
distance = 45;
angle = 0;
angleInc = Math.PI * 2 / circles;
minRad = 1;
maxRad = 6;
iconContext.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < circles; i++, angle += angleInc) {
let radius;
radius = minRad + Math.abs(maxRad * Math.sin(map(dt % circles, 0, circles, 0, Math.PI * 2) + i * Math.PI * 3 / circles));
iconContext.beginPath();
iconContext.fillStyle = `hsl(${(i * 10 + color) % 360}, 100%, 50%)`;
iconContext.arc(distance * Math.cos(angle) + iconCanvas.width / 2, distance * Math.sin(angle) + iconCanvas.height / 2, radius, 0, Math.PI * 2);
iconContext.fill();
}
color += 1;
dt += .1;
animeKiller = requestAnimationFrame(loading_icon);
}
loading_icon();
sub = subs[Math.floor(Math.random() * subs.length)];
interval = intervals[Math.floor(Math.random() * intervals.length)];
fetch(
`https://www.reddit.com/r/${sub}/top.json?t=${interval}&limit=100`,
{ method: "GET" }).then(res => res.json()).then(response => {
let child;
cancelAnimationFrame(animeKiller);
quote.innerHTML = '';
do {
child = response.data.children[Math.floor(Math.random() * response.data.children.length)];
} while (child.data.title.length > 200 && window.innerWidth < minWidth);
quote.innerHTML = child.data.title; //...
quoteLink.innerText = sub == "todayilearned" ? "read more" : ` by u/${child.data.author} on reddit`;
quoteLink.href = child.data.url;
});
}
if (window.innerWidth < minWidth) {
canvas.width = .9 * window.innerWidth;
canvas.height = window.innerWidth;
}
else {
canvas.width = minWidth;
canvas.height = minWidth;
}
appearButt.addEventListener("click", () => {
kill_clock();
time.innerHTML = '';
if (analog) {
appearButt.innerText = "Toggle Analog";
formatButt.style.display = "inline";
run_digital_clock();
}
else {
appearButt.innerText = "Toggle Digital";
formatButt.style.display = "none";
run_analog_clock();
}
analog = !analog;
});
function anime() {
let angle, angleInc, offsetX, offsetY, distance;
context.clearRect(0, 0, canvas.width, canvas.height);
if (darkMode) {
context.fillStyle = "#1375c8";
context.beginPath();
context.arc(canvas.width / 2, canvas.height / 2, .5 * canvas.width, 0, Math.PI * 2);
context.fill();
}
context.lineWidth = 7;
context.strokeStyle = "purple";
context.beginPath();
context.arc(canvas.width / 2, canvas.height / 2, .49 * canvas.width, 0, Math.PI * 2);
context.stroke();
angleInc = Math.PI / 6;
angle = - Math.PI / 3;
offsetX = -.025 * canvas.width;
offsetY = .03 * canvas.width;
distance = .425 * canvas.width;
context.fillStyle = darkMode? "azure" : "red";
context.font = `${.08 * canvas.width}px monospace`;
for (let i = 1; i <= 12; i++, angle += angleInc)
context.fillText(i, distance * Math.cos(angle) + canvas.width / 2 + offsetX, distance * Math.sin(angle) + canvas.height / 2 + offsetY);
if (hand.naturalWidth) {
let date;
date = new Date();
draw_arrow(canvas, hand, 150, .25 * canvas.width, map(date.getHours() % 12, 0, 12, 0, Math.PI * 2) - Math.PI);
draw_arrow(canvas, hand, 100, .35 * canvas.width, map(date.getMinutes(), 0, 60, 0, Math.PI * 2) - Math.PI);
draw_arrow(canvas, hand, 75, .45 * canvas.width, map(date.getSeconds(), 0, 60, 0, Math.PI * 2) - Math.PI);
}
context.fillStyle = "purple";
context.beginPath();
context.arc(canvas.width / 2, canvas.height / 2, 20, 0, Math.PI * 2);
context.fill();
context.beginPath();
context.fillStyle = "skyblue";
context.arc(canvas.width / 2, canvas.height / 2, 10, 0, Math.PI * 2);
context.fill();
clockKiller = setTimeout(anime, 1000);
}
function run_analog_clock() {
time.append(canvas);
anime();
}
function append_clock(container) {
container.innerHTML = '';
container.append(quoteContainer);
container.append(timeWarper);
}
function kill_clock() {
clearTimeout(clockKiller);
}
function run_digital_clock() {
const date = new Date();
let hours, minutes, seconds;
hours = String(date.getHours() % (basedFormat ? 24 : 12 + (date.getHours() == 12 ? 1 : 0))).padStart(2, '0');
minutes = String(date.getMinutes()).padStart(2, '0');
seconds = String(date.getSeconds()).padStart(2, '0');
time.innerText = `${hours}:${minutes}:${seconds} ${basedFormat ? '' : date.getHours() < 12 ? " AM" : " PM"}`;
day.innerText = new Date().toDateString();
clockKiller = setTimeout(run_digital_clock, 1000);
}
function draw_arrow(canvas, arrow, width, height, angle) {
let context;
context = canvas.getContext("2d");
context.save();
context.translate(canvas.width / 2, canvas.height / 2);
context.rotate(angle);
context.drawImage(arrow, - width / 2, 0, width, height);
context.restore();
}
function switch_to_clock(container) {
kill_clock();
time.innerHTML = '';
append_clock(container);
if (analog)
run_analog_clock();
else
run_digital_clock();
}
function enable_dark_mode(dark) {
darkMode = dark;
}
return { switch_to_clock, kill_clock, enable_dark_mode };
}
)();