File size: 35,514 Bytes
244baf9 221f6d7 244baf9 | 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 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 | <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M5Stack LLM8850 画像生成展示</title>
<style>
:root {
--primary-color: #3498db;
--selected-color: #e74c3c;
--success-color: #27ae60;
--warning-color: #f39c12;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 20px;
background: #f0f3f5;
padding-bottom: 120px;
/* 为固定输入框预留空间 */
}
.container {
max-width: 100%;
margin: 0 auto;
background: white;
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
padding: 2rem;
position: relative;
}
/* 会社ロゴ */
.company-logo {
position: absolute;
top: 20px;
right: 20px;
width: 80px;
height: auto;
}
h1 {
text-align: center;
color: #2c3e50;
margin-bottom: 2rem;
font-weight: 600;
margin-right: 100px;
/* 为图标留出空间 */
}
/* 選択された項目のエリアスタイル */
#selected-terms-box {
position: sticky;
top: 0;
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(5px);
z-index: 10;
padding: 1rem;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
margin-bottom: 1.5rem;
border-radius: 8px;
}
.selected-terms-label {
font-weight: 500;
color: #2c3e50;
margin-bottom: 0.5rem;
font-size: 0.9rem;
}
.selected-term {
display: inline-flex;
align-items: center;
padding: 0.4rem 0.8rem;
background: var(--selected-color);
color: white;
border-radius: 20px;
margin: 0.3rem;
font-size: 0.9em;
animation: popIn 0.2s ease;
cursor: pointer;
transition: all 0.2s ease;
}
.selected-term:hover {
background: #c0392b;
transform: scale(1.05);
}
.controls {
display: inline-flex;
gap: 0.5rem;
margin-left: 1rem;
vertical-align: middle;
}
.btn {
padding: 0.6rem 1.2rem;
border: none;
border-radius: 20px;
font-size: 0.9rem;
cursor: pointer;
transition: all 0.2s ease;
font-weight: 500;
}
#generate-btn {
background: var(--primary-color);
color: white;
padding: 1rem 2rem;
/* 放大生成按钮 */
font-size: 1.1rem;
font-weight: 600;
}
#random-btn {
background: var(--success-color);
color: white;
}
#clear-terms-btn {
background: var(--warning-color);
color: white;
}
#clear-images-btn {
background: #95a5a6;
color: white;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
.btn:disabled {
background: #bdc3c7;
cursor: not-allowed;
transform: none;
box-shadow: none;
}
@keyframes popIn {
from {
transform: scale(0.8);
opacity: 0;
}
to {
transform: scale(1);
opacity: 1;
}
}
/* プログレスバーのスタイル(非ポップアップ式) */
.progress-container {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
padding: 1rem;
border-radius: 8px;
margin-bottom: 1rem;
color: white;
display: none;
animation: slideDown 0.3s ease;
}
.progress-container.active {
display: block;
}
.progress-bar {
width: 100%;
height: 6px;
background: rgba(255, 255, 255, 0.3);
border-radius: 3px;
overflow: hidden;
margin: 0.5rem 0;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #fff, #f39c12);
width: 0%;
transition: width 0.3s ease;
border-radius: 3px;
}
.progress-info {
display: flex;
justify-content: space-between;
align-items: center;
font-size: 0.9rem;
}
.progress-step {
opacity: 0.9;
}
.fun-fact {
background: rgba(255, 255, 255, 0.1);
padding: 0.5rem;
border-radius: 4px;
margin-top: 0.5rem;
font-style: italic;
font-size: 0.85rem;
animation: fadeIn 0.5s ease;
}
@keyframes slideDown {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* 項目分類のスタイル */
#term-categories {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 1.5rem;
}
.category {
background: #ffffff;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
overflow: hidden;
}
.category-header {
padding: 1rem;
background: var(--primary-color);
color: white;
font-weight: 500;
position: sticky;
top: 0;
z-index: 1;
}
.scroll-container {
max-height: 240px;
overflow-y: hidden;
padding: 1rem;
position: relative;
}
.term-item {
padding: 0.8rem 1.2rem;
margin: 0.5rem 0;
border-radius: 6px;
background: #f8f9fa;
cursor: pointer;
transition: all 0.2s ease;
position: relative;
overflow: hidden;
display: flex;
align-items: center;
min-height: 48px;
}
.term-content {
display: flex;
flex-direction: column;
gap: 0.2rem;
}
.japanese {
font-weight: 500;
color: #2c3e50;
}
.term {
font-size: 0.85rem;
color: #7f8c8d;
font-style: italic;
}
.term-item.selected {
background: var(--primary-color);
color: white;
}
.term-item.selected .term {
color: rgba(255, 255, 255, 0.8);
}
.term-item:hover {
transform: translateX(5px);
box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.1);
}
/* レイアウト調整 - 画面全体に広げる */
.main-content {
display: grid;
grid-template-columns: 1fr 500px;
gap: 2rem;
min-height: 70vh;
}
.left-column {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 1.2rem;
align-content: start;
}
.right-column {
background: #f8f9fa;
border-radius: 8px;
padding: 1rem;
display: flex;
flex-direction: column;
}
.images-header {
text-align: center;
color: #2c3e50;
margin-bottom: 1rem;
font-size: 1.1rem;
font-weight: 500;
flex-shrink: 0;
}
/* 画像コンテナを2列に変更 */
#images-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1rem;
flex: 1;
}
.image-item {
background: white;
border-radius: 8px;
padding: 0.5rem;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
animation: zoomIn 0.3s ease;
align-self: start;
}
.image-item img {
width: 100%;
height: auto;
border-radius: 6px;
display: block;
}
.image-terms {
font-size: 0.75rem;
color: #7f8c8d;
margin-top: 0.5rem;
padding: 0.5rem;
background: #ecf0f1;
border-radius: 4px;
word-wrap: break-word;
line-height: 1.3;
}
.empty-state {
grid-column: 1 / -1;
text-align: center;
color: #7f8c8d;
padding: 2rem;
font-style: italic;
}
@keyframes zoomIn {
from {
opacity: 0;
transform: scale(0.9);
}
to {
opacity: 1;
transform: scale(1);
}
}
/* レスポンシブデザイン */
@media (max-width: 1400px) {
.main-content {
grid-template-columns: 1fr 450px;
}
}
@media (max-width: 1200px) {
.main-content {
grid-template-columns: 1fr;
}
}
@media (max-width: 768px) {
.container {
padding: 1rem;
}
.company-logo {
width: 60px;
top: 10px;
right: 10px;
}
h1 {
margin-right: 70px;
font-size: 1.5rem;
}
#term-categories {
grid-template-columns: 1fr;
}
#images-container {
grid-template-columns: 1fr;
}
.controls {
flex-direction: column;
gap: 0.5rem;
margin-left: 0;
margin-top: 1rem;
}
.btn {
padding: 0.8rem 1.5rem;
}
#generate-btn {
padding: 1rem 1.5rem;
}
}
/* 画面全体に広げる調整 */
@media (min-width: 1600px) {
.container {
max-width: 95%;
}
.main-content {
grid-template-columns: 1fr 600px;
}
.left-column {
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}
}
/* 手动输入区域样式 */
.manual-input-container {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background: #ffffff;
box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
border-top: 1px solid #e1e8ed;
z-index: 100;
padding: 1rem 2rem;
margin: 0;
}
.input-header {
font-weight: 500;
color: #2c3e50;
margin-bottom: 1rem;
font-size: 1rem;
}
.input-wrapper {
display: flex;
gap: 1rem;
margin-bottom: 0.5rem;
}
#manual-term-input {
flex: 1;
padding: 0.8rem 1.2rem;
border: 1px solid #ddd;
border-radius: 20px;
font-size: 0.9rem;
transition: all 0.2s ease;
}
#manual-term-input:focus {
outline: none;
border-color: var(--primary-color);
box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}
#add-term-btn {
padding: 0.8rem 1.5rem;
white-space: nowrap;
}
.input-note {
font-size: 0.8rem;
color: #7f8c8d;
font-style: italic;
}
/* 响应式设计 */
@media (max-width: 768px) {
.input-wrapper {
flex-direction: column;
}
#add-term-btn {
align-self: flex-start;
}
}
</style>
</head>
<body>
<div class="container">
<!-- 公司图标 -->
<img src="https://docs.m5stack.com/assets/m5logo2022.svg" alt="M5Stack Logo" class="company-logo">
<h1>M5Stack LLM8850 画像生成展示</h1>
<div id="selected-terms-box">
<div class="selected-terms-label">選択されたプロンプト Selected Prompts:</div>
<div id="selected-terms-list"></div>
<div class="controls">
<button id="generate-btn" class="btn">今すぐ生成 Generate Now</button>
<button id="random-btn" class="btn">ランダム記述 Random Terms</button>
<button id="clear-terms-btn" class="btn">プロンプトをクリア Clear Terms</button>
<button id="clear-images-btn" class="btn">画像をクリア Clear Images</button>
</div>
</div>
<!-- 非弹出式进度条 -->
<div class="progress-container" id="progress-container">
<div class="progress-info">
<span>AI が創作中... AI is creating...</span>
<span id="progress-percent">0%</span>
</div>
<div class="progress-bar">
<div class="progress-fill" id="progress-fill"></div>
</div>
<div class="progress-step" id="progress-step">初期化中... Initializing...</div>
<div class="fun-fact" id="fun-fact"></div>
</div>
<div class="main-content">
<div class="left-column">
<div id="term-categories"></div>
</div>
<div class="right-column">
<div class="images-header">生成履歴 Generation History</div>
<div id="images-container">
<div class="empty-state">
キーワードを選択し、生成ボタンをクリックして創作を開始してください<br>
Select terms and click generate to start creating
</div>
</div>
</div>
</div>
<!-- 手动输入提示词区域 -->
<div class="manual-input-container">
<div class="input-header">手動入力 Manual Input:</div>
<div class="input-wrapper">
<input type="text" id="manual-term-input"
placeholder="英語の形容詞を入力してください (例: beautiful, dark, fantasy) - Please enter an English adjective">
<button id="add-term-btn" class="btn">追加 Add</button>
</div>
<div class="input-note">※ 単一の英語形容詞を入力してください - Please enter a single English adjective</div>
</div>
</div>
<script>
let scrollControllers = new Map();
// 有趣的提示词汇
const funFacts = [
"あなたの創造的なアイデアを分析中... Analyzing your creative ideas...",
"AIブレインが最適な構図を考え中... AI brain thinking about optimal composition...",
"パレット準備中、完璧な色を探しています... Preparing palette, finding perfect colors...",
"ピクセルスプライトが組み合わせ待ち列に並んでいます... Pixel sprites lining up for combination...",
"魔法で文字を視覚に変換中... Magic transforming text to visuals...",
"クリエイティブファクトリー全速力運転中... Creative factory at full speed...",
"各ディテールを精巧に彫刻中... Every detail carefully crafted...",
"ライトマスターが明暗を調整中... Light master adjusting brightness...",
"ブラシがキャンバス上で舞っています... Brushes dancing on canvas...",
"芸術的インスピレーションが形になりつつあります... Artistic inspiration taking shape...",
"創造性エンジン全開... Creativity engine at full power...",
"美学アルゴリズムがディテールを最適化中... Aesthetic algorithm optimizing details...",
"ビジュアルシンフォニーが演奏開始予定... Visual symphony about to play...",
"想像力が境界を突破中... Imagination breaking boundaries...",
"ミラクルワークショップが魔法をかけ中... Miracle workshop casting magic..."
];
// UNet去噪步骤提示
const unetSteps = [
"入力レイヤーの前処理中... Preprocessing input layers...",
"第1層CNN処理中... Layer 1 CNN processing...",
"第2層特徴抽出中... Layer 2 feature extraction...",
"第3層セマンティック理解... Layer 3 semantic understanding...",
"第4層ディテール最適化... Layer 4 detail optimization...",
"第5層カラー調和... Layer 5 color harmony...",
"第6層テクスチャ生成... Layer 6 texture generation...",
"第7層構図調整... Layer 7 composition adjustment...",
"第8層ライティング処理... Layer 8 lighting processing...",
"第9層ディテール強化... Layer 9 detail enhancement...",
"第10層品質チェック... Layer 10 quality check...",
"第11層スタイル融合... Layer 11 style fusion...",
"第12層最終カラー調整... Layer 12 final coloring...",
"第13層シャープ化処理... Layer 13 sharpening...",
"第14層ノイズ除去... Layer 14 noise removal...",
"第15層コントラスト最適化... Layer 15 contrast optimization...",
"第16層彩度調整... Layer 16 saturation adjustment...",
"第17層最終レンダリング... Layer 17 final rendering...",
"第18層品質検証... Layer 18 quality validation...",
"第19層出力準備... Layer 19 output preparation...",
"第20層処理完了... Layer 20 completion..."
];
async function loadAndDisplayTerms() {
try {
const response = await fetch('./person_jp.json');
const data = await response.json();
const proprietaryTerms = data.prompt_terms.filter(term => term.type === "专用");
const categories = proprietaryTerms.reduce((acc, term) => {
const subtype = term.subtype || "通用 General";
acc[subtype] = acc[subtype] || [];
acc[subtype].push(term);
return acc;
}, {});
const container = document.getElementById('term-categories');
Object.entries(categories).forEach(([subtype, terms]) => {
const category = document.createElement('div');
category.className = 'category';
const header = document.createElement('div');
header.className = 'category-header';
header.textContent = subtype;
const scrollContainer = document.createElement('div');
scrollContainer.className = 'scroll-container';
terms.forEach(term => {
const item = document.createElement('div');
item.className = 'term-item';
item.dataset.term = term.term;
item.innerHTML = `
<div class="term-content">
<div class="japanese">${term.japanese}</div>
<div class="term">${term.term}</div>
</div>
`;
scrollContainer.appendChild(item);
});
category.append(header, scrollContainer);
container.appendChild(category);
if (scrollContainer.scrollHeight > scrollContainer.clientHeight) {
setupAutoScroll(scrollContainer);
}
});
} catch (error) {
console.error('データ読み込み失敗 Data loading failed:', error);
}
}
function setupAutoScroll(container) {
let isPaused = false;
let scrollPos = 0;
const scrollSpeed = 0.5;
const scrollHeight = container.scrollHeight - container.clientHeight;
container.addEventListener('mouseenter', () => isPaused = true);
container.addEventListener('mouseleave', () => isPaused = false);
const scroll = () => {
if (!isPaused && scrollHeight > 0) {
scrollPos = (scrollPos + scrollSpeed) % scrollHeight;
const easedPos = scrollPos < scrollHeight / 2 ? scrollPos : scrollHeight - scrollPos;
container.scrollTop = easedPos;
}
requestAnimationFrame(scroll);
};
scroll();
}
function updateSelections() {
const selectedItems = Array.from(document.querySelectorAll('.term-item.selected'));
const container = document.getElementById('selected-terms-list');
// 保存手动添加的提示词
const manualTerms = [];
const existingSelectedTerms = container.querySelectorAll('.selected-term');
existingSelectedTerms.forEach(termElement => {
const term = termElement.dataset.term;
// 检查是否是手动添加的(没有对应的.term-item)
const hasTermItem = document.querySelector(`.term-item[data-term="${term}"]`);
if (!hasTermItem) {
manualTerms.push({
term: term,
element: termElement.cloneNode(true)
});
}
});
// 重新生成选中提示词列表
container.innerHTML = selectedItems.map(item => {
const japanese = item.querySelector('.japanese').textContent;
const english = item.querySelector('.term').textContent;
return `<span class="selected-term" data-term="${english}">${japanese} ${english}</span>`;
}).join('');
// 重新添加手动添加的提示词
manualTerms.forEach(manualTerm => {
container.appendChild(manualTerm.element);
});
}
function showProgress() {
const container = document.getElementById('progress-container');
const progressFill = document.getElementById('progress-fill');
const progressStep = document.getElementById('progress-step');
const progressPercent = document.getElementById('progress-percent');
const funFact = document.getElementById('fun-fact');
container.classList.add('active');
let currentStep = 0;
const totalSteps = 20;
const stepDuration = 240; //进度条刷新时间
return new Promise((resolve) => {
const interval = setInterval(() => {
currentStep++;
const progress = (currentStep / totalSteps) * 100;
progressFill.style.width = progress + '%';
progressPercent.textContent = Math.round(progress) + '%';
progressStep.textContent = unetSteps[currentStep - 1] || '処理中... Processing...';
if (currentStep % 3 === 0) {
const randomFact = funFacts[Math.floor(Math.random() * funFacts.length)];
funFact.textContent = randomFact;
}
if (currentStep >= totalSteps) {
clearInterval(interval);
progressStep.textContent = 'まもなく完了... Almost done...';
setTimeout(() => {
container.classList.remove('active');
resolve();
}, 500);
}
}, stepDuration);
});
}
function addImageToHistory(selectedTerms) {
const imagesContainer = document.getElementById('images-container');
// 清空初始提示
const emptyState = imagesContainer.querySelector('.empty-state');
if (emptyState) {
emptyState.remove();
}
const imgContainer = document.createElement('div');
imgContainer.className = 'image-item';
const img = document.createElement('img');
img.src = 'generated_image.png?' + new Date().getTime();
img.alt = 'Generated Image';
const termsDiv = document.createElement('div');
termsDiv.className = 'image-terms';
termsDiv.textContent = '关键词 Keywords: ' + selectedTerms.join(', ');
imgContainer.appendChild(img);
imgContainer.appendChild(termsDiv);
// 添加到开头
imagesContainer.prepend(imgContainer);
// 限制历史记录数量
while (imagesContainer.children.length > 20) {
imagesContainer.removeChild(imagesContainer.lastChild);
}
}
function clearAllTerms() {
// 清除所有选中状态
document.querySelectorAll('.term-item.selected').forEach(item => {
item.classList.remove('selected');
});
// 清除手动添加的词条
const container = document.getElementById('selected-terms-list');
const manualTerms = container.querySelectorAll('.selected-term');
manualTerms.forEach(term => {
// 检查是否是手动添加的(没有对应的.term-item)
const termValue = term.dataset.term;
const hasTermItem = document.querySelector(`.term-item[data-term="${termValue}"]`);
if (!hasTermItem) {
term.remove();
}
});
updateSelections();
}
function clearAllImages() {
const imagesContainer = document.getElementById('images-container');
imagesContainer.innerHTML = '<div class="empty-state">項目を選択して生成ボタンをクリックして創作を開始してください<br>Select terms and click generate to start creating</div>';
}
// 随机选择每个类别中的一个提示词
function selectRandomTerms() {
// 先清除所有已选择的词条
clearAllTerms();
// 获取所有类别
const categories = document.querySelectorAll('.category');
const randomTerms = [];
categories.forEach(category => {
const termItems = category.querySelectorAll('.term-item');
if (termItems.length > 0) {
// 从每个类别中随机选择一个
const randomIndex = Math.floor(Math.random() * termItems.length);
const selectedItem = termItems[randomIndex];
selectedItem.classList.add('selected');
randomTerms.push({
japanese: selectedItem.querySelector('.japanese').textContent,
english: selectedItem.querySelector('.term').textContent
});
}
});
// 更新选中的词条显示
updateSelections();
// 可选:显示一个提示告诉用户选择了哪些词条
if (randomTerms.length > 0) {
console.log('ランダムに選ばれた用語:', randomTerms);
// 可以添加一个简短的提示
const notification = document.createElement('div');
notification.style.cssText = `
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
background: var(--success-color);
color: white;
padding: 0.8rem 1.5rem;
border-radius: 20px;
z-index: 1000;
animation: slideDown 0.3s ease;
`;
notification.textContent = `${randomTerms.length}個の条項がランダムに選択されました Randomly selected ${randomTerms.length} terms`;
document.body.appendChild(notification);
// 3秒后移除提示
setTimeout(() => {
notification.remove();
}, 3000);
}
}
// 事件监听
document.addEventListener('click', (e) => {
// 点击词条选择/取消选择
if (e.target.closest('.term-item')) {
const item = e.target.closest('.term-item');
item.classList.toggle('selected');
updateSelections();
}
// 点击已选择的词条气泡取消选择
if (e.target.closest('.selected-term')) {
const term = e.target.closest('.selected-term').dataset.term;
const termItem = document.querySelector(`.term-item[data-term="${term}"]`);
if (termItem) {
termItem.classList.remove('selected');
updateSelections();
} else {
// 如果是手动添加的提示词,直接移除元素
e.target.closest('.selected-term').remove();
}
}
});
window.addEventListener('load', () => {
loadAndDisplayTerms();
const generateBtn = document.getElementById('generate-btn');
const randomBtn = document.getElementById('random-btn');
const clearTermsBtn = document.getElementById('clear-terms-btn');
const clearImagesBtn = document.getElementById('clear-images-btn');
const addTermBtn = document.getElementById('add-term-btn');
const manualTermInput = document.getElementById('manual-term-input');
// 生成按钮
generateBtn.addEventListener('click', async () => {
// 获取预定义的选中词条
const predefinedTerms = Array.from(document.querySelectorAll('.term-item.selected'))
.map(item => item.dataset.term);
// 获取手动输入的词条
const manualTerms = Array.from(document.querySelectorAll('#selected-terms-list .selected-term'))
.map(item => item.dataset.term)
.filter(term => {
// 过滤掉预定义的词条,避免重复
return !predefinedTerms.includes(term);
});
// 合并所有词条
const selectedTerms = [...predefinedTerms, ...manualTerms];
if (selectedTerms.length === 0) {
alert('少なくとも1つのキーワードを選択してください Please select at least one term');
return;
}
generateBtn.disabled = true;
const progressPromise = showProgress();
try {
const [response] = await Promise.all([
fetch('/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ terms: selectedTerms })
}),
progressPromise
]);
if (response.ok) {
addImageToHistory(selectedTerms);
} else {
const error = await response.json();
alert(`生成失敗 Generation failed: ${error.error}`);
}
} catch (error) {
console.error('生成リクエストに失敗しました Generation request failed:', error);
alert('生成リクエストに失敗しました Generation request failed, please check network connection');
} finally {
generateBtn.disabled = false;
}
});
randomBtn.addEventListener('click', () => {
selectRandomTerms();
});
// 清除提示词按钮
clearTermsBtn.addEventListener('click', clearAllTerms);
// 清除图片按钮
clearImagesBtn.addEventListener('click', () => {
if (confirm('生成されたすべての画像を削除してもよろしいですか? Are you sure you want to clear all generated images?')) {
clearAllImages();
}
});
// 添加手动输入的提示词
addTermBtn.addEventListener('click', () => {
const term = manualTermInput.value.trim();
if (term) {
// 创建一个新的选中项
const selectedTermsList = document.getElementById('selected-terms-list');
const existingTerm = selectedTermsList.querySelector(`.selected-term[data-term="${term}"]`);
// 避免重复添加
if (!existingTerm) {
const span = document.createElement('span');
span.className = 'selected-term';
span.dataset.term = term;
span.textContent = term;
selectedTermsList.appendChild(span);
}
// 清空输入框
manualTermInput.value = '';
}
});
// 回车键添加提示词
manualTermInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
addTermBtn.click();
}
});
});
</script>
</body>
</html>
|