Elias207 commited on
Commit
d7673e5
·
verified ·
1 Parent(s): cc120ec

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +79 -82
index.html CHANGED
@@ -258,7 +258,7 @@
258
  <textarea id="prompt-input" rows="3" placeholder="مثال: پس‌زمینه را حذف کن و یک ساحل آفتابی قرار بده"></textarea>
259
  <button id="submit-btn" disabled>
260
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 3L12 8L17 10L12 12L10 17L8 12L3 10L8 8L10 3z"/></svg>
261
- <span id="btn-text">ایجاد کن</span>
262
  <div class="spinner"></div>
263
  </button>
264
  <p id="error-message"></p>
@@ -273,7 +273,7 @@
273
  <div class="generator-container">
274
  <div class="noise-layer"></div><div class="sketch-layer"></div><div class="building-layer"></div>
275
  <div class="pixel-grid"></div><div class="particles"></div>
276
- <div class="text-overlay">در حال ویرایش تصویر...</div><div class="progress-bar"></div>
277
  </div>
278
  </div>
279
  <div id="result-grid"></div>
@@ -324,104 +324,101 @@
324
  </div>
325
 
326
  <script>
327
- // --- START OF APPLICATION LOGIC SCRIPT ---
328
-
329
- // ... (all selectors are the same) ...
330
  const API_URL = 'https://alfa-editor-worker.onrender.com/api/edit';
331
  const uploadArea = document.getElementById('upload-area'); const fileInput = document.getElementById('file-input'); const previewImage = document.getElementById('preview-image-main'); const removeFileBtn = document.getElementById('remove-file-btn-main'); const promptInput = document.getElementById('prompt-input'); const submitBtn = document.getElementById('submit-btn'); const resultContainer = document.getElementById('result-container'); const resultGrid = document.getElementById('result-grid'); const errorMessage = document.getElementById('error-message'); const lightbox = document.getElementById('lightbox'); const lightboxImg = document.getElementById('lightbox-img'); const lightboxClose = document.getElementById('lightbox-close'); const lightboxDownload = document.getElementById('lightbox-download'); const lightboxNext = document.getElementById('lightbox-next'); const historyGrid = document.getElementById('history-grid'); const clearHistoryBtn = document.getElementById('clear-history-btn'); const confirmationModal = document.getElementById('confirmation-modal'); const modalMessageText = document.getElementById('modal-message-text'); const modalConfirmBtn = document.getElementById('modal-confirm-btn'); const modalCancelBtn = document.getElementById('modal-cancel-btn');
332
  let uploadedFile = null; let currentLightboxUrl = null; let currentLightboxGroup = []; let currentLightboxIndex = 0;
