-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.html
410 lines (386 loc) · 8.39 KB
/
card.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
<!doctype html>
<meta charset='utf-8'/>
<meta name='viewport' content='width=device-width, initial-scale=1'/>
<meta name='theme-color' content='#000000'/>
<title>
Cards
</title>
<style type='text/css'>
:root {
--accent: #808080;
accent-color: var(--accent);
color-scheme: dark;
}
body, input, select {
background: #000;
color: #FFF;
display: flex;
flex-direction: column;
font-family: sans-serif;
font-size: 1em;
margin: 0;
}
body {
overflow-x: hidden;
min-height: 100vh;
}
label {
display: flex;
}
input, select, .label {
padding: .5em;
}
input, select {
border: 0;
}
.flex {
display: flex;
justify-content: center;
}
.grow {
flex-grow: 1;
}
.center {
align-self: center;
}
.border {
border: #404040 solid;
}
.label {
border-width: 0 1px 0 0;
}
#field {
border-width: 1px 0;
}
#color-label {
border-width: 1px 1px 0;
}
</style>
<label id='color-label' class='center border'>
<span class='label border'>Color</span>
<select id='color' class='grow' onChange='setColor(this.value)'></select>
</label>
<canvas id='field' class='center border'></canvas>
<script type='text/javascript'>
const _ = void 0
const eid = document.getElementById.bind(document)
const {asin, ceil, cos, floor, max, min, PI, pow, random, sin, sqrt} = Math
const phi = (1 + sqrt(5)) * .5
const tau = 2 * PI
const tau_4 = tau / 4
const tau_8 = tau / 8
const height = 104
const width = 274
const pad_h = 0
const pad_v = 2
let z = 1 // zoom
const dl = 16
const pip_size = 2.75
const cs = 3
const g = dl + cs + 1
const gy = dl + 1
const cf = cs + 2
const cmax = 13
const ps = 30
let palettes
const color_palettes = {
'black': '#000000',
'blue': '#002040',
'blue2': '#004080',
'gray_32': '#202020',
'gray': '#C0C0C0',
'green_light': '#C0FFC0',
'magenta': '#F000F0',
'red': '#E00000',
'white': '#F1EDDB',
}
const card_colors = ['black', 'blue', 'gray_32']
const obj_colors = {
die: 'blue',
}
const number_colors = {
4: 'blue2',
[ps]: 'blue2',
}
const rots = {
3: tau_4,
4: tau_8,
5: tau_4,
6: 0,
7: tau_4,
8: tau/16,
}
const y_offsets = {
3: 4/3,
}
const themes = {
Dark: {
background: color_palettes.black,
color: color_palettes.white,
},
}
const polygons = []
for(k in themes){
const theme = themes[k]
Object.assign(theme, color_palettes)
}
eid('color').innerHTML = card_colors.map(
t => `<option value='${t}' ${t===obj_colors.die ? 'selected' : ''}>${capitalize(t)}</option>`
).join('')
const can = eid('field')
const c = can.getContext('2d')
function capitalize(s) {
return s.charAt(0).toUpperCase() + s.slice(1)
}
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 = eid('color-label').style.width = (width*z)+'px'
c.scale(scale, scale)
synchronize()
}
window.addEventListener('resize', resize)
function setColor(color){
obj_colors.die = color
resize()
}
function setTheme(theme){
palettes = themes[theme]
can.style.backgroundColor = palettes.background
}
// display at the start of each second
function synchronize(){
window.clearTimeout(window.synchronizeTimeout)
const offset = 0
const t = new Date(Date.now() + offset)
display(t)
const r = 1000 - (t % 1000)
// window.synchronizeTimeout = window.setTimeout(synchronize, r)
}
function draw_shape(path, x, y, m = 1){
m = m*z
c.beginPath()
c.moveTo(x*z + (path[0][0])*m, y*z + (path[0][1])*m)
for(i=1; i<path.length; ++i){
c.lineTo(x*z + (path[i][0])*m, y*z + (path[i][1])*m)
}
c.fill()
}
function circle(r, x, y){
c.beginPath()
c.arc(x*z, y*z, r*z, 0, tau)
c.fill()
}
function polygon(sides, radius = 1, rot = tau_4){
return Array(sides).fill().map((_,i)=>([
radius * cos(i * tau / sides - rot),
radius * sin(i * tau / sides - rot),
]))
}
function polygram(
sides = 3,
radius = 1,
rot = 0,
inner_radius = sin(asin(phi - 1)) / (1 + sin(asin(phi - 1))),
){
return Array(sides*2).fill().map((_,i)=>([
(i%2 ? 1 : inner_radius) * radius * sin(i * tau / (2*sides) - rot),
(i%2 ? 1 : inner_radius) * radius * cos(i * tau / (2*sides) - rot),
]))
}
function draw_die(
pips,
x,
y,
color,
dimensions = [],
round = z*dl/8
){
let [columns, rows] = dimensions
rows ??= columns ??= ceil(sqrt(pips.length))
const s = max(columns, rows)
const dgc = dl/(columns+1)
const dgr = dl/(rows+1)
const ar = 1
if(color){
c.beginPath()
c.roundRect(x*z, y*z, dl*z*ar, dl*z, round)
c.fillStyle = color_palettes[color] || color_palettes[obj_colors.die]
c.fill()
}
pips.forEach((pip, i)=>{
if(!pip){ return }
c.fillStyle = pip.color ?? color_palettes[number_colors[pip]] ?? obj_colors.pip
let poly = pip.shape ?? polygons[pip]
if(!poly){
const key = `pip=${pip},s=${s}`
poly = polygons[key] ??= polygon(pip, min(dgr,dgc)/pip_size, rots[pip])
}
draw_shape(
poly,
x + dgc*(1 + i % columns),
y + dgr*(1 + floor(i / columns)),
)
})
}
const card_abbrs = {
1: 'A',
11: 'J',
12: 'Q',
13: 'K',
}
const card_patterns = {
1: [0b1n, [1,1], 'ace'],
2: [0b10000000000000000010n],
3: [0b10000000010000000010n],
4: [0b101000000000000000101n],
5: [0b101000000010000000101n],
6: [0b101000000101000000101n],
7: [0b101000000101000010101n],
8: [0b101010000101000010101n],
9: [0b101000101010101000101n],
10: [0b101010101000101010101n],
/*J*/ 11: [0b11010011001100010001110n, [4,6]],
/*Q*/ 12: [0b10100010101010100000100010101000100n, [5,7]],
/*K*/ 13: [0b1001101010011100101010011n, [5,5]],
}
function card_pattern_decode(mask, pip){
let pips = []
let i = 0
let i_mask = 1n
while(i_mask <= mask){
pips.push((mask & i_mask) ? pip : _)
++i
i_mask = i_mask << 1n
}
return pips
}
function draw_card(
[card_val, card_suit],
x,
y,
color = obj_colors.die,
round = z*dl/16,
){
const suit = suits[card_suit]
let dimensions, pips
if(card_val === 0){
pips = Array(121).fill().map((_,i)=>i%3 ? 4 : _)
}
else{
const card_pattern = card_patterns[card_val]
if(!card_pattern){
throw new Error(`card_patterns[${card_val}] not found`)
}
pips = card_pattern_decode(
card_pattern[0],
suit[card_pattern[2] || 'pip'],
)
dimensions = card_pattern[1] || [3,7]
}
c.beginPath()
c.roundRect(x*z, y*z, (dl+cs)*z, dl*z, round)
c.fillStyle = color_palettes[color]
c.fill()
c.strokeStyle = color_palettes['black']
c.stroke()
if(suit){
c.fillStyle = color_palettes[suit.color]
let s = card_abbrs[card_val] || (card_val+'')
const m = pow(s.length, -3/4)
c.font = `${(6)*m*z}px monospace`
c.fillText(
s,
(x + 2 - 1/m)*z,
(y + 7 - 2/m)*z,
)
draw_shape(
suit.pip.shape,
x + 2.5,
y + 7,
)
}
draw_die(pips, x+cs+.5, y, _, dimensions, [0, round, round, 0])
}
const suits = [
{
name: 'spade',
color: 'gray',
shape: [[0,50],[5,73],[25,85],[46,73],[25,100],[75,100],[52,73],[75,85],[95,73],[100,50],[50,0]],
ace_scale: 20,
pip_scale: 45,
radius: 50,
},
{
name: 'heart',
color: 'red',
shape:[[50,100],[0,35],[5,12],[25,0],[50,12],[75,0],[95,12],[100,35]],
ace_scale: 20,
pip_scale: 45,
radius: 50,
},
{
name: 'club',
color: 'green_light',
shape:[[50,0],[29,9],[25,35],[0,50],[5,73],[25,85],[45,75],[25,100],[75,100],[55,75],[75,85],[95,73],[100,50],[75,35],[71,9]],
ace_scale: 20,
pip_scale: 45,
radius: 50,
},
{
name: 'diamond',
color: 'magenta',
ace_scale: 20,
},
]
const csuits = suits.length
for(let i = 0; i <= 2; ++i){
const suit = suits[i]
const {ace_scale, color, pip_scale, radius, shape} = suit
suit.ace = {
color: color_palettes[color] || color,
shape: shape.map(a=>[(a[0]-radius)/ace_scale, (a[1]-radius)/ace_scale])
}
suit.pip = {
color: color_palettes[color] || color,
shape: shape.map(a=>[(a[0]-radius)/pip_scale, (a[1]-radius)/pip_scale])
}
}
suits[3].ace = {
color: suits[3].color,
shape: polygram(4, 50/suits[3].ace_scale, tau_8, sqrt(3)/3),
}
suits[3].pip = {
color: suits[3].color,
shape: polygram(4, dl/((4+1) * 2.75), tau_8, sqrt(3)/3),
}
function display(t){
c.clearRect(0, 0, width*z, height*z)
let x, y = pad_v
x = pad_h - cf
Array(cmax * csuits).fill().map((_, i)=>([
(i % cmax) + 1,
floor(i / cmax),
]))
.forEach(card => draw_card(card, x += cf, y))
let gc = dl+cs+1
x = pad_h - g
suits.forEach((suit, suit_i) => {
x = pad_h - gc
y += gy
Array(cmax).fill().forEach((_, val_i)=>{
draw_card([val_i + 1, suit_i], x += gc, y)
})
})
x = pad_h, y += gy
draw_card([0], x, y)
}
setTheme('Dark')
resize()
</script>