-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
624 lines (548 loc) · 17.7 KB
/
Copy pathscript.js
File metadata and controls
624 lines (548 loc) · 17.7 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
const canvas = document.querySelector("#wheelCanvas");
const ctx = canvas.getContext("2d");
const spinBtn = document.querySelector("#spinBtn");
const quickBtn = document.querySelector("#quickBtn");
const boxBtn = document.querySelector("#boxBtn");
const itemList = document.querySelector("#itemList");
const templateList = document.querySelector("#templateList");
const historyList = document.querySelector("#historyList");
const itemCount = document.querySelector("#itemCount");
const previewCount = document.querySelector("#previewCount");
const previewChips = document.querySelector("#previewChips");
const setNameInput = document.querySelector("#setNameInput");
const noRepeatToggle = document.querySelector("#noRepeatToggle");
const resultPanel = document.querySelector("#resultPanel");
const resultText = document.querySelector("#resultText");
const resultMeta = document.querySelector("#resultMeta");
const toast = document.querySelector("#toast");
const boxGrid = document.querySelector("#boxGrid");
const TAU = Math.PI * 2;
const STORAGE_KEY = "pickjoy-state-v2";
const palette = ["#ff6048", "#159a8b", "#4d73d9", "#f0b631", "#9a61d6", "#5b9e3d", "#ef7d45", "#2d9db0", "#d94f7b"];
const templates = [
{
name: "吃什么",
title: "今天吃什么",
items: ["火锅", "烧烤", "寿司", "面条", "汉堡", "米饭套餐", "轻食", "随便走进一家"]
},
{
name: "去哪",
title: "周末去哪",
items: ["看电影", "逛公园", "找家咖啡店", "城市漫步", "在家做饭", "去书店", "拍照"]
},
{
name: "谁来",
title: "谁先来",
items: ["我来", "你来", "左边的人", "右边的人", "年龄最小", "年龄最大"]
},
{
name: "奖励",
title: "今日奖励",
items: ["奶茶", "电影", "打游戏 1 小时", "早点睡", "买个小东西", "散步"]
},
{
name: "惩罚",
title: "轻量惩罚",
items: ["做 10 个深蹲", "讲一个冷笑话", "发一句夸夸", "收拾桌面", "真心话", "大冒险"]
},
{
name: "学习",
title: "学习任务抽取",
items: ["背 20 个单词", "刷 10 道题", "复盘笔记", "读 15 分钟", "写一段总结", "休息 5 分钟"]
}
];
let state = {
title: "今天吃什么",
mode: "wheel",
noRepeat: false,
rotation: 0,
history: [],
removedThisRound: [],
items: templates[0].items.map((text) => ({ text, weight: 1 }))
};
let isPicking = false;
function normalizeAngle(angle) {
return ((angle % TAU) + TAU) % TAU;
}
function cleanItems() {
return state.items
.map((item) => ({ text: String(item.text || "").trim(), weight: Math.max(1, Number(item.weight || 1)) }))
.filter((item) => item.text);
}
function availableItems() {
const usable = cleanItems();
if (!state.noRepeat) return usable;
const filtered = usable.filter((item) => !state.removedThisRound.includes(item.text));
if (filtered.length >= 2) return filtered;
state.removedThisRound = [];
return usable;
}
function weightedPick(items) {
const total = items.reduce((sum, item) => sum + item.weight, 0);
let ticket = Math.random() * total;
for (let index = 0; index < items.length; index += 1) {
ticket -= items[index].weight;
if (ticket <= 0) return index;
}
return items.length - 1;
}
function getSegments(items) {
const total = items.reduce((sum, item) => sum + item.weight, 0);
let angle = -Math.PI / 2;
return items.map((item) => {
const size = (item.weight / total) * TAU;
const segment = {
item,
start: angle,
end: angle + size,
center: angle + size / 2
};
angle += size;
return segment;
});
}
function drawWheel() {
const items = availableItems();
const size = canvas.width;
const center = size / 2;
const radius = center - 34;
ctx.clearRect(0, 0, size, size);
ctx.save();
ctx.translate(center, center);
ctx.rotate(state.rotation);
if (items.length < 2) {
ctx.beginPath();
ctx.arc(0, 0, radius, 0, TAU);
ctx.fillStyle = "#fff5df";
ctx.fill();
ctx.lineWidth = 14;
ctx.strokeStyle = "#1f2024";
ctx.stroke();
ctx.restore();
ctx.fillStyle = "#746b5f";
ctx.font = "800 36px Inter, Noto Sans SC, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("至少两个选项", center, center);
return;
}
const segments = getSegments(items);
segments.forEach((segment, index) => {
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.arc(0, 0, radius, segment.start, segment.end);
ctx.closePath();
ctx.fillStyle = palette[index % palette.length];
ctx.fill();
ctx.lineWidth = 8;
ctx.strokeStyle = "#fffdf8";
ctx.stroke();
});
ctx.beginPath();
ctx.arc(0, 0, radius, 0, TAU);
ctx.lineWidth = 14;
ctx.strokeStyle = "#1f2024";
ctx.stroke();
ctx.restore();
// Draw labels after the wheel so every label remains horizontal and readable.
segments.forEach((segment) => {
const angle = segment.center + state.rotation;
const x = center + Math.cos(angle) * radius * 0.6;
const y = center + Math.sin(angle) * radius * 0.6;
const label = segment.item.text.length > 8 ? `${segment.item.text.slice(0, 8)}...` : segment.item.text;
ctx.save();
ctx.translate(x, y);
ctx.fillStyle = "rgba(31, 32, 36, 0.18)";
roundedRect(ctx, -86, -22, 172, 44, 22);
ctx.fill();
ctx.fillStyle = "#fffdf8";
ctx.font = "900 27px Inter, Noto Sans SC, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(label, 0, 1, 150);
ctx.restore();
});
}
function roundedRect(context, x, y, width, height, radius) {
context.beginPath();
context.moveTo(x + radius, y);
context.arcTo(x + width, y, x + width, y + height, radius);
context.arcTo(x + width, y + height, x, y + height, radius);
context.arcTo(x, y + height, x, y, radius);
context.arcTo(x, y, x + width, y, radius);
context.closePath();
}
function saveState() {
localStorage.setItem(STORAGE_KEY, JSON.stringify({
title: state.title,
items: state.items,
noRepeat: state.noRepeat,
history: state.history.slice(0, 20)
}));
}
function loadState() {
const params = new URLSearchParams(location.search);
const shared = params.get("set");
if (shared) {
try {
const decoded = JSON.parse(decodeURIComponent(atob(shared)));
if (Array.isArray(decoded.items) && decoded.items.length >= 2) {
state.title = decoded.title || state.title;
state.items = decoded.items.map((item) => ({
text: String(item.text || "").slice(0, 28),
weight: Math.max(1, Number(item.weight || 1))
}));
return;
}
} catch {
showToast("分享链接读取失败,已打开默认模板");
}
}
try {
const saved = JSON.parse(localStorage.getItem(STORAGE_KEY));
if (!saved) return;
state = {
...state,
title: saved.title || state.title,
noRepeat: Boolean(saved.noRepeat),
history: Array.isArray(saved.history) ? saved.history.slice(0, 20) : [],
items: Array.isArray(saved.items) && saved.items.length >= 2 ? saved.items : state.items
};
} catch {
localStorage.removeItem(STORAGE_KEY);
}
}
function renderTemplates() {
templateList.innerHTML = "";
templates.forEach((template) => {
const button = document.createElement("button");
button.className = "template-btn";
button.type = "button";
button.textContent = template.name;
button.addEventListener("click", () => applyTemplate(template));
templateList.appendChild(button);
});
}
function renderCounts() {
const count = cleanItems().length;
itemCount.textContent = `${count} 个选项`;
previewCount.textContent = `${availableItems().length} 个`;
}
function renderItems() {
renderCounts();
setNameInput.value = state.title;
noRepeatToggle.checked = state.noRepeat;
itemList.innerHTML = "";
state.items.forEach((item, index) => {
const row = document.createElement("div");
row.className = "item-row";
row.innerHTML = `
<input type="text" value="${escapeHtml(item.text)}" aria-label="选项 ${index + 1}">
<input type="number" value="${Number(item.weight || 1)}" min="1" max="99" aria-label="权重 ${index + 1}">
<button class="delete-btn" type="button" aria-label="删除选项 ${index + 1}">×</button>
`;
const [textInput, weightInput] = row.querySelectorAll("input");
textInput.addEventListener("input", () => {
state.items[index].text = textInput.value.slice(0, 28);
state.removedThisRound = [];
saveState();
renderNonEditor();
});
weightInput.addEventListener("input", () => {
state.items[index].weight = Math.max(1, Math.min(99, Number(weightInput.value || 1)));
saveState();
renderNonEditor();
});
row.querySelector("button").addEventListener("click", () => deleteItem(index));
itemList.appendChild(row);
});
}
function renderPreview() {
previewChips.innerHTML = "";
availableItems().forEach((item) => {
const chip = document.createElement("span");
chip.className = "preview-chip";
chip.textContent = item.weight > 1 ? `${item.text} ×${item.weight}` : item.text;
previewChips.appendChild(chip);
});
}
function renderHistory() {
historyList.innerHTML = "";
if (!state.history.length) {
const empty = document.createElement("li");
empty.textContent = "还没有结果";
historyList.appendChild(empty);
return;
}
state.history.slice(0, 8).forEach((entry) => {
const item = document.createElement("li");
item.textContent = entry;
historyList.appendChild(item);
});
}
function renderBoxes(revealedWinner = "") {
const items = availableItems();
boxGrid.innerHTML = "";
items.forEach((item, index) => {
const card = document.createElement("div");
card.className = `mystery-card ${revealedWinner ? "" : "is-hidden"}`;
if (item.text === revealedWinner) card.classList.add("is-winner");
card.dataset.index = String(index);
card.innerHTML = `<span>盲盒 ${index + 1}</span><strong>${escapeHtml(item.text)}</strong>`;
boxGrid.appendChild(card);
});
}
function renderMode() {
document.querySelectorAll(".mode-btn").forEach((button) => {
button.classList.toggle("active", button.dataset.mode === state.mode);
});
document.querySelectorAll("[data-view]").forEach((view) => {
view.classList.toggle("hidden", view.dataset.view !== state.mode);
});
}
function renderNonEditor() {
renderCounts();
renderPreview();
renderBoxes();
drawWheel();
}
function renderDynamic() {
renderNonEditor();
}
function renderAll() {
renderMode();
renderItems();
renderPreview();
renderBoxes();
drawWheel();
renderHistory();
}
function applyTemplate(template) {
state.title = template.title;
state.items = template.items.map((text) => ({ text, weight: 1 }));
state.history = [];
state.removedThisRound = [];
state.rotation = 0;
setResult("还没抽", `${template.title} 已载入。`, true);
showToast(`已切换到「${template.title}」`);
saveState();
renderAll();
}
function deleteItem(index) {
if (state.items.length <= 2) {
showToast("至少保留两个选项");
return;
}
state.items.splice(index, 1);
state.removedThisRound = [];
saveState();
renderAll();
}
function addItem() {
state.items.push({ text: `新选项 ${state.items.length + 1}`, weight: 1 });
state.removedThisRound = [];
saveState();
renderAll();
itemList.lastElementChild?.querySelector("input")?.focus();
}
function spinWheel() {
const items = availableItems();
if (isPicking || items.length < 2) {
showToast("至少需要两个可用选项");
return;
}
isPicking = true;
spinBtn.disabled = true;
quickBtn.disabled = true;
setResult("转动中...", "等它停下来,不会马上弹窗。", true);
const winnerIndex = weightedPick(items);
const segment = getSegments(items)[winnerIndex];
const desiredRotation = normalizeAngle(-Math.PI / 2 - segment.center);
const currentNorm = normalizeAngle(state.rotation);
const delta = normalizeAngle(desiredRotation - currentNorm);
const start = state.rotation;
const end = state.rotation + delta + TAU * (6 + Math.floor(Math.random() * 3));
const duration = 4200;
const startedAt = performance.now();
function tick(now) {
const progress = Math.min(1, (now - startedAt) / duration);
const eased = 1 - Math.pow(1 - progress, 4.2);
state.rotation = start + (end - start) * eased;
drawWheel();
if (progress < 1) {
requestAnimationFrame(tick);
return;
}
state.rotation = normalizeAngle(end);
isPicking = false;
spinBtn.disabled = false;
quickBtn.disabled = false;
drawWheel();
finishPick(items[winnerIndex].text, "转盘");
}
requestAnimationFrame(tick);
}
function quickPick() {
const items = availableItems();
if (isPicking || items.length < 2) {
showToast("至少需要两个可用选项");
return;
}
isPicking = true;
spinBtn.disabled = true;
quickBtn.disabled = true;
const winner = items[weightedPick(items)].text;
let frame = 0;
const totalFrames = 26;
const timer = setInterval(() => {
const item = items[frame % items.length];
setResult(item.text, "快抽滚动中...", true);
frame += 1;
if (frame >= totalFrames) {
clearInterval(timer);
isPicking = false;
spinBtn.disabled = false;
quickBtn.disabled = false;
finishPick(winner, "快抽");
}
}, 70);
}
function boxPick() {
const items = availableItems();
if (isPicking || items.length < 2) {
showToast("至少需要两个可用选项");
return;
}
isPicking = true;
boxBtn.disabled = true;
renderBoxes();
const winnerIndex = weightedPick(items);
let frame = 0;
const totalFrames = 34;
const timer = setInterval(() => {
document.querySelectorAll(".mystery-card").forEach((card) => card.classList.remove("is-active"));
const active = document.querySelector(`.mystery-card[data-index="${frame % items.length}"]`);
active?.classList.add("is-active");
frame += 1;
if (frame >= totalFrames) {
clearInterval(timer);
isPicking = false;
boxBtn.disabled = false;
renderBoxes(items[winnerIndex].text);
finishPick(items[winnerIndex].text, "盲盒");
}
}, 76);
}
function finishPick(value, source) {
state.history.unshift(value);
state.history = state.history.slice(0, 20);
if (state.noRepeat && !state.removedThisRound.includes(value)) {
state.removedThisRound.push(value);
}
setResult(value, `${state.title} · ${source}抽取`, false);
saveState();
renderHistory();
renderPreview();
drawWheel();
playPop();
}
function setResult(value, meta, empty) {
resultText.textContent = value;
resultMeta.textContent = meta;
resultPanel.classList.toggle("empty", Boolean(empty));
resultPanel.classList.remove("flash");
void resultPanel.offsetWidth;
resultPanel.classList.add("flash");
}
async function copyText(text) {
try {
await navigator.clipboard.writeText(text);
showToast("已复制");
} catch {
showToast("复制失败,可以手动选中复制");
}
}
function shareCurrentSet() {
const payload = {
title: state.title,
items: cleanItems().map((item) => ({
text: item.text,
weight: item.weight
}))
};
const encoded = btoa(encodeURIComponent(JSON.stringify(payload)));
const url = `${location.origin}${location.pathname}?set=${encoded}`;
copyText(url);
}
function resetAll() {
applyTemplate(templates[0]);
showToast("已重置");
}
function surpriseTemplate() {
const next = templates[Math.floor(Math.random() * templates.length)];
applyTemplate(next);
}
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(showToast.timer);
showToast.timer = setTimeout(() => toast.classList.remove("show"), 1700);
}
function escapeHtml(value) {
return String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """);
}
function playPop() {
try {
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audio = new AudioContext();
const oscillator = audio.createOscillator();
const gain = audio.createGain();
oscillator.type = "triangle";
oscillator.frequency.setValueAtTime(360, audio.currentTime);
oscillator.frequency.exponentialRampToValueAtTime(760, audio.currentTime + 0.16);
gain.gain.setValueAtTime(0.0001, audio.currentTime);
gain.gain.exponentialRampToValueAtTime(0.13, audio.currentTime + 0.02);
gain.gain.exponentialRampToValueAtTime(0.0001, audio.currentTime + 0.2);
oscillator.connect(gain);
gain.connect(audio.destination);
oscillator.start();
oscillator.stop(audio.currentTime + 0.22);
} catch {
// Sound is optional.
}
}
document.querySelectorAll(".mode-btn").forEach((button) => {
button.addEventListener("click", () => {
state.mode = button.dataset.mode;
renderAll();
});
});
setNameInput.addEventListener("input", () => {
state.title = setNameInput.value.slice(0, 24) || "未命名抽奖盘";
saveState();
});
noRepeatToggle.addEventListener("change", () => {
state.noRepeat = noRepeatToggle.checked;
state.removedThisRound = [];
saveState();
renderNonEditor();
});
document.querySelector("#addItemBtn").addEventListener("click", addItem);
document.querySelector("#resetBtn").addEventListener("click", resetAll);
document.querySelector("#surpriseBtn").addEventListener("click", surpriseTemplate);
document.querySelector("#shareBtn").addEventListener("click", shareCurrentSet);
document.querySelector("#clearHistoryBtn").addEventListener("click", () => {
state.history = [];
saveState();
renderHistory();
});
spinBtn.addEventListener("click", spinWheel);
quickBtn.addEventListener("click", quickPick);
boxBtn.addEventListener("click", boxPick);
loadState();
renderTemplates();
renderAll();