333
-
334
- // --- NEW: Function to convert any image file to a PNG File object ---
335
- const convertToPNG = (file) => {
336
- return new Promise((resolve, reject) => {
337
- const image = new Image();
338
- const reader = new FileReader();
339
-
340
- image.onload = () => {
341
- const canvas = document.createElement('canvas');
342
- canvas.width = image.width;
343
- canvas.height = image.height;
344
- const ctx = canvas.getContext('2d');
345
- ctx.drawImage(image, 0, 0);
346
-
347
- canvas.toBlob((blob) => {
348
- if (!blob) {
349
- reject(new Error('تبدیل به PNG ناموفق بود.'));
350
- return;
351
- }
352
- const originalName = file.name.substring(0, file.name.lastIndexOf('.')) || file.name;
353
- const pngFile = new File([blob], `${originalName}.png`, { type: 'image/png' });
354
- resolve(pngFile);
355
- }, 'image/png', 1.0); // 1.0 for max quality
356
- };
357
-
358
- image.onerror = () => reject(new Error('فایل تصویر قابل خواندن نیست.'));
359
- reader.onload = e => { image.src = e.target.result; };
360
- reader.onerror = () => reject(new Error('خطا در خواندن فایل.'));
361
-
362
- reader.readAsDataURL(file);
363
- });
364
- };
365
-
366
- // --- MODIFIED: The handleFile function is now async to handle conversion ---
367
- const handleFile = async (file) => {
368
- if (!file || !file.type.startsWith('image/')) {
369
- displayError('لطفا یک فایل تصویری معتبر انتخاب کنید.');
370
- return;
371
- }
372
-
373
- let fileToProcess = file;
374
- // Optimize: If the file is not PNG or JPEG, convert it.
375
- if (file.type !== 'image/png' && file.type !== 'image/jpeg') {
376
- try {
377
- // console.log(`Converting ${file.type} to PNG...`);
378
- fileToProcess = await convertToPNG(file);
379
- } catch (error) {
380
- console.error('Image conversion failed:', error);
381
- displayError('خطا در تبدیل فرمت تصویر. لطفا فایل دیگری را امتحان کنید.');
382
- resetUploader(); // Clear the uploader on failure
383
- return;
384
- }
385
- }
386
-
387
- uploadedFile = fileToProcess;
388
-
389
- // Use the original file for preview to avoid delay
390
- const reader = new FileReader();
391
- reader.onload = (e) => { previewImage.src = e.target.result; };
392
- reader.readAsDataURL(file);
393
-
394
- uploadArea.classList.add('has-file');
395
- checkFormState();
396
- clearResult();
397
- };
398
-
399
- // ... (The rest of the JS functions are unchanged) ...
400
- const showConfirmationModal = (message, onConfirm) => { modalMessageText.textContent = message; confirmationModal.classList.add('visible'); modalConfirmBtn.onclick = () => { onConfirm(); hideConfirmationModal(); }; modalCancelBtn.onclick = () => { hideConfirmationModal(); }; }; const hideConfirmationModal = () => { confirmationModal.classList.remove('visible'); modalConfirmBtn.onclick = null; modalCancelBtn.onclick = null; }; const resetUploader = () => { uploadedFile = null; fileInput.value = ''; previewImage.src = ''; uploadArea.classList.remove('has-file'); checkFormState(); }; const checkFormState = () => { submitBtn.disabled = !uploadedFile || promptInput.value.trim() === ''; }; const setLoading = (isLoading) => { const btnSpinner = submitBtn.querySelector('.spinner'); const btnText = document.getElementById('btn-text'); const btnIcon = submitBtn.querySelector('svg'); if (isLoading) { resultContainer.classList.remove('has-content'); resultContainer.classList.add('loading'); btnSpinner.style.display = 'inline-block'; btnIcon.style.display = 'none'; btnText.textContent = 'در حال پردازش...'; submitBtn.disabled = true; resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; } else { resultContainer.classList.remove('loading'); btnSpinner.style.display = 'none'; btnIcon.style.display = 'inline-block'; btnText.textContent = 'خلق کن'; checkFormState(); } }; const displayResult = (imageUrls) => { resultGrid.innerHTML = ''; imageUrls.forEach((url) => { const img = document.createElement('img'); img.src = url; img.alt = 'تصویر ویرایش شده'; img.addEventListener('click', () => openLightbox(url, imageUrls)); resultGrid.appendChild(img); }); resultContainer.classList.add('has-content'); }; const clearResult = () => { resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; resultContainer.classList.remove('has-content'); }; const displayError = (message) => { errorMessage.textContent = message; errorMessage.style.display = 'block'; }; const updateLightboxImage = () => { const newUrl = currentLightboxGroup[currentLightboxIndex]; lightboxImg.src = newUrl; currentLightboxUrl = newUrl; }; const openLightbox = (clickedUrl, urlGroup) => { currentLightboxGroup = urlGroup; currentLightboxIndex = urlGroup.indexOf(clickedUrl); updateLightboxImage(); lightboxNext.style.display = urlGroup.length > 1 ? 'flex' : 'none'; lightbox.classList.add('visible'); }; const closeLightbox = () => { lightbox.classList.remove('visible'); currentLightboxGroup = []; }; const handleDownload = async () => { if (!currentLightboxUrl) return; const spinner = lightboxDownload.querySelector('.spinner'); const icon = lightboxDownload.querySelector('svg'); spinner.style.display = 'block'; icon.style.display = 'none'; lightboxDownload.disabled = true; try { const response = await fetch(currentLightboxUrl); if (!response.ok) throw new Error('Network response was not ok.'); const blob = await response.blob(); const objectUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = objectUrl; a.download = `ai-edited-image-${Date.now()}.png`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(objectUrl); } catch (e) { console.error('Download failed:', e); alert('خطا در دانلود تصویر. لطفا دوباره تلاش کنید.'); } finally { spinner.style.display = 'none'; icon.style.display = 'block'; lightboxDownload.disabled = false; } }; const getHistory = () => JSON.parse(localStorage.getItem('aiPhotoshopHistory') || '[]'); const saveHistory = (history) => { localStorage.setItem('aiPhotoshopHistory', JSON.stringify(history)); renderHistory(); }; const addToHistory = (item) => { let history = getHistory(); history.unshift(item); if (history.length > 15) history.pop(); saveHistory(history); }; const handleClearHistory = () => { showConfirmationModal('آیا از پاک کردن تمام تاریخچه مطمئن هستید؟ این عمل غیرقابل بازگشت است.', () => { saveHistory([]); }); }; const handleDeleteItem = (index) => { showConfirmationModal('آیا از حذف این مورد از تاریخچه مطمئن هستید؟', () => { let history = getHistory(); history.splice(index, 1); saveHistory(history); }); }; const renderHistory = () => { const history = getHistory(); historyGrid.innerHTML = ''; clearHistoryBtn.style.display = history.length > 0 ? 'flex' : 'none'; history.forEach((item, index) => { const card = document.createElement('div'); card.className = 'history-item'; const deleteBtn = document.createElement('button'); deleteBtn.className = 'history-delete-btn'; deleteBtn.title = 'حذف این مورد'; deleteBtn.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>`; deleteBtn.onclick = () => handleDeleteItem(index); card.appendChild(deleteBtn); const imageGrid = document.createElement('div'); imageGrid.className = 'history-item-grid'; item.urls.forEach(url => { const img = document.createElement('img'); img.src = url; img.alt = 'تصویر از تاریخچه'; img.addEventListener('click', () => openLightbox(url, item.urls)); imageGrid.appendChild(img); }); card.appendChild(imageGrid); historyGrid.appendChild(card); }); };
401
  document.addEventListener('DOMContentLoaded', () => { renderHistory(); checkFormState(); });
402
  removeFileBtn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); resetUploader(); }); fileInput.addEventListener('change', (e) => handleFile(e.target.files[0])); ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, p => { p.preventDefault(); p.stopPropagation(); }); }); ['dragenter', 'dragover'].forEach(e => { uploadArea.addEventListener(e, () => { if (!uploadArea.classList.contains('has-file')) uploadArea.classList.add('drag-over'); }); }); ['dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, () => uploadArea.classList.remove('drag-over')); }); uploadArea.addEventListener('drop', e => { if (!uploadArea.classList.contains('has-file')) handleFile(e.dataTransfer.files[0]); }); promptInput.addEventListener('input', checkFormState);
403
  submitBtn.addEventListener('click', async () => { if (submitBtn.disabled) return; setLoading(true); const formData = new FormData(); formData.append('image', uploadedFile); formData.append('prompt', promptInput.value.trim()); try { const response = await fetch(API_URL, { method: 'POST', body: formData }); if (!response.ok) { const errorData = await response.json().catch(() => ({ error: `خطای سرور: ${response.statusText}` })); throw new Error(errorData.error || `یک خطای ناشناخته رخ داد.`); } const result = await response.json(); if (result.image_urls && Array.isArray(result.image_urls) && result.image_urls.length > 0) { displayResult(result.image_urls); addToHistory({ prompt: promptInput.value.trim(), urls: result.image_urls }); } else { throw new Error('پاسخ معتبری از سرور دریافت نشد.'); } } catch (error) { displayError(error.message); } finally { setLoading(false); } });
404
  lightboxClose.addEventListener('click', closeLightbox); lightbox.addEventListener('click', (e) => { if (e.target === lightbox) closeLightbox(); }); lightboxDownload.addEventListener('click', handleDownload); lightboxNext.addEventListener('click', () => { currentLightboxIndex = (currentLightboxIndex + 1) % currentLightboxGroup.length; updateLightboxImage(); }); clearHistoryBtn.addEventListener('click', handleClearHistory);
405
-
406
  </script>
407
 
408
  <script>
409
- // --- Header Animation Script (Unchanged) ---
410
  document.addEventListener('DOMContentLoaded', () => {
411
  const canvas = document.getElementById('neural-network-canvas');
412
  const header = canvas.parentElement;
413
  const ctx = canvas.getContext('2d');
 
414
  let particles = [];
415
- const particleCount = 20; const maxDistance = 100;
 
 
416
  const computedStyles = getComputedStyle(document.documentElement);
417
  const particleColor = computedStyles.getPropertyValue('--accent-primary').trim();
418
  const lineColor = computedStyles.getPropertyValue('--text-tertiary').trim();
419
- function resizeCanvas() { canvas.width = header.clientWidth; canvas.height = header.clientHeight; init(); }
420
- class Particle { constructor() { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height; this.vx = (Math.random() - 0.5) * 0.3; this.vy = (Math.random() - 0.5) * 0.3; this.radius = 1.2; } update() { this.x += this.vx; this.y += this.vy; if (this.x < 0 || this.x > canvas.width) this.vx *= -1; if (this.y < 0 || this.y > canvas.height) this.vy *= -1; } draw() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2); ctx.fillStyle = particleColor; ctx.fill(); } }
421
- function init() { particles = []; for (let i = 0; i < particleCount; i++) { particles.push(new Particle()); } }
422
- function connectParticles() { for (let i = 0; i < particles.length; i++) { for (let j = i + 1; j < particles.length; j++) { const dx = particles[i].x - particles[j].x; const dy = particles[i].y - particles[j].y; const distance = Math.sqrt(dx * dx + dy * dy); if (distance < maxDistance) { ctx.beginPath(); ctx.moveTo(particles[i].x, particles[i].y); ctx.lineTo(particles[j].x, particles[j].y); ctx.strokeStyle = lineColor; ctx.lineWidth = 0.2; ctx.globalAlpha = 1 - distance / maxDistance; ctx.stroke(); } } } ctx.globalAlpha = 1; }
423
- function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); particles.forEach(particle => { particle.update(); particle.draw(); }); connectParticles(); requestAnimationFrame(animate); }
424
- window.addEventListener('resize', resizeCanvas); resizeCanvas(); animate();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
425
  });
426
  </script>
427
  </body>
 
258
  <textarea id="prompt-input" rows="3" placeholder="مثال: پس‌زمینه را حذف کن و یک ساحل آفتابی قرار بده"></textarea>
259
  <button id="submit-btn" disabled>
260
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 3L12 8L17 10L12 12L10 17L8 12L3 10L8 8L10 3z"/></svg>
261
+ <span id="btn-text">خلق کن</span>
262
  <div class="spinner"></div>
263
  </button>
264
  <p id="error-message"></p>
 
273
  <div class="generator-container">
274
  <div class="noise-layer"></div><div class="sketch-layer"></div><div class="building-layer"></div>
275
  <div class="pixel-grid"></div><div class="particles"></div>
276
+ <div class="text-overlay">در حال ساخت تصویر...</div><div class="progress-bar"></div>
277
  </div>
278
  </div>
279
  <div id="result-grid"></div>
 
324
  </div>
325
 
326
  <script>
327
+ // --- Application Logic Script (Unchanged) ---
 
 
328
  const API_URL = 'https://alfa-editor-worker.onrender.com/api/edit';
329
  const uploadArea = document.getElementById('upload-area'); const fileInput = document.getElementById('file-input'); const previewImage = document.getElementById('preview-image-main'); const removeFileBtn = document.getElementById('remove-file-btn-main'); const promptInput = document.getElementById('prompt-input'); const submitBtn = document.getElementById('submit-btn'); const resultContainer = document.getElementById('result-container'); const resultGrid = document.getElementById('result-grid'); const errorMessage = document.getElementById('error-message'); const lightbox = document.getElementById('lightbox'); const lightboxImg = document.getElementById('lightbox-img'); const lightboxClose = document.getElementById('lightbox-close'); const lightboxDownload = document.getElementById('lightbox-download'); const lightboxNext = document.getElementById('lightbox-next'); const historyGrid = document.getElementById('history-grid'); const clearHistoryBtn = document.getElementById('clear-history-btn'); const confirmationModal = document.getElementById('confirmation-modal'); const modalMessageText = document.getElementById('modal-message-text'); const modalConfirmBtn = document.getElementById('modal-confirm-btn'); const modalCancelBtn = document.getElementById('modal-cancel-btn');
330
  let uploadedFile = null; let currentLightboxUrl = null; let currentLightboxGroup = []; let currentLightboxIndex = 0;
331
+ const showConfirmationModal = (message, onConfirm) => { modalMessageText.textContent = message; confirmationModal.classList.add('visible'); modalConfirmBtn.onclick = () => { onConfirm(); hideConfirmationModal(); }; modalCancelBtn.onclick = () => { hideConfirmationModal(); }; }; const hideConfirmationModal = () => { confirmationModal.classList.remove('visible'); modalConfirmBtn.onclick = null; modalCancelBtn.onclick = null; }; const resetUploader = () => { uploadedFile = null; fileInput.value = ''; previewImage.src = ''; uploadArea.classList.remove('has-file'); checkFormState(); }; const handleFile = (file) => { if (!file || !file.type.startsWith('image/')) { displayError('لطفا یک فایل تصویری معتبر انتخاب کنید.'); return; } uploadedFile = file; const reader = new FileReader(); reader.onload = (e) => { previewImage.src = e.target.result; }; reader.readAsDataURL(file); uploadArea.classList.add('has-file'); checkFormState(); clearResult(); }; const checkFormState = () => { submitBtn.disabled = !uploadedFile || promptInput.value.trim() === ''; }; const setLoading = (isLoading) => { const btnSpinner = submitBtn.querySelector('.spinner'); const btnText = document.getElementById('btn-text'); const btnIcon = submitBtn.querySelector('svg'); if (isLoading) { resultContainer.classList.remove('has-content'); resultContainer.classList.add('loading'); btnSpinner.style.display = 'inline-block'; btnIcon.style.display = 'none'; btnText.textContent = 'در حال پردازش...'; submitBtn.disabled = true; resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; } else { resultContainer.classList.remove('loading'); btnSpinner.style.display = 'none'; btnIcon.style.display = 'inline-block'; btnText.textContent = 'خلق کن'; checkFormState(); } }; const displayResult = (imageUrls) => { resultGrid.innerHTML = ''; imageUrls.forEach((url) => { const img = document.createElement('img'); img.src = url; img.alt = 'تصویر ویرایش شده'; img.addEventListener('click', () => openLightbox(url, imageUrls)); resultGrid.appendChild(img); }); resultContainer.classList.add('has-content'); }; const clearResult = () => { resultGrid.innerHTML = ''; errorMessage.style.display = 'none'; resultContainer.classList.remove('has-content'); }; const displayError = (message) => { errorMessage.textContent = message; errorMessage.style.display = 'block'; }; const updateLightboxImage = () => { const newUrl = currentLightboxGroup[currentLightboxIndex]; lightboxImg.src = newUrl; currentLightboxUrl = newUrl; }; const openLightbox = (clickedUrl, urlGroup) => { currentLightboxGroup = urlGroup; currentLightboxIndex = urlGroup.indexOf(clickedUrl); updateLightboxImage(); lightboxNext.style.display = urlGroup.length > 1 ? 'flex' : 'none'; lightbox.classList.add('visible'); }; const closeLightbox = () => { lightbox.classList.remove('visible'); currentLightboxGroup = []; }; const handleDownload = async () => { if (!currentLightboxUrl) return; const spinner = lightboxDownload.querySelector('.spinner'); const icon = lightboxDownload.querySelector('svg'); spinner.style.display = 'block'; icon.style.display = 'none'; lightboxDownload.disabled = true; try { const response = await fetch(currentLightboxUrl); if (!response.ok) throw new Error('Network response was not ok.'); const blob = await response.blob(); const objectUrl = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = objectUrl; a.download = `ai-edited-image-${Date.now()}.png`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(objectUrl); } catch (e) { console.error('Download failed:', e); alert('خطا در دانلود تصویر. لطفا دوباره تلاش کنید.'); } finally { spinner.style.display = 'none'; icon.style.display = 'block'; lightboxDownload.disabled = false; } }; const getHistory = () => JSON.parse(localStorage.getItem('aiPhotoshopHistory') || '[]'); const saveHistory = (history) => { localStorage.setItem('aiPhotoshopHistory', JSON.stringify(history)); renderHistory(); }; const addToHistory = (item) => { let history = getHistory(); history.unshift(item); if (history.length > 15) history.pop(); saveHistory(history); }; const handleClearHistory = () => { showConfirmationModal('آیا از پاک کردن تمام تاریخچه مطمئن هستید؟ این عمل غیرقابل بازگشت است.', () => { saveHistory([]); }); }; const handleDeleteItem = (index) => { showConfirmationModal('آیا از حذف این مورد از تاریخچه مطمئن هستید؟', () => { let history = getHistory(); history.splice(index, 1); saveHistory(history); }); }; const renderHistory = () => { const history = getHistory(); historyGrid.innerHTML = ''; clearHistoryBtn.style.display = history.length > 0 ? 'flex' : 'none'; history.forEach((item, index) => { const card = document.createElement('div'); card.className = 'history-item'; const deleteBtn = document.createElement('button'); deleteBtn.className = 'history-delete-btn'; deleteBtn.title = 'حذف این مورد'; deleteBtn.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 6h18"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6"/><path d="M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>`; deleteBtn.onclick = () => handleDeleteItem(index); card.appendChild(deleteBtn); const imageGrid = document.createElement('div'); imageGrid.className = 'history-item-grid'; item.urls.forEach(url => { const img = document.createElement('img'); img.src = url; img.alt = 'تصویر از تاریخچه'; img.addEventListener('click', () => openLightbox(url, item.urls)); imageGrid.appendChild(img); }); card.appendChild(imageGrid); historyGrid.appendChild(card); }); };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332
  document.addEventListener('DOMContentLoaded', () => { renderHistory(); checkFormState(); });
