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;
// UI Elements
this.lyricsInput = document.getElementById('lyrics');
this.themeInput = document.getElementById('theme');
this.artStyleSelect = document.getElementById('artStyle');
this.colorPaletteSelect = document.getElementById('colorPalette');
this.pageLayoutSelect = document.querySelector('.layout-controls .layout-select');
this.textStyleSelect = document.getElementById('textStyle');
this.exportBtn = document.querySelector('.layout-controls #exportBtn');
this.generateBtn = document.getElementById('generateBtn');
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');
// Add new property to track layout controls container
this.layoutControls = document.querySelector('.layout-controls');
// Initially hide layout controls
this.layoutControls.style.display = 'none';
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.pageLayoutSelect.addEventListener('change', () => {
if (this.pages.length > 0) {
this.updatePreview();
}
});
this.textStyleSelect.addEventListener('change', () => {
if (this.pages.length > 0) {
this.updatePreview();
}
});
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;
// Hide layout controls during generation
this.layoutControls.style.display = 'none';
// Set default page layout to single
this.selectedStyle = {
artStyle: this.artStyleSelect.value,
colorPalette: this.colorPaletteSelect.value,
pageLayout: 'single'
};
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, this.selectedStyle.colorPalette, 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;
// After successful generation, show the layout controls
this.layoutControls.style.display = 'flex';
this.updatePreview();
} catch (error) {
console.error(error);
this.showMessage(error.message, 'error');
this.previewArea.innerHTML = `<p>Error generating storybook: ${error.message}</p>`;
// Keep layout controls hidden on error
this.layoutControls.style.display = 'none';
} finally {
this.isGenerating = false;
this.generateBtn.classList.remove('generating');
this.generateBtn.disabled = false;
}
}
async buildImagePrompt(artStyle, colorPalette, theme, sceneDescription) {
// Base quality parameters
const qualityParams = "masterpiece quality, highly detailed, sharp focus, professional lighting";
// Color palette prompts
const colorPalettes = {
'vibrant': "vibrant saturated colors, bold color combinations, high contrast, energetic palette",
'pastel': "soft pastel colors, gentle muted tones, delicate color harmony, soothing palette",
'monochrome': "monochromatic color scheme, variations in tone and shade, sophisticated palette",
'earthy': "natural earth tones, organic color palette, browns, greens, and warm neutrals",
'neon': "bright neon colors, electric hues, high energy color palette, glowing effects",
'vintage': "faded vintage colors, retro color palette, nostalgic tones, aged appearance",
'cool': "cool color temperature, blues, purples, and cool greens, calming palette",
'warm': "warm color temperature, reds, oranges, and yellows, inviting palette",
'jewel': "rich jewel tones, deep saturated colors, luxurious color palette",
'grayscale': "black and white, grayscale values, dramatic contrast, monochromatic"
};
const selectedPalette = colorPalettes[colorPalette] || colorPalettes['vibrant'];
// 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",
selectedPalette,
"dynamic lighting",
"emotional storytelling through visuals"
].join(", ");
// Build final prompt
return `${stylePrompts[artStyle]},
${sceneDescription},
${theme},
${selectedPalette},
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 layoutClass = (() => {
switch(this.pageLayoutSelect.value) {
case 'spread': return 'spread-layout';
case 'comic': return 'comic-layout';
default: return '';
}
})();
const textStyleClass = this.textStyleSelect.value !== 'default' ? this.textStyleSelect.value : '';
this.previewArea.innerHTML = `
<div class="storybook-page ${layoutClass}">
<img src="${page.imageUrl}" alt="Story Illustration">
<div class="page-text ${textStyleClass}">
<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();
}
}
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 {
// Define custom fonts for pdfmake
const pdfFonts = {
Roboto: {
normal: 'https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/fonts/Roboto/Roboto-Regular.ttf',
bold: 'https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/fonts/Roboto/Roboto-Medium.ttf',
italics: 'https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/fonts/Roboto/Roboto-Italic.ttf',
bolditalics: 'https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/fonts/Roboto/Roboto-MediumItalic.ttf'
},
};
const docDefinition = {
content: [],
pageSize: 'A4',
pageOrientation: this.pageLayoutSelect.value === 'comic' ? 'landscape' : 'portrait',
defaultStyle: {
font: 'Roboto'
}
};
const textStyle = this.textStyleSelect.value;
const getTextStyling = () => {
switch(textStyle) {
case 'elegant':
return {
font: 'Roboto',
fontSize: 10, // Reduced from 14
lineHeight: 1.6,
italics: true
};
case 'playful':
return {
font: 'Roboto',
fontSize: 11, // Reduced from 14
lineHeight: 1.6
};
case 'dramatic':
return {
font: 'Roboto',
fontSize: 12, // Reduced from 14
lineHeight: 1.6,
bold: true
};
case 'minimal':
return {
font: 'Roboto',
fontSize: 9, // Reduced from 14
lineHeight: 1.6
};
default:
return {
font: 'Roboto',
fontSize: 10, // Reduced from 12
lineHeight: 1.6
};
}
};
for (let i = 0; i < this.pages.length; i++) {
const page = this.pages[i];
const imgData = await this.getImageData(page.imageUrl);
const textStyling = getTextStyling();
// Create layout based on selected style
switch(this.pageLayoutSelect.value) {
case 'spread':
docDefinition.content.push({
columns: [
{
width: '50%',
image: imgData.dataUrl,
fit: [350, 450]
},
{
width: '50%',
text: page.lyrics,
...textStyling,
margin: [20, 0, 0, 0]
}
],
margin: [0, 20]
});
break;
case 'comic':
docDefinition.content.push({
columns: [
{
width: 'auto',
stack: [
{
image: imgData.dataUrl,
fit: [400, 250]
},
{
text: page.lyrics,
...textStyling,
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,
...textStyling,
alignment: 'center',
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 with custom fonts
pdfMake.fonts = pdfFonts;
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.colorPaletteSelect.value = 'vibrant';
this.pageLayoutSelect.value = 'single';
this.textStyleSelect.value = 'default';
// 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';
// Hide layout controls
this.layoutControls.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();
});