-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse_canvas_script.js
executable file
·416 lines (357 loc) · 11 KB
/
use_canvas_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
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
411
412
413
414
415
416
const MAX_POPULATION = 11;
const PC_FLAG = !navigator.userAgent.match(/(iPhone|iPod|iPad|Android.*Mobile)/i);
const MIN_SCALE = 8;
let scale = 10;
// 机の位置と大きさ
let coo_siz = [[0]];
// テキストの位置
let txt_coo = [[0]];
let attend = [true, true, true, true, true, true, true, false, false, false, true];
let population = 0;
// 順番を格納する配列
let orders_num = [0];
let pointed = -1;
// グローバルのイテレータ変数
let g_itr = 0;
let running = false;
let manual_seed_flag = false;
// シード付き乱数生成
// 参考:https://sbfl.net/blog/2017/06/01/javascript-reproducible-random/
class Random {
constructor(seed = 88675123) {
this.x = 123456789;
this.y = 362436069;
this.z = 521288629;
this.w = seed;
}
// XorShift
// 乱数生成アルゴリズム
next() {
const t = this.x ^ (this.x << 11);
this.x = this.y;
this.y = this.z;
this.z = this.w;
return this.w = (this.w ^ (this.w >>> 19)) ^ (t ^ (t >>> 8));
}
// 範囲内の整数乱数を生成
// minは含むがmaxは含まない
nextInt(min, max) {
return min + (Math.abs(this.next()) % (max - min));
}
}
// スケールの変更
const changeScale = () => {
// 机の位置と大きさ
// 位置は長方形の左上の座標
coo_siz = [
[0, 0, scale * 3, scale * 5],
[0, scale * 6, scale * 3, scale * 5],
[0, scale * 12, scale * 3, scale * 5],
[scale * 4, scale * 16, scale * 5, scale * 3],
[scale * 10, scale * 16, scale * 5, scale * 3],
[scale * 16, scale * 6, scale * 3, scale * 5],
[scale * 16, 0, scale * 3, scale * 5],
[scale * 6.4, scale * 1.8, scale * 3, scale * 5],
[scale * 6.4, scale * 7, scale * 3, scale * 5],
[scale * 9.6, scale * 1.8, scale * 3, scale * 5],
[scale * 9.6, scale * 7, scale * 3, scale * 5]
];
txt_coo = [];
// 長方形の右下の座標を計算し, 配列に追加
for (let i of coo_siz) {
i.push(i[0] + i[2], i[1] + i[3]);
txt_coo.push([i[0] + i[2] / 2, i[1] + i[3] * 0.5 + scale * 0.5]);
}
// フォント指定
ctx.font = String(scale * 1.6) + "px serif";
// 文字列は中央揃え
ctx.textAlign = "center";
}
// 背景描画
const drawBackGround = () => {
// 背景の描画
ctx.fillStyle = "rgba(65, 105, 225, 1)";
ctx.fillRect(0, 0, scale * 19, scale * 19);
// 机を色分けして描画
for (let i = 0; i < MAX_POPULATION; i++) {
if (attend[i]) {
ctx.fillStyle = "rgb(200, 200, 0)";
} else {
ctx.fillStyle = "rgb(100, 100, 0)";
}
ctx.fillRect(coo_siz[i][0], coo_siz[i][1], coo_siz[i][2], coo_siz[i][3]);
}
}
// 順番の描画開始
const startDrawOrders = () => {
drawBackGround();
// 文字色の設定
ctx.fillStyle = "rgb(200, 0, 0)";
// イテレータ変数初期化
g_itr = 0;
setTimeout(drawOrders, 200);
}
// 順番を描画する (時間差あり)
const drawOrders = () => {
const coo = txt_coo[orders_num[g_itr++]];
ctx.fillText(g_itr, coo[0], coo[1]);
if (g_itr < population) {
setTimeout(drawOrders, 200);
} else {
running = false;
setDisabledAll(false);
}
}
// 順番描画 (即時)
const drawOrdersImm = () => {
// 文字色
ctx.fillStyle = "rgb(200, 0, 0)";
let x, y;
for (let i = 0; i < population; i++) {
[x, y] = txt_coo[orders_num[i]];
ctx.fillText(i + 1, x, y);
}
}
// 指定した机とその番号を上書き
const drawTableAndOrder = (table_num, io) => {
// in
if (io && PC_FLAG) {
if (attend[table_num]) {
ctx.fillStyle = "rgb(255, 100, 100)";
} else {
ctx.fillStyle = "rgb(200, 0, 0)";
}
}
// out
else {
if (attend[table_num]) {
ctx.fillStyle = "rgb(200, 200, 0)";
} else {
ctx.fillStyle = "rgb(100, 100, 0)";
}
}
ctx.fillRect(coo_siz[table_num][0], coo_siz[table_num][1], coo_siz[table_num][2], coo_siz[table_num][3]);
if (orders_num.includes(table_num) && population) {
const coo = txt_coo[table_num];
ctx.fillStyle = "rgb(200, 0, 0)";
ctx.fillText(orders_num.indexOf(table_num) + 1, coo[0], coo[1]);
}
}
// 出席者の取得
const getAttendees = () => {
let attendees = [];
for (i = 0; i < MAX_POPULATION; i++) {
if (attend[i]) {
attendees.push(i);
}
}
return attendees;
}
// シャッフルボタン押下時に実行する関数
const onShuffleClick = () => {
running = true;
setDisabledAll(true);
let i, r;
// シードの自動設定 (時刻)
if (!manual_seed_flag) {
const t = (new Date).getTime();
seed_value.value = (t / 13 | 0) & 0x7fffffff;
}
// 実数や空文字は整数に変換
seed_value.value = parseInt(Number(seed_value.value), 10);
const random = new Random(seed_value.value);
// 人数のカウントと出席者の取得
const attendees = getAttendees();
population = attendees.length;
// ランダムに順番を決める
orders_num = [];
for (i = population; i > 0; i--) {
r = random.nextInt(0, i);
orders_num = orders_num.concat(attendees.splice(r, 1));
}
startDrawOrders();
}
// 順番に関する変数を初期化
const initOrder = () => {
population = 0;
orders_num = [];
}
// 一括切り替え
const setDisabledAll = b => {
shuffle_button.disabled = b;
manual_seed.disabled = b;
all_attend_button.disabled = b;
all_absent_button.disabled = b;
b4_button.disabled = b;
// シードの入力欄は例外
if (manual_seed_flag) {
seed_value.disabled = b;
}
}
// 全員欠席なら真を返す
const noPresenter = () => {
for (const flag of attend) {
if (flag) {
return false;
}
}
return true;
}
// canvasのリサイズ
const onWindowResize = () => {
// シャッフル中は動作しない
if (running) {
return;
}
// 幅と高さから基準を計算
const bw = canvas_parent.clientWidth;
const bh = document.documentElement.clientHeight - above_canvas.offsetHeight - below_canvas.offsetHeight - 20;
scale = (bw < bh ? bw : bh) / 19;
scale = MIN_SCALE < scale ? scale : MIN_SCALE;
canvas.height = canvas.width = scale * 19;
changeScale();
drawBackGround();
drawOrdersImm();
}
// ポインタの位置から机を計算
const calcPointed = e => {
const x = e.offsetX;
const y = e.offsetY;
let x0, y0, x1, y1;
// まずは直前の座標を確認
if (pointed >= 0) {
[x0, y0, , , x1, y1] = coo_siz[pointed];
// ポインタの位置が変化していない
if (x0 <= x && x < x1 && y0 <= y && y < y1) {
return;
}
}
pointed = -1;
for (let i = 0; i < MAX_POPULATION; i++) {
[x0, y0, , , x1, y1] = coo_siz[i];
if (x0 <= x && x < x1 && y0 <= y && y < y1) {
pointed = i;
return;
}
}
}
// シード手入力ボタンのクリック時動作
const onManualSeedClick = () => {
manual_seed_flag = manual_seed.classList.contains('active');
seed_value.disabled = !manual_seed_flag;
}
// 差集合の計算
const difSet = (A, B) => {
return new Set([...A].filter(e => (!B.has(e))))
}
const above_canvas = document.getElementById("above_canvas");
const below_canvas = document.getElementById("below_canvas");
const canvas_parent = document.getElementById("canvas_parent");
const canvas = document.getElementById("canvas");
const shuffle_button = document.getElementById("shuffle_button");
const manual_seed = document.getElementById("manual_seed");
const seed_value = document.getElementById("seed_value");
const all_attend_button = document.getElementById("all_attend_button");
const all_absent_button = document.getElementById("all_absent_button");
const b4_button = document.getElementById("b4_button");
// シャッフルボタンのクリック時動作
shuffle_button.addEventListener("click", onShuffleClick);
// シード入力欄でEnterキーを押したとき, シャッフルを実行
seed_value.addEventListener("keyup", e => {
if (e.key == "Enter") {
// シャッフルボタンが無効の場合は実行しない
if (!shuffle_button.disabled) {
onShuffleClick();
}
}
});
// シード手入力ボタンのクリック時動作
manual_seed.addEventListener("click", onManualSeedClick);
// canvasのなにか
let ctx;
if (canvas.getContext) {
ctx = canvas.getContext("2d");
// ctx.globalCompositeOperation = "source-in";
ctx.globalCompositeOperation = "source-over";
canvas.addEventListener("mousemove", e => {
if (running) {
return;
}
const prev_pointed = pointed;
calcPointed(e);
if (prev_pointed == pointed) {
return;
} else if (prev_pointed >= 0) {
drawTableAndOrder(prev_pointed, false);
}
if (pointed >= 0) {
drawTableAndOrder(pointed, true);
}
});
canvas.addEventListener("mouseout", () => {
if (pointed == -1) {
return;
}
drawTableAndOrder(pointed, false);
pointed = -1;
});
canvas.addEventListener("click", e => {
calcPointed(e);
if (running || pointed < 0) {
return;
}
if (!population) {
attend[pointed] = !attend[pointed];
drawTableAndOrder(pointed, true);
} else if (confirm("順番をリセットしますか?")) {
attend[pointed] = !attend[pointed];
initOrder();
drawBackGround();
}
shuffle_button.disabled = noPresenter();
});
// リサイズ時の動作を指定
window.addEventListener("resize", onWindowResize);
// canvasサイズの初期化
onWindowResize();
}
// 全選択, 全解除ともに同じ関数を呼び出す
const setAttendAll = b => {
if (population && !confirm("順番をリセットしますか?")) {
return;
}
attend = attend.map(() => b);
initOrder();
drawBackGround();
}
// 全選択
all_attend_button.addEventListener("click", () => {
if (population == MAX_POPULATION) {
return;
}
setAttendAll(true);
shuffle_button.disabled = false;
});
// 全解除
all_absent_button.addEventListener("click", () => {
setAttendAll(false);
shuffle_button.disabled = true;
});
// B4のみ
b4_button.addEventListener("click", () => {
const b4_indices = new Set([3, 4, 5, 10]);
const attendees = new Set(getAttendees());
if (difSet(b4_indices, attendees).size === 0 && difSet(attendees, b4_indices).size === 0) {
return;
}
if (population && !confirm("順番をリセットしますか?")) {
return;
}
for (let i = 0; i < MAX_POPULATION; i++) {
attend[i] = b4_indices.has(i);
}
initOrder();
drawBackGround();
shuffle_button.disabled = false;
});
onManualSeedClick();