333
  removeFileBtn.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); resetUploader(); }); fileInput.addEventListener('change', (e) => handleFile(e.target.files[0])); ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, p => { p.preventDefault(); p.stopPropagation(); }); }); ['dragenter', 'dragover'].forEach(e => { uploadArea.addEventListener(e, () => { if (!uploadArea.classList.contains('has-file')) uploadArea.classList.add('drag-over'); }); }); ['dragleave', 'drop'].forEach(e => { uploadArea.addEventListener(e, () => uploadArea.classList.remove('drag-over')); }); uploadArea.addEventListener('drop', e => { if (!uploadArea.classList.contains('has-file')) handleFile(e.dataTransfer.files[0]); }); promptInput.addEventListener('input', checkFormState);
334
  submitBtn.addEventListener('click', async () => { if (submitBtn.disabled) return; setLoading(true); const formData = new FormData(); formData.append('image', uploadedFile); formData.append('prompt', promptInput.value.trim()); try { const response = await fetch(API_URL, { method: 'POST', body: formData }); if (!response.ok) { const errorData = await response.json().catch(() => ({ error: `خطای سرور: ${response.statusText}` })); throw new Error(errorData.error || `یک خطای ناشناخته رخ داد.`); } const result = await response.json(); if (result.image_urls && Array.isArray(result.image_urls) && result.image_urls.length > 0) { displayResult(result.image_urls); addToHistory({ prompt: promptInput.value.trim(), urls: result.image_urls }); } else { throw new Error('پاسخ معتبری از سرور دریافت نشد.'); } } catch (error) { displayError(error.message); } finally { setLoading(false); } });
