-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (91 loc) · 3.84 KB
/
Copy pathscript.js
File metadata and controls
114 lines (91 loc) · 3.84 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
let isGenerating = false;
// Trigger generation on 'Enter' key
document.getElementById('prompt').addEventListener('keypress', function (e) {
if (e.key === 'Enter') generateImage();
});
async function generateImage() {
if (isGenerating) return;
const promptInput = document.getElementById('prompt');
const prompt = promptInput.value.trim();
if (!prompt) {
alert("Please enter a description first!");
return;
}
// UI Updates
isGenerating = true;
const btn = document.getElementById('generate-btn');
btn.classList.add('loading');
// Get Options
const style = document.getElementById('style').value;
const format = document.getElementById('format').value;
const isHD = document.getElementById('hd-option').checked;
const noLogo = document.getElementById('private-option').checked;
// Calculate Dimensions
let width = 1024;
let height = 1024;
if (format === 'portrait') { width = 768; height = 1152; }
else if (format === 'landscape') { width = 1152; height = 768; }
else if (format === 'widescreen') { width = 1280; height = 720; }
// Construct the URL
// We add a random seed to prevent caching!
const seed = Math.floor(Math.random() * 1000000);
const encodedPrompt = encodeURIComponent(`${style} style, ${prompt}`);
// Pollinations URL structure
let imageUrl = `https://image.pollinations.ai/prompt/${encodedPrompt}?width=${width}&height=${height}&seed=${seed}&nologo=${noLogo}`;
if (isHD) {
imageUrl += "&model=flux"; // Better quality model
}
// Create a new image object to pre-load
const img = new Image();
img.onload = () => {
displayResult(img.src);
isGenerating = false;
btn.classList.remove('loading');
};
img.onerror = () => {
alert("Oops! Server is busy. Try again.");
isGenerating = false;
btn.classList.remove('loading');
};
// Start loading
img.src = imageUrl;
}
function displayResult(url) {
const generatorPanel = document.getElementById('generator-panel');
const resultContainer = document.getElementById('result-container');
const imageWrapper = document.getElementById('image-wrapper');
const downloadBtn = document.getElementById('download-btn');
// Inject Image
imageWrapper.innerHTML = `<img src="${url}" alt="Generated Image">`;
// Setup Download Button (Fetching blob to avoid CORS issues)
downloadBtn.onclick = async () => {
try {
downloadBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Saving...';
const response = await fetch(url);
const blob = await response.blob();
const blobUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = blobUrl;
link.download = `dreamscape-${Date.now()}.jpg`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
downloadBtn.innerHTML = '<i class="fas fa-download"></i> Save';
} catch (e) {
console.error(e);
alert("Could not download automatically. Right click the image to save.");
downloadBtn.innerHTML = '<i class="fas fa-download"></i> Save';
}
};
// Transition Animations
generatorPanel.classList.add('hidden');
resultContainer.classList.remove('hidden');
resultContainer.classList.add('animate__animated', 'animate__fadeInUp');
}
function hideResults() {
const generatorPanel = document.getElementById('generator-panel');
const resultContainer = document.getElementById('result-container');
resultContainer.classList.add('hidden');
generatorPanel.classList.remove('hidden');
generatorPanel.classList.add('animate__animated', 'animate__fadeIn');
}