html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Comic Strip Creator</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
<style>
/* Root Variables for Consistent Theming */
:root {
--primary-color: #6B48FF;
--secondary-color: #FFB648;
--accent-color: #FF6B6B;
--background-color: #F0F2F5;
--card-bg: #FFFFFF;
--text-primary: #2C3E50;
--text-secondary: #7F8C8D;
--success: #2ECC71;
--error: #E74C3C;
--shadow: rgba(0, 0, 0, 0.1);
--border-radius: 8px;
--transition-speed: 0.3s;
}
/* Global Styles */
body {
font-family: 'Poppins', sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
background: var(--background-color);
color: var(--text-primary);
line-height: 1.6;
}
h1, h2 {
color: var(--primary-color);
text-align: center;
margin-bottom: 20px;
}
h1 {
font-size: 2.5em;
position: relative;
}
h1::after {
content: '';
display: block;
width: 60px;
height: 4px;
background: var(--secondary-color);
margin: 10px auto;
border-radius: 2px;
}
/* Input Section Styling */
.input-section {
background: var(--card-bg);
padding: 30px;
border-radius: 16px;
box-shadow: 0 8px 24px var(--shadow);
margin-bottom: 30px;
transition: transform var(--transition-speed) ease;
}
.input-section:hover {
transform: translateY(-2px);
}
.character-inputs {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 30px;
margin-bottom: 20px;
}
.input-group {
margin-bottom: 20px;
position: relative;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: var(--text-primary);
font-size: 0.95em;
}
input, textarea, select {
width: 100%;
padding: 12px;
border: 2px solid #E0E7FF;
border-radius: var(--border-radius);
font-family: 'Poppins', sans-serif;
transition: border-color var(--transition-speed) ease, box-shadow var(--transition-speed) ease;
font-size: 1em;
}
input:focus, textarea:focus, select:focus {
outline: none;
border-color: var(--primary-color);
box-shadow: 0 0 0 3px rgba(107, 72, 255, 0.1);
}
/* Buttons Styling */
.button-group {
display: flex;
flex-wrap: wrap;
gap: 15px;
justify-content: center;
margin-top: 20px;
}
button {
background: var(--primary-color);
color: white;
padding: 12px 24px;
border: none;
border-radius: var(--border-radius);
cursor: pointer;
font-size: 16px;
font-weight: 600;
transition: background var(--transition-speed) ease, transform var(--transition-speed) ease, box-shadow var(--transition-speed) ease;
text-transform: uppercase;
letter-spacing: 1px;
}
button:hover {
background: #5536FF;
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(107, 72, 255, 0.2);
}
#generate-combined-image {
background: var(--secondary-color);
}
#generate-combined-image:hover {
background: #FF9F29;
}
/* Feedback Messages */
.error-message, .success-message {
padding: 15px;
border-radius: var(--border-radius);
margin: 15px 0;
display: none;
font-size: 0.95em;
}
.error-message {
color: var(--error);
background: #FFF5F5;
border-left: 4px solid var(--error);
}
.success-message {
color: var(--success);
background: #F0FFF0;
border-left: 4px solid var(--success);
}
/* Loading Indicators */
.loading {
display: none;
text-align: center;
padding: 20px;
color: var(--primary-color);
font-weight: 600;
position: relative;
}
.loading::before {
content: '';
display: block;
width: 40px;
height: 40px;
margin: 0 auto 10px;
border: 4px solid var(--primary-color);
border-top: 4px solid transparent;
border-radius: 50%;
animation: spin 1s linear infinite;
}
.loading::after {
content: attr(data-message);
display: block;
font-size: 14px;
margin-top: 8px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
/* Story and Image Containers */
#story-preview, #combined-image-container {
background: var(--card-bg);
padding: 30px;
border-radius: 16px;
margin: 30px 0;
box-shadow: 0 8px 24px var(--shadow);
display: none;
animation: fadeIn 0.5s ease;
}
#story-preview p {
white-space: pre-wrap;
}
#combined-comic-image {
width: 100%;
max-width: 1024px;
border: 3px solid var(--primary-color);
border-radius: 12px;
transition: transform var(--transition-speed) ease;
}
#combined-comic-image:hover {
transform: scale(1.02);
}
/* Download Button */
#download-image {
background: var(--success);
margin-top: 15px;
}
#download-image:hover {
background: #28a745;
}
/* Responsive Design */
@media (max-width: 768px) {
body {
padding: 10px;
}
.input-section {
padding: 20px;
}
h1 {
font-size: 2em;
}
button {
width: 100%;
}
}
</style>
</head>
<body>
<main>
<!-- Input Section -->
<section class="input-section" aria-labelledby="app-title">
<h1 id="app-title">Comic Strip Creator</h1>
<form id="comic-form" novalidate>
<div class="character-inputs">
<!-- Character 1 Inputs -->
<div class="character-1">
<h2>Character 1</h2>
<div class="input-group">
<label for="char1-name">Name:</label>
<input type="text" id="char1-name" name="char1-name" required placeholder="Enter character name" aria-required="true">
</div>
<div class="input-group">
<label for="char1-description">Description:</label>
<textarea id="char1-description" name="char1-description" rows="4" required placeholder="Describe your character's appearance and personality" aria-required="true"></textarea>
</div>
</div>
<!-- Character 2 Inputs -->
<div class="character-2">
<h2>Character 2</h2>
<div class="input-group">
<label for="char2-name">Name:</label>
<input type="text" id="char2-name" name="char2-name" required placeholder="Enter character name" aria-required="true">
</div>
<div class="input-group">
<label for="char2-description">Description:</label>
<textarea id="char2-description" name="char2-description" rows="4" required placeholder="Describe your character's appearance and personality" aria-required="true"></textarea>
</div>
</div>
</div>
<!-- Story Outline -->
<div class="input-group">
<label for="story-outline">Story Outline:</label>
<textarea id="story-outline" name="story-outline" rows="4" required placeholder="Write your story outline here" aria-required="true"></textarea>
</div>
<!-- Scene Description -->
<div class="input-group">
<label for="scene-description">Scene Description:</label>
<textarea id="scene-description" name="scene-description" rows="4" required placeholder="Describe the scene and setting" aria-required="true"></textarea>
</div>
<!-- Art Style Selection -->
<div class="input-group">
<label for="art-style">Art Style:</label>
<select id="art-style" name="art-style" required aria-required="true">
<option value="" disabled selected>Select an art style</option>
<option value="manga">Manga</option>
<option value="comic">Comic Book</option>
<option value="realistic">Realistic</option>
<option value="cartoon">Cartoon</option>
<option value="watercolor">Watercolor</option>
<option value="whimsical">Whimsical Storybook</option>
<option value="pop">Pop Art</option>
<option value="sketch">Detailed Sketch</option>
</select>
</div>
<!-- Feedback Messages -->
<div id="error-message" class="error-message" role="alert"></div>
<div id="success-message" class="success-message" role="status"></div>
<!-- Buttons -->
<div class="button-group">
<button type="button" id="generate-story">Generate Story</button>
<button type="button" id="generate-combined-image" style="display: none;">Generate Comic Strip</button>
</div>
</form>
</section>
<!-- Story Preview -->
<section id="story-preview" aria-live="polite">
<h2>Generated Story Panels:</h2>
<p></p>
</section>
<!-- Combined Comic Image -->
<section id="combined-image-container" aria-live="polite">
<h2>Your Comic Strip:</h2>
<img id="combined-comic-image" src="" alt="Combined Comic Strip">
<button id="download-image">Download Comic Strip</button>
</section>
<!-- Loading Indicators -->
<div id="loading-story" class="loading" data-message="Creating your story..."></div>
<div id="loading-image" class="loading" data-message="Crafting your comic panels..."></div>
</main>
<script>
// Utility Function for Displaying Messages
const displayMessage = (element, message, type) => {
element.textContent = message;
element.style.display = 'block';
element.className = type === 'error' ? 'error-message' : 'success-message';
setTimeout(() => {
element.style.display = 'none';
}, 5000);
};
// Validate Form Inputs
const validateForm = () => {
const form = document.getElementById('comic-form');
const requiredFields = form.querySelectorAll('[required]');
let isValid = true;
requiredFields.forEach(field => {
if (!field.value.trim()) {
isValid = false;
field.classList.add('input-error');
} else {
field.classList.remove('input-error');
}
});
return isValid;
};
// Fetch API Wrapper with Error Handling
const fetchAPI = async (url, options) => {
try {
const response = await fetch(url, options);
if (!response.ok) throw new Error(`Error: ${response.status} ${response.statusText}`);
return response;
} catch (error) {
throw error;
}
};
// Generate Story Function
const generateStory = async () => {
const errorMessage = document.getElementById('error-message');
const successMessage = document.getElementById('success-message');
const loading = document.getElementById('loading-story');
const storyPreview = document.getElementById('story-preview');
const generateImageBtn = document.getElementById('generate-combined-image');
errorMessage.style.display = 'none';
successMessage.style.display = 'none';
storyPreview.style.display = 'none';
generateImageBtn.style.display = 'none';
if (!validateForm()) {
displayMessage(errorMessage, 'Please fill in all required fields correctly.', 'error');
return;
}
loading.style.display = 'block';
try {
const char1Name = document.getElementById('char1-name').value.trim();
const char1Desc = document.getElementById('char1-description').value.trim();
const char2Name = document.getElementById('char2-name').value.trim();
const char2Desc = document.getElementById('char2-description').value.trim();
const storyOutline = document.getElementById('story-outline').value.trim();
const sceneDesc = document.getElementById('scene-description').value.trim();
const artStyle = document.getElementById('art-style').value;
const prompt = `Create a 4-panel comic strip story with characters: ${char1Name} (${char1Desc}) and ${char2Name} (${char2Desc}). Story outline: ${storyOutline}. Scene: ${sceneDesc}. Style: ${artStyle}. Break it into 4 distinct scenes without dialogue.`;
const response = await fetchAPI('https://text.pollinations.ai/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
messages: [
{ role: 'system', content: 'You are a comic strip writer. Create visual scene brief descriptions without dialogue.' },
{ role: 'user', content: prompt }
],
model: 'openai',
jsonMode: false
}),
});
const data = await response.text();
// Display the generated story
storyPreview.querySelector('p').innerHTML = data.replace(/\n/g, '<br>');
storyPreview.style.display = 'block';
generateImageBtn.style.display = 'inline-block';
window.storyData = data; // Store story data globally
displayMessage(successMessage, 'Story generated successfully!', 'success');
} catch (error) {
displayMessage(errorMessage, error.message || 'Failed to generate story. Please try again.', 'error');
console.error('Error:', error);
} finally {
loading.style.display = 'none';
}
};
// Generate Combined Image Function
const generateCombinedImage = () => {
const loadingImage = document.getElementById('loading-image');
const combinedImageContainer = document.getElementById('combined-image-container');
const combinedComicImage = document.getElementById('combined-comic-image');
const errorMessage = document.getElementById('error-message');
const successMessage = document.getElementById('success-message');
errorMessage.style.display = 'none';
successMessage.style.display = 'none';
combinedImageContainer.style.display = 'none';
try {
const artStyle = document.getElementById('art-style').value;
const char1Name = document.getElementById('char1-name').value.trim();
const char2Name = document.getElementById('char2-name').value.trim();
const scenes = window.storyData.split('\n').filter(scene => scene.trim()).slice(0, 4);
if (scenes.length < 4) {
throw new Error('Insufficient number of scenes to create a 4-panel comic.');
}
const prompt = `Create a single image containing a 4-panel strip arranged in a 2x2 grid. Each panel should depict the following scenes featuring ${char1Name} and ${char2Name} in the ${artStyle} style:
Panel 1: ${scenes[0]}
Panel 2: ${scenes[1]}
Panel 3: ${scenes[2]}
Panel 4: ${scenes[3]}
Ensure consistent character designs, no text and dialogues, and seamless transitions between panels, maintaining the ${artStyle} aesthetic throughout the comic strip.`;
const encodedPrompt = encodeURIComponent(prompt);
const imageUrl = `https://image.pollinations.ai/prompt/${encodedPrompt}?model=flux&width=1024&height=1024&nologo=true&private=true`;
// Display the loading indicator
loadingImage.style.display = 'block';
// Once the image is loaded, display it
combinedComicImage.onload = () => {
loadingImage.style.display = 'none';
combinedImageContainer.style.display = 'block';
displayMessage(successMessage, 'Comic strip generated successfully!', 'success');
};
combinedComicImage.onerror = () => {
loadingImage.style.display = 'none';
displayMessage(errorMessage, 'Failed to load the comic strip image. Please try again.', 'error');
};
// Set the image source
combinedComicImage.src = imageUrl;
} catch (error) {
displayMessage(errorMessage, error.message || 'Failed to generate the combined comic image.', 'error');
console.error('Error generating combined image:', error);
}
};
// Download Image Function
const downloadImage = () => {
const comicImage = document.getElementById('combined-comic-image');
if (comicImage.src) {
const link = document.createElement('a');
link.href = comicImage.src;
link.download = 'comic_strip.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} else {
alert('No image available to download.');
}
};
// Event Listeners
document.getElementById('generate-story').addEventListener('click', generateStory);
document.getElementById('generate-combined-image').addEventListener('click', generateCombinedImage);
document.getElementById('download-image').addEventListener('click', downloadImage);
</script>
</body>
</html>
This refined version not only enhances the visual appeal and user interaction of your Comic Strip Creator app but also ensures the codebase is maintainable and scalable for future developments. Feel free to further customize the styles and functionalities to better fit your specific requirements!