335
  lightboxClose.addEventListener('click', closeLightbox); lightbox.addEventListener('click', (e) => { if (e.target === lightbox) closeLightbox(); }); lightboxDownload.addEventListener('click', handleDownload); lightboxNext.addEventListener('click', () => { currentLightboxIndex = (currentLightboxIndex + 1) % currentLightboxGroup.length; updateLightboxImage(); }); clearHistoryBtn.addEventListener('click', handleClearHistory);
 
336
  </script>
337
 
338
  <script>
339
+ // --- Header Animation Script (Extremely sparse version) ---
340
  document.addEventListener('DOMContentLoaded', () => {
341
  const canvas = document.getElementById('neural-network-canvas');
342
  const header = canvas.parentElement;
343
  const ctx = canvas.getContext('2d');
344
+
345
  let particles = [];
346
+ const particleCount = 20; // کاهش شدید تعداد ذرات به 20
347
+ const maxDistance = 100; // کاهش فاصله اتصال برای خطوط کمتر
348
+
349
  const computedStyles = getComputedStyle(document.documentElement);
350
  const particleColor = computedStyles.getPropertyValue('--accent-primary').trim();
351
  const lineColor = computedStyles.getPropertyValue('--text-tertiary').trim();
352
+
353
+ function resizeCanvas() {
354
+ canvas.width = header.clientWidth;
355
+ canvas.height = header.clientHeight;
356
+ init();
357
+ }
358
+
359
+ class Particle {
360
+ constructor() {
361
+ this.x = Math.random() * canvas.width;
362
+ this.y = Math.random() * canvas.height;
363
+ this.vx = (Math.random() - 0.5) * 0.3; // سرعت آرام
364
+ this.vy = (Math.random() - 0.5) * 0.3; // سرعت آرام
365
+ this.radius = 1.2; // کوچک‌تر کردن ذرات
366
+ }
367
+ update() {
368
+ this.x += this.vx;
369
+ this.y += this.vy;
370
+ if (this.x < 0 || this.x > canvas.width) this.vx *= -1;
371
+ if (this.y < 0 || this.y > canvas.height) this.vy *= -1;
372
+ }
373
+ draw() {
374
+ ctx.beginPath();
375
+ ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
376
+ ctx.fillStyle = particleColor;
377
+ ctx.fill();
378
+ }
379
+ }
380
+
381
+ function init() {
382
+ particles = [];
383
+ for (let i = 0; i < particleCount; i++) {
384
+ particles.push(new Particle());
385
+ }
386
+ }
387
+
388
+ function connectParticles() {
389
+ for (let i = 0; i < particles.length; i++) {
390
+ for (let j = i + 1; j < particles.length; j++) {
391
+ const dx = particles[i].x - particles[j].x;
392
+ const dy = particles[i].y - particles[j].y;
393
+ const distance = Math.sqrt(dx * dx + dy * dy);
394
+
395
+ if (distance < maxDistance) {
396
+ ctx.beginPath();
397
+ ctx.moveTo(particles[i].x, particles[i].y);
398
+ ctx.lineTo(particles[j].x, particles[j].y);
399
+ ctx.strokeStyle = lineColor;
400
+ ctx.lineWidth = 0.2; // نازک‌تر کردن خطوط
401
+ ctx.globalAlpha = 1 - distance / maxDistance;
402
+ ctx.stroke();
403
+ }
404
+ }
405
+ }
406
+ ctx.globalAlpha = 1;
407
+ }
408
+
409
+ function animate() {
410
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
411
+ particles.forEach(particle => {
412
+ particle.update();
413
+ particle.draw();
414
+ });
415
+ connectParticles();
416
+ requestAnimationFrame(animate);
417
+ }
418
+
419
+ window.addEventListener('resize', resizeCanvas);
420
+ resizeCanvas();
421
+ animate();
422
  });
423
  </script>
424
  </body>