evaluate this app: <script> // Pollinations API Handler Class class PollinationsAPI {…
- User
evaluate this app: // Pollinations API Handler Class class PollinationsAPI { static retryDelay = 500; // ms static maxRetries = 5; static requestQueue = []; static isProcessingQueue = false; static rateLimitDelay = 1000; // 1 second
static async fetchWithRetry(url, options = {}, retries = this.maxRetries) { try { const response = await fetch(url, options); if (response.status === 429) { throw new Error('RATE_LIMIT'); } if (!response.ok) { throw new Error('FETCH_ERROR'); } return response; } catch (error) { if (retries === 0) throw error; if (error.message === 'RATE_LIMIT') { await new Promise(res => setTimeout(res, this.rateLimitDelay)); } else { await new Promise(res => setTimeout(res, this.retryDelay * (this.maxRetries - retries + 1))); } return this.fetchWithRetry(url, options, retries - 1); } } static async queueRequest(requestFn) { return new Promise((resolve, reject) => { this.requestQueue.push({ requestFn, resolve, reject }); if (!this.isProcessingQueue) this.processQueue(); }); } static async processQueue() { if (this.requestQueue.length === 0) { this.isProcessingQueue = false; return; } this.isProcessingQueue = true; const { requestFn, resolve, reject } = this.requestQueue.shift(); try { const result = await requestFn(); resolve(result); } catch (error) { reject(error); } await new Promise(res => setTimeout(res, this.rateLimitDelay)); this.processQueue(); } static async generateText(prompt) { return this.queueRequest(async () => { try { const url = `https://text.pollinations.ai/${encodeURIComponent(prompt)}`; const response = await this.fetchWithRetry(url); return response.text(); } catch (error) { if (error.message === 'RATE_LIMIT') { throw new Error('Rate limit exceeded. Please try again later.'); } throw new Error('Failed to generate text.'); } }); } static async generateImage(prompt, settings = {}) { return this.queueRequest(async () => { const baseUrl = 'https://image.pollinations.ai/prompt/'; const params = new URLSearchParams({ width: settings.width || 1024, height: settings.height || 576, seed: settings.seed || Math.floor(Math.random() * 1000000), nologo: 'true', private: 'true', t: Date.now() }); const encodedPrompt = encodeURIComponent(prompt); const url = `${baseUrl}${encodedPrompt}?${params}`; try { const response = await this.fetchWithRetry(url); return response.url; } catch (error) { if (error.message === 'RATE_LIMIT') { throw new Error('Rate limit exceeded. Please try again later.'); } throw new Error('Failed to generate image.'); } }); } } // Main Application Class class LyricalStorybookCreator { constructor() { this.currentPage = 0; this.pages = []; this.isGenerating = false; this.selectedStyle = null; this.darkMode = true; // Change from false to true // UI Elements this.lyricsInput = document.getElementById('lyrics'); this.themeInput = document.getElementById('theme'); this.artStyleSelect = document.getElementById('artStyle'); this.pageLayoutSelect = document.getElementById('pageLayout'); this.textStyleSelect = document.getElementById('textStyle'); this.generateBtn = document.getElementById('generateBtn'); this.exportBtn = document.getElementById('exportBtn'); this.resetBtn = document.getElementById('resetBtn'); this.previewArea = document.getElementById('previewArea'); this.messageBox = document.getElementById('messageBox'); this.paginationControls = document.getElementById('paginationControls'); this.prevPageBtn = document.getElementById('prevPageBtn'); this.nextPageBtn = document.getElementById('nextPageBtn'); this.pageIndicator = document.getElementById('pageIndicator'); this.modeToggle = document.getElementById('modeToggle'); this.bindEvents(); } bindEvents() { this.generateBtn.addEventListener('click', () => this.generateStorybook()); this.exportBtn.addEventListener('click', () => this.exportPDF()); this.resetBtn.addEventListener('click', () => this.resetApp()); this.prevPageBtn.addEventListener('click', () => this.navigatePage(-1)); this.nextPageBtn.addEventListener('click', () => this.navigatePage(1)); this.modeToggle.addEventListener('change', () => this.toggleTheme()); // Swipe Navigation for Mobile this.previewArea.addEventListener('touchstart', this.handleTouchStart.bind(this), false); this.previewArea.addEventListener('touchend', this.handleTouchEnd.bind(this), false); this.xDown = null; } handleTouchStart(evt) { const firstTouch = evt.touches[0]; this.xDown = firstTouch.clientX; } handleTouchEnd(evt) { if (!this.xDown) return; const xUp = evt.changedTouches[0].clientX; const deltaX = this.xDown - xUp; const threshold = 50; // Required min distance for swipe if (deltaX > threshold) { this.navigatePage(1); } else if (deltaX < -threshold) { this.navigatePage(-1); } this.xDown = null; } showMessage(message, type = 'success') { this.messageBox.textContent = message; this.messageBox.className = `message ${type}`; this.messageBox.style.display = 'block'; setTimeout(() => { this.messageBox.style.display = 'none'; }, 4000); } async generateStorybook() { if (this.isGenerating) return; // Get current styles directly this.selectedStyle = { artStyle: this.artStyleSelect.value, pageLayout: this.pageLayoutSelect.value, textStyle: this.textStyleSelect.value }; const lyrics = this.lyricsInput.value.trim(); const theme = this.themeInput.value.trim(); if (!lyrics || !theme) { this.showMessage('Please enter both lyrics and theme.', 'error'); return; } this.isGenerating = true; this.generateBtn.classList.add('generating'); this.generateBtn.disabled = true; // Add loading spinner to preview area this.previewArea.innerHTML = ` <div class="loading-container"> <div class="loading-spinner"></div> <p>Generating your storybook...</p> </div> `; this.showMessage('Generating your storybook...', 'success'); try { // Split lyrics into segments (3-4 maximum) const rawSegments = lyrics.split('\n').filter(line => line.trim() !== ''); const maxPages = rawSegments.length <= 12 ? 3 : 4; // Use 3 pages for shorter lyrics, 4 for longer const segmentsPerPage = Math.ceil(rawSegments.length / maxPages); const segments = []; for (let i = 0; i < rawSegments.length; i += segmentsPerPage) { segments.push(rawSegments.slice(i, i + segmentsPerPage).join(' ')); } this.pages = []; for (let i = 0; i < segments.length && i < maxPages; i++) { const segment = segments[i]; // Generate scene description const sceneDescription = await PollinationsAPI.generateText(` Create a vivid, detailed description for a storybook illustration. Context: These song lyrics: "${segment}" Theme: ${theme} Style: ${this.selectedStyle.artStyle} Consider: - The emotional tone and mood - Key symbols and metaphors - Color palette and lighting - Character expressions and poses - Environmental details and setting - Composition and focal points Describe the scene in rich, artistic detail. `); // Generate image prompt const imagePrompt = await this.buildImagePrompt(this.selectedStyle.artStyle, theme, sceneDescription); const imageUrl = await PollinationsAPI.generateImage(imagePrompt, { width: 1280, height: 720 }); this.pages.push({ lyrics: segment, description: sceneDescription, imageUrl: imageUrl }); this.showMessage(`Page ${i + 1} of ${segments.length} generated.`, 'success'); } this.currentPage = 0; this.updatePreview(); } catch (error) { console.error(error); this.showMessage(error.message, 'error'); // Show error in preview area this.previewArea.innerHTML = `<p>Error generating storybook: ${error.message}</p>`; } finally { this.isGenerating = false; this.generateBtn.classList.remove('generating'); this.generateBtn.disabled = false; } } async buildImagePrompt(artStyle, theme, sceneDescription) { // Base quality parameters const qualityParams = "masterpiece quality, highly detailed, sharp focus, professional lighting"; // Style-specific prompts const stylePrompts = { 'whimsical': "whimsical and playful illustration, magical atmosphere, enchanted storybook style, soft dreamy colors", 'photorealistic': "hyperrealistic detailed illustration, cinematic lighting, dramatic atmosphere, vivid colors", 'sketch': "detailed hand-drawn sketch, artistic linework, expressive strokes, traditional illustration", 'pencil': "detailed pencil drawing, fine graphite shading, delicate linework, traditional pencil art technique, soft textures", 'vintage': "classic vintage storybook art, traditional illustration style, nostalgic color palette, timeless aesthetic", 'cartoon': "animated storybook style, expressive characters, bold colors, clean lines", 'exaggerated': "stylized artistic illustration, bold and dynamic composition, emphasize key elements, dramatic perspective", 'watercolor': "delicate watercolor illustration, soft color transitions, artistic texture, ethereal atmosphere", 'acrylic': "rich acrylic painting style, bold brushstrokes, textured illustration, vibrant colors", 'digital': "modern digital art, clean refined illustration, perfect blend of realism and stylization", 'pop': "pop art illustration style, bold colors, graphic elements, contemporary storybook aesthetic", 'woodcut': "traditional woodcut print style, bold contrasting lines, textured blocks, hand-carved aesthetic, dramatic black and white composition" }; // Composition elements const composition = [ "balanced composition", "attention to detail", "harmonious color palette", "dynamic lighting", "emotional storytelling through visuals" ].join(", "); // Build final prompt return `${stylePrompts[artStyle]}, ${sceneDescription}, ${theme}, storybook illustration style, ${composition}, ${qualityParams}, suitable for narrative illustration, story-driven artwork, emotional depth, artistic excellence, trending on artstation`; } updatePreview() { if (this.pages.length === 0) { this.previewArea.innerHTML = '<p>No pages to display. Generate your storybook first.</p>'; this.paginationControls.style.display = 'none'; return; } const page = this.pages[this.currentPage]; const fontFamily = this.getFontFamily(this.selectedStyle.textStyle); const layoutClass = (() => { switch(this.selectedStyle.pageLayout) { case 'spread': return 'spread-layout'; case 'comic': return 'comic-layout'; default: return ''; } })(); this.previewArea.innerHTML = ` <div class="storybook-page ${layoutClass}"> <img src="${page.imageUrl}" alt="Story Illustration"> <div class="page-text" style="font-family: '${fontFamily}', sans-serif;"> <p>${page.lyrics}</p> </div> </div> `; // Update pagination controls this.pageIndicator.textContent = `Page ${this.currentPage + 1} of ${this.pages.length}`; this.paginationControls.style.display = 'flex'; this.prevPageBtn.disabled = this.currentPage === 0; this.nextPageBtn.disabled = this.currentPage === this.pages.length - 1; } navigatePage(direction) { const newPage = this.currentPage + direction; if (newPage >= 0 && newPage < this.pages.length) { this.currentPage = newPage; this.updatePreview(); } } getFontFamily(style) { const fontMap = { 'multilingual': 'Noto Sans', 'storybook': 'Roboto', 'elegant': 'Montserrat', 'modern': 'Helvetica', 'handwritten': 'Pacifico', 'classic': 'Times', 'minimalist': 'Arial' }; return fontMap[style] || 'Roboto'; } async exportPDF() { if (this.pages.length === 0) { this.showMessage('No storybook to export. Please generate it first.', 'error'); return; } this.exportBtn.classList.add('generating'); this.exportBtn.disabled = true; this.showMessage('Exporting your storybook as PDF...', 'success'); try { const docDefinition = { content: [], pageSize: 'A4', pageOrientation: this.selectedStyle.pageLayout === 'comic' ? 'landscape' : 'portrait', defaultStyle: { font: 'Roboto' } }; for (let i = 0; i < this.pages.length; i++) { const page = this.pages[i]; const imgData = await this.getImageData(page.imageUrl); // Create layout based on selected style switch(this.selectedStyle.pageLayout) { case 'spread': docDefinition.content.push({ columns: [ { width: '50%', image: imgData.dataUrl, fit: [250, 350] }, { width: '50%', text: page.lyrics, fontSize: 14, margin: [20, 0, 0, 0] } ], margin: [0, 20] }); break; case 'comic': docDefinition.content.push({ columns: [ { width: 'auto', stack: [ { image: imgData.dataUrl, fit: [200, 150] }, { text: page.lyrics, fontSize: 12, margin: [10, 0] } ] } ], margin: [0, 20] }); break; default: // single page layout docDefinition.content.push({ stack: [ { image: imgData.dataUrl, width: 500, alignment: 'center', margin: [0, 0, 0, 20] }, { text: page.lyrics, fontSize: 14, alignment: 'left', margin: [40, 0, 40, 20] } ] }); } // Add Page Break if not the last page if (i < this.pages.length - 1) { docDefinition.content.push({ text: '', pageBreak: 'after' }); } } // Create PDF and Download pdfMake.createPdf(docDefinition).download('Lyrical_Storybook.pdf'); this.showMessage('Storybook exported successfully!', 'success'); } catch (error) { console.error(error); this.showMessage('Failed to export PDF. Please try again.', 'error'); } finally { this.exportBtn.classList.remove('generating'); this.exportBtn.disabled = false; } } getImageData(url) { return new Promise((resolve, reject) => { const img = new Image(); img.setAttribute('crossOrigin', 'anonymous'); img.onload = () => { const canvas = document.createElement('canvas'); canvas.width = img.width; canvas.height = img.height; const ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0); const dataUrl = canvas.toDataURL('image/jpeg'); resolve({ dataUrl, width: img.width, height: img.height }); }; img.onerror = () => { reject(new Error('Failed to load image.')); }; img.src = url; }); } resetApp() { // Clear Inputs this.lyricsInput.value = ''; this.themeInput.value = ''; this.artStyleSelect.value = 'woodcut'; this.pageLayoutSelect.value = 'single'; this.textStyleSelect.value = 'multilingual'; // Reset Variables this.pages = []; this.currentPage = 0; this.selectedStyle = null; // Reset Preview this.previewArea.innerHTML = '<p>Preview will appear here once you generate your storybook.</p>'; this.paginationControls.style.display = 'none'; // Clear Messages this.messageBox.style.display = 'none'; } toggleTheme() { this.darkMode = !this.darkMode; document.body.classList.toggle('light-mode', !this.darkMode); document.body.classList.toggle('dark-mode', this.darkMode); } } // Initialize the Application on DOM Content Loaded document.addEventListener('DOMContentLoaded', () => { window.storybookCreator = new LyricalStorybookCreator(); }); - Assistant