Sure! I've made the requested changes to your code:
- Fixed the download button to work correctly.
- Removed the description below the image after it loads. The description is visible before the image loads.
- Reduced the gap between images for a tighter layout.
- Added functionality to open an image in a larger view with the description on the side when clicked.
Here is the full, updated code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CreativeSpark - AI-Powered Image Creation</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
<style>
:root {
--bg-color: #0f0f0f;
--text-color: #ffffff;
--accent-color: #1e1e1e;
--hover-color: #2a2a2a;
--border-color: #333333;
--button-color: #2d2d2d;
--button-text: #9e9e9e;
--button-active: #ffffff;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: var(--bg-color);
color: var(--text-color);
line-height: 1.6;
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 0;
}
.logo {
font-size: 24px;
font-weight: bold;
display: flex;
align-items: center;
}
.logo i {
margin-right: 10px;
color: #ffffff;
}
.nav-icons {
display: flex;
gap: 20px;
}
.nav-icons i {
font-size: 18px;
color: var(--button-text);
cursor: pointer;
transition: color 0.3s ease;
}
.nav-icons i:hover {
color: var(--button-active);
}
.creation-panel {
background-color: var(--accent-color);
border-radius: 12px;
padding: 20px;
margin-top: 20px;
position: relative;
}
.prompt-input {
width: 100%;
padding: 15px 20px;
border-radius: 8px;
border: none;
background-color: transparent;
color: var(--text-color);
font-size: 16px;
outline: none;
}
.prompt-input::placeholder {
color: #6e6e6e;
}
.input-icons {
position: absolute;
right: 20px;
top: 20px;
display: flex;
gap: 15px;
}
.input-icons i {
color: var(--button-text);
cursor: pointer;
font-size: 18px;
}
.options {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
margin-top: 15px;
gap: 20px;
}
.option-group {
display: flex;
flex-direction: column;
gap: 5px;
flex: 1;
min-width: 150px;
}
.option-label {
font-size: 12px;
color: var(--button-text);
}
.option-buttons {
display: flex;
flex-wrap: wrap;
gap: 5px;
}
.option-button {
padding: 5px 10px;
border-radius: 5px;
border: none;
background-color: var(--button-color);
color: var(--button-text);
cursor: pointer;
transition: all 0.3s ease;
font-size: 12px;
}
.option-button.active {
background-color: var(--button-active);
color: var(--bg-color);
}
.generate-button {
padding: 10px 20px;
border-radius: 5px;
border: none;
background-color: var(--button-active);
color: var(--bg-color);
font-size: 14px;
cursor: pointer;
transition: background-color 0.3s ease;
position: absolute;
right: 20px;
bottom: 20px;
}
.generate-button:hover {
background-color: #e0e0e0;
}
.style-tags {
display: flex;
gap: 10px;
margin-top: 15px;
flex-wrap: wrap;
}
.style-tag {
padding: 5px 10px;
border-radius: 20px;
background-color: transparent;
color: var(--button-text);
font-size: 14px;
cursor: pointer;
transition: color 0.3s ease;
}
.style-tag:hover {
color: var(--button-active);
}
.image-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 10px;
margin-top: 30px;
}
.image-card {
position: relative;
border-radius: 10px;
overflow: hidden;
transition: transform 0.3s ease;
background-color: #1e1e1e;
}
.image-card:hover {
transform: scale(1.05);
}
.image-card img {
width: 100%;
height: auto;
display: block;
}
.image-overlay {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
opacity: 0;
transition: opacity 0.3s ease;
gap: 10px;
}
.image-card:hover .image-overlay {
opacity: 1;
}
.download-btn, .like-btn {
background-color: rgba(255, 255, 255, 0.8);
border: none;
border-radius: 50%;
width: 40px;
height: 40px;
font-size: 18px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.download-btn:hover, .like-btn:hover {
background-color: #ffffff;
}
.like-btn.liked {
color: #ff4136;
}
/* Modal Style */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1000; /* Sit on top */
padding-top: 60px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgba(0,0,0,0.9); /* Black w/ opacity */
}
.modal-content {
margin: auto;
padding: 20px;
display: flex;
max-width: 80%;
align-items: center;
justify-content: center;
color: #fff;
}
.modal-content img {
max-width: 60%;
height: auto;
border-radius: 10px;
}
#modalDescription {
margin-left: 20px;
max-width: 40%;
}
.modal-description p {
margin-bottom: 10px;
}
.close {
position: absolute;
top: 20px;
right: 45px;
color: #fff;
font-size: 40px;
font-weight: bold;
cursor: pointer;
}
/* Adjust modal content on small screens */
@media screen and (max-width: 768px) {
.options {
flex-direction: column;
gap: 15px;
}
.generate-button {
position: static;
width: 100%;
margin-top: 15px;
}
.modal-content {
flex-direction: column;
max-width: 90%;
}
.modal-content img {
max-width: 100%;
}
#modalDescription {
margin-left: 0;
margin-top: 20px;
max-width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<header>
<div class="logo">
<i class="fas fa-spark"></i> CreativeSpark
</div>
<div class="nav-icons">
<i class="fas fa-bolt" title="Upgrade plan"></i>
<i class="fas fa-home" title="Home"></i>
<i class="fas fa-bell" title="Notifications"></i>
<i class="fas fa-user" title="Profile"></i>
</div>
</header>
<div class="creation-panel">
<input type="text" class="prompt-input" placeholder="What do you want to create?">
<div class="options">
<div class="option-group">
<span class="option-label">Magic Prompt</span>
<div class="option-buttons">
<button class="option-button active" data-magic="auto">Auto</button>
<button class="option-button" data-magic="on">On</button>
<button class="option-button" data-magic="off">Off</button>
</div>
</div>
<div class="option-group">
<span class="option-label">Aspect Ratio</span>
<div class="option-buttons">
<button class="option-button active" data-ratio="9:16">9:16</button>
<button class="option-button" data-ratio="1:1">1:1</button>
<button class="option-button" data-ratio="16:9">16:9</button>
<button class="option-button" data-ratio="4:3">4:3</button>
<button class="option-button" data-ratio="3:2">3:2</button>
</div>
</div>
<div class="option-group">
<span class="option-label">Visibility</span>
<div class="option-buttons">
<button class="option-button active" data-visibility="public">Public</button>
<button class="option-button" data-visibility="private">Private</button>
</div>
</div>
<div class="option-group">
<span class="option-label">Model</span>
<div class="option-buttons">
<button class="option-button active" data-model="flux">Flux</button>
<button class="option-button" data-model="flux-realism">Flux-Realism</button>
<button class="option-button" data-model="flux-anime">Flux-Anime</button>
<button class="option-button" data-model="flux-3d">Flux-3D</button>
<button class="option-button" data-model="turbo">Turbo</button>
</div>
</div>
<div class="option-group">
<span class="option-label">Resolution</span>
<div class="option-buttons">
<button class="option-button" data-width="800" data-height="800">800x800</button>
<button class="option-button active" data-width="1024" data-height="1024">1024x1024</button>
<button class="option-button" data-width="1280" data-height="720">1280x720</button>
<button class="option-button" data-width="1920" data-height="1080">1920x1080</button>
<button class="option-button" data-width="2000" data-height="2000">2000x2000</button>
</div>
</div>
</div>
<button class="generate-button">Generate</button>
<div class="style-tags">
<span class="style-tag">photo</span>
<span class="style-tag">illustration</span>
<span class="style-tag">3d render</span>
<span class="style-tag">typography</span>
<span class="style-tag">cinematic</span>
<span class="style-tag">modern</span>
<span class="style-tag">vintage</span>
<span class="style-tag">minimalistic</span>
</div>
</div>
<div class="image-grid" id="imageGrid">
<!-- Dynamically generated image cards will be inserted here -->
</div>
</div>
<!-- Modal for enlarged image and description -->
<div id="imageModal" class="modal">
<span class="close">×</span>
<div class="modal-content">
<img id="modalImage" src="" alt="">
<div id="modalDescription" class="modal-description"></div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const generateBtn = document.querySelector('.generate-button');
const promptInput = document.querySelector('.prompt-input');
const imageGrid = document.getElementById('imageGrid');
const optionButtons = document.querySelectorAll('.option-button');
const styleTags = document.querySelectorAll('.style-tag');
let selectedRatio = '9:16';
let magicPrompt = 'auto';
let visibility = 'public';
let selectedModel = 'flux';
let selectedWidth = 1024;
let selectedHeight = 1024;
let imageCount = 0;
// Handle option button selections
optionButtons.forEach(button => {
button.addEventListener('click', () => {
const parentGroup = button.parentElement.parentElement;
parentGroup.querySelectorAll('.option-button').forEach(btn => btn.classList.remove('active'));
button.classList.add('active');
// Update parameters based on the group
if (button.dataset.ratio) {
selectedRatio = button.dataset.ratio;
[selectedWidth, selectedHeight] = getRatioDimensions(selectedRatio);
} else if (button.dataset.magic) {
magicPrompt = button.dataset.magic;
} else if (button.dataset.visibility) {
visibility = button.dataset.visibility;
} else if (button.dataset.model) {
selectedModel = button.dataset.model;
} else if (button.dataset.width && button.dataset.height) {
selectedWidth = button.dataset.width;
selectedHeight = button.dataset.height;
}
});
});
// Handle style tag clicks
styleTags.forEach(tag => {
tag.addEventListener('click', () => {
promptInput.value += ' ' + tag.textContent;
});
});
// Handle Generate button click
generateBtn.addEventListener('click', () => {
const prompt = promptInput.value.trim();
if (prompt) {
generateImages(prompt);
}
});
// Function to generate images
function generateImages(prompt) {
const fragment = document.createDocumentFragment();
for (let i = 1; i <= 2; i++) {
imageCount++;
const seed = generateSeed();
const enhancedPrompt = enhancePrompt(prompt);
const encodedPrompt = encodeURIComponent(enhancedPrompt);
const imageUrl = `https://pollinations.ai/p/${encodedPrompt}?width=${selectedWidth}&height=${selectedHeight}&seed=${seed}&model=${selectedModel}&nofeed=true&nologo=true`;
const card = createImageCard(imageUrl, enhancedPrompt, selectedWidth, selectedHeight, seed);
fragment.insertBefore(card, fragment.firstChild);
}
imageGrid.insertBefore(fragment, imageGrid.firstChild);
}
// Function to enhance prompt to ~50 words if short
function enhancePrompt(prompt) {
const wordCount = prompt.split(' ').length;
if (wordCount >= 50) return prompt;
const details = "in high detail, vivid colors, professional lighting, photorealistic, artistic composition, dynamic perspective, intricate textures, captivating mood, sophisticated style";
return `${prompt} ${details}`;
}
// Function to generate a random seed
function generateSeed() {
return Math.floor(Math.random() * 1000000);
}
// Function to get width and height based on aspect ratio
function getRatioDimensions(ratio) {
const aspectRatios = {
"9:16": [576, 1024],
"1:1": [1024, 1024],
"16:9": [1024, 576],
"4:3": [800, 600],
"3:2": [900, 600],
};
return aspectRatios[ratio] || [1024, 1024];
}
// Function to create an image card
function createImageCard(imageUrl, prompt, width, height, seed) {
const card = document.createElement('div');
card.className = 'image-card';
const img = document.createElement('img');
img.src = imageUrl;
img.alt = prompt;
img.crossOrigin = "anonymous"; // For download functionality
const overlay = document.createElement('div');
overlay.className = 'image-overlay';
overlay.innerHTML = `
<button class="download-btn" title="Download"><i class="fas fa-download"></i></button>
<button class="like-btn" title="Like"><i class="far fa-heart"></i></button>
`;
const descriptionDiv = document.createElement('div');
descriptionDiv.style = 'padding: 10px; font-size: 12px; background-color: #1e1e1e;';
descriptionDiv.innerHTML = `
<p><strong>Prompt:</strong> ${prompt}</p>
<p><strong>Resolution:</strong> ${width}x${height}</p>
<p><strong>Seed:</strong> ${seed} <i style="font-size: 10px;">Each seed generates a new image variation</i></p>
<p><strong>Model:</strong> ${selectedModel}</p>
`;
// Remove the description after image loads
img.addEventListener('load', () => {
if (descriptionDiv.parentNode) {
descriptionDiv.parentNode.removeChild(descriptionDiv);
}
});
// Open modal on image click
img.addEventListener('click', () => {
openModal(imageUrl, prompt, width, height, seed);
});
card.appendChild(img);
card.appendChild(overlay);
card.appendChild(descriptionDiv);
return card;
}
// Function to open modal with image and description
function openModal(imageUrl, prompt, width, height, seed) {
const modal = document.getElementById('imageModal');
const modalImg = document.getElementById('modalImage');
const modalDesc = document.getElementById('modalDescription');
modal.style.display = 'block';
modalImg.src = imageUrl;
modalImg.alt = prompt;
modalDesc.innerHTML = `
<p><strong>Prompt:</strong> ${prompt}</p>
<p><strong>Resolution:</strong> ${width}x${height}</p>
<p><strong>Seed:</strong> ${seed} <i style="font-size: 10px;">Each seed generates a new image variation</i></p>
<p><strong>Model:</strong> ${selectedModel}</p>
`;
}
// Close modal functionality
const modal = document.getElementById('imageModal');
const closeBtn = document.querySelector('.close');
closeBtn.addEventListener('click', () => {
modal.style.display = 'none';
});
window.addEventListener('click', (event) => {
if (event.target === modal) {
modal.style.display = 'none';
}
});
// Event delegation for download and like buttons
imageGrid.addEventListener('click', (e) => {
if (e.target.closest('.download-btn')) {
const img = e.target.closest('.image-card').querySelector('img');
const prompt = img.alt || 'generated-image';
downloadImage(img.src, prompt.replace(/\s+/g, '-').toLowerCase() + '.jpg');
} else if (e.target.closest('.like-btn')) {
const likeBtn = e.target.closest('.like-btn');
likeBtn.classList.toggle('liked');
likeBtn.querySelector('i').classList.toggle('fas');
likeBtn.querySelector('i').classList.toggle('far');
}
});
// Function to download an image
function downloadImage(url, filename) {
const img = new Image();
img.crossOrigin = 'anonymous';
img.src = url;
img.onload = function() {
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');
const link = document.createElement('a');
link.href = dataURL;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
img.onerror = function() {
alert('Failed to download image due to CORS restrictions.');
};
}
});
</script>
</body>
</html>
Notes on the Changes:
-
Download Button Functionality:
- I've modified the
downloadImage function to handle cross-origin images by drawing them onto a canvas and then triggering a download. This should make the download button work correctly.
-
Removing Description After Image Loads:
- Within the
createImageCard function, an event listener is added to each image to remove the description div after the image has fully loaded.
-
Reducing Gap Between Images:
- Updated the CSS
.image-grid class by changing gap: 20px; to gap: 10px; to decrease the space between images.
-
Image Modal with Description:
- Added a modal in the HTML structure.
- Styled the modal in the CSS.
- Implemented the JavaScript functions
openModal to display the modal and populate it with the image and its description when an image is clicked.
- The modal can be closed by clicking the close button or clicking outside the modal content.
All other features from the original code remain intact. The overall look is preserved, and the new functionalities should enhance the user experience as per your requirements.
Feel free to integrate this code into your project, and let me know if you need any further adjustments!