-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.html
359 lines (333 loc) · 7.96 KB
/
segment.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
<!doctype html>
<meta charset='utf-8'/>
<meta name='viewport' content='width=device-width, initial-scale=1'/>
<meta name='theme-color' content='#000000'/>
<title>
Segmented Clock
</title>
<style type='text/css'>
:root {
--bg: #000;
--text: #FFF;
accent-color: var(--accent);
color-scheme: dark;
}
body {
overflow: hidden;
}
body, .btn, input, select {
font-family: 'Roboto', sans-serif;
font-size: 1rem;
margin: 0;
}
body, .btn, select {
background-color: var(--bg);
color: var(--text);
}
.btn, input, select, summary, .label {
display: block;
padding: .5em;
border-width: 1px;
}
input {
width: 100%;
}
select {
min-width: 8rem;
}
.btn {
border-color: rgba(0, 0, 0, .5);
border-style: outset;
border-width: 0px;
cursor: pointer;
display: flex;
line-height: 2;
padding: 0 .5em;
min-width: 2em;
text-decoration: none;
}
.fade {
opacity: 0.5;
}
.flex {
display: flex;
}
.nosel {
user-select: none;
}
</style>
<details class='fade flex wide'>
<summary accesskey='s' class='btn nosel' title='Settings'>☰</summary>
<label class='flex'>
<span class='label'>Offset</span>
<input id='offset' value='0' type='range' max='86400000' step='50000' onInput='synchronize()'>
</label>
<label class='flex'>
<span class='label'>Theme</span>
<select id='theme' onChange='setTheme(this.value)'></select>
</label>
<div>
<a href='..' accesskey='h' class='btn'>
<span class='hotkey'>H</span>ome
</a>
</div>
</details>
<div><canvas id='face'></canvas></div>
<script type='text/javascript'>
const _ = void 0
const doc = document
const $ = doc.querySelector.bind(doc)
const eid = doc.getElementById.bind(doc)
const locale = 'en-us'
let palettes
const color_palettes = {
'amber': '#E2A71E',
'black': '#000000',
'blue': '#0080DB',
'cyan': '#00F0F0',
'green': '#00ED00',
'magenta': '#F000F0',
'red': '#F10000',
'yellow': '#F0F000',
'white': '#F1EDDB',
}
const themes = {
Dark: {
bg: color_palettes.black,
color: color_palettes.white,
},
Light: {
bg: color_palettes.white,
color: color_palettes.black,
text: color_palettes.black,
},
Amber: {
bg: color_palettes.black,
color: color_palettes.amber,
},
Blue: {
bg: color_palettes.black,
color: color_palettes.blue,
},
Cyan: {
bg: color_palettes.black,
color: color_palettes.cyan,
},
Green: {
bg: color_palettes.black,
color: color_palettes.green,
},
Magenta: {
bg: color_palettes.black,
color: color_palettes.magenta,
},
Red: {
bg: color_palettes.black,
color: color_palettes.red,
},
Yellow: {
bg: color_palettes.black,
color: color_palettes.yellow,
},
}
for(k in themes){
const theme = themes[k]
Object.assign(theme, color_palettes)
}
const colors = ['accent', 'bg', 'color', 'text'].concat(Object.keys(color_palettes))
eid('theme').innerHTML = Object.keys(themes).map(t => `<option value='${t}'>${t}</option>`).join('')
const can = eid('face')
const c = can.getContext('2d')
const height = 30
const width = 87
const pad_h = 2
const pad_v = 4
const char_height = 10
const char_width = 14
const grid_w = char_width + 0
const grid_w2 = grid_w * 2
// zoom
let z = 1
const offset_el = eid('offset')
const {cos, floor, min, sin} = Math
function resize(){
const scale = window.devicePixelRatio || 1
z = floor(min(
window.innerWidth / width,
window.innerHeight / height,
))
can.height = height * scale * z
can.width = width * scale * z
can.style.height = (height*z)+'px'
can.style.width = (width*z)+'px'
c.scale(scale, scale)
synchronize()
}
window.addEventListener('resize', resize)
function setTheme(theme){
palettes = themes[theme]
$(`#theme [value=${theme}]`).selected = true
palettes.accent = palettes.color
colors.forEach(color => {
doc.documentElement.style.setProperty('--'+color, palettes[color])
})
$(`meta[name='theme-color']`).setAttribute('content', palettes.bg)
synchronize()
}
// display at the start of each second
function synchronize(){
window.clearTimeout(window.synchronizeTimeout)
const offset = Number(offset_el.value)
const t = new Date(Date.now() + offset)
display(t)
const r = 1000 - (t % 1000)
window.synchronizeTimeout = window.setTimeout(synchronize, r)
}
function zeropad2(s){
return ('0' + s).slice(-2)
}
function shape(path, x, y, m = 1){
m = m*z
c.beginPath()
c.moveTo((path[0][0] + x)*m, (path[0][1] + y)*m)
for(i=1; i<path.length; ++i){
c.lineTo((path[i][0] + x)*m, (path[i][1] + y)*m)
}
c.fill()
}
const tau = 2 * Math.PI
function circle(r, x, y){
c.beginPath()
c.arc(x*z, y*z, r*z, 0, tau)
c.fill()
}
const colon_sides = 6
const colon_radius = 1.16
const colon_shape = Array(colon_sides).fill().map((_,i)=>([
colon_radius * cos(i * tau / colon_sides),
colon_radius * sin(i * tau / colon_sides),
]))
function colon(x, y){
shape(colon_shape, x, y+5)
shape(colon_shape, x, y+9)
}
const db = [[4,11.625], [6,11.625], [6,9], [5,8.25], [4,9]]
const dc = [[.25,14], [9.75,14], [7.75,12], [2.25,12]]
const dl = [[0,13.75], [2,11.75], [2,8.25], [1,7.25], [0,8.25]]
const dlt = [[-.375,14], [-4,14], [-3,12], [-.375,12]]
const drt = [[10.375,14], [14,14], [13,12], [10.375,12]]
const dr = [[10,13.75], [8,11.75], [8,8.25], [9,7.25], [10,8.25]]
const ult = [[-.375,0], [-4,0], [-3,2], [-.375,2]]
const urt = [[10.375,0], [14,0], [13,2], [10.375,2]]
const mc = [[1.25, 7], [2.25,6], [7.75,6], [8.75, 7], [7.75,8], [2.25,8]]
const mr = [[5,6], [7.75,6], [8.75, 7], [7.75,8], [5,8]]
const mlt = [[-4, 7], [-3,6], [-.25,6], [.75, 7], [-.25,8], [-3,8]]
const mrt = [[14, 7], [13,6], [10.25,6], [9.25, 7], [10.25,8], [13.25,8]]
const ub = [[4,2.375], [6,2.375], [6,5], [5,5.75], [4,5]]
const uc = [[.25,0], [9.75,0], [7.75,2], [2.25,2]]
const ul = [[0,.25], [2,2.25], [2,5.75], [1,6.75], [0,5.75]]
const ur = [[10,.25], [8,2.25], [8,5.75], [9,6.75], [10,5.75]]
const segs = [ur, ul, uc, dr, dl, dc, mc, ub, db, ult, dlt, urt, drt, mlt, mrt, mr]
const char_masks = {
'0': 0b111111,
'1': 0b1001,
'2': 0b1110101,
'3': 0b1101101,
'4': 0b1001011,
'5': 0b1101110,
'6': 0b1111110,
'7': 0b1101,
'8': 0b1111111,
'9': 0b1101111,
' ': 0,
'-': 0b1000000,
':': 0b110000000,
'A': 0b1011111,
'B': 0b1000000110101101,
'C': 0b110110,
'D': 0b110101101,
'E': 0b1110110,
'F': 0b1010110,
'G': 0b1000000000111110,
'H': 0b1011011,
'I': 0b110100100,
'J': 0b111001,
'K': 0b1100001011011,
'L': 0b110010,
'M': 0b110011111,
'N': 0b0000011111,
'O': 0b111111,
'P': 0b1010111,
'Q': 0b1000000111111,
'R': 0b1001011111,
'S': 0b1101110,
'T': 0b110000100,
'T2': 0b1000010110,
'T3': 0b100000001101,
'U': 0b111011,
'V': 0b100010011,
'W': 0b100111011,
'X': 0b1111001011011,
'Y': 0b101000011,
'Z': 0b1110101,
'?': 0b11111111111,
}
function mask_segs(mask){
let s = []
let i = 0
let i_mask = 1
while(i_mask <= mask){
if(mask & i_mask){
s.push(segs[i])
}
++i
i_mask = i_mask << 1
}
return s
}
const char_paths = Object.fromEntries(
Object.entries(char_masks).map(
([k, mask], i) => [k, mask_segs(mask)]
)
)
function draw_char(s, x, y, m = 1){
(char_paths[s] || char_paths['?']).forEach(
path => shape(path, x, y, m)
)
}
function draw_str(chars, x, y, m = 1, gw = grid_w){
if(!(chars instanceof Array)){
chars = (chars+'').split('')
}
chars.forEach((s, i) => {
draw_char(s, x + i * gw, y, m)
})
}
function display(t){
const month = ((t.getMonth() + 1)+'').padStart(2)
const weekday = t.toLocaleString(locale, {weekday:'short'}).toUpperCase().slice(0,3)
const hours = zeropad2(t.getHours())
const minutes = zeropad2(t.getMinutes())
const seconds = zeropad2(t.getSeconds())
const day_of_month = zeropad2(t.getDate())
c.fillStyle = palettes.color
c.clearRect(0, 0, width*z, height*z)
let x = pad_h, y = pad_v, m = .5
draw_str(weekday, x = pad_h/m + 10, y, m)
draw_str(month, x += grid_w*3 + 6, y, m)
shape(
[[2.25, 7], [3.25,6], [6.75,6], [7.75, 7], [6.75,8], [3.25,8]],
x + grid_w2 - 3, y, m
)
draw_str(day_of_month, x += grid_w2+8, y, m)
x = pad_h
y += 10
draw_str(hours, x, y)
colon(x+29.5, y)
draw_str(minutes, x += grid_w2+7, y)
m = 0.75
draw_str(seconds, (x + grid_w2)/m, (y+3.5)/m, m)
}
setTheme('Amber')
resize()
</script>