Spaces:
Running
Running
File size: 9,730 Bytes
7dda21d 4c0a883 7dda21d 4c0a883 7dda21d 4c0a883 7dda21d 4c0a883 7dda21d 4c0a883 7dda21d | 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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | document.addEventListener('DOMContentLoaded', function() {
// DOM Elements
const fileInput = document.getElementById('fileInput');
const uploadBtn = document.getElementById('uploadBtn');
const resetBtn = document.getElementById('resetBtn');
const saveBtn = document.getElementById('saveBtn');
const imagePreview = document.getElementById('imagePreview');
const uploadOverlay = document.getElementById('uploadOverlay');
const previewContainer = imagePreview.parentElement;
// Control elements
const hdrIntensity = document.getElementById('hdrIntensity');
const saturation = document.getElementById('saturation');
const tones = document.getElementById('tones');
const colorTemp = document.getElementById('colorTemp');
const brightness = document.getElementById('brightness');
const contrast = document.getElementById('contrast');
const quality = document.getElementById('quality');
// Value displays
const hdrValue = document.getElementById('hdrValue');
const saturationValue = document.getElementById('saturationValue');
const tonesValue = document.getElementById('tonesValue');
const colorTempValue = document.getElementById('colorTempValue');
const brightnessValue = document.getElementById('brightnessValue');
const contrastValue = document.getElementById('contrastValue');
const qualityValue = document.getElementById('qualityValue');
// Original image data
let originalImage = null;
let currentImage = null;
// Initialize event listeners
function init() {
// Upload button click
uploadBtn.addEventListener('click', () => fileInput.click());
// File input change
fileInput.addEventListener('change', handleFileSelect);
// Reset button
resetBtn.addEventListener('click', resetImage);
// Save button
saveBtn.addEventListener('click', saveImage);
// Drag and drop
previewContainer.addEventListener('dragover', handleDragOver);
previewContainer.addEventListener('dragleave', handleDragLeave);
previewContainer.addEventListener('drop', handleDrop);
// Control sliders
const sliders = [hdrIntensity, saturation, tones, colorTemp, brightness, contrast, quality];
sliders.forEach(slider => {
slider.addEventListener('input', updateSliderValue);
slider.addEventListener('input', applyFilters);
});
// Add debounce to sliders for better performance
const debouncedSliders = [hdrIntensity, saturation, tones, colorTemp];
debouncedSliders.forEach(slider => {
slider.addEventListener('input', debounce(applyFilters, 100));
});
// Initialize slider values
updateAllSliderValues();
applyFilters(); // Apply initial filter state
}
// Debounce function to limit rapid calls
function debounce(func, wait) {
let timeout;
return function() {
const context = this, args = arguments;
clearTimeout(timeout);
timeout = setTimeout(() => {
func.apply(context, args);
}, wait);
};
}
// Handle file selection
function handleFileSelect(e) {
const file = e.target.files[0] || (e.dataTransfer && e.dataTransfer.files[0]);
if (!file) return;
if (!file.type.match('image.*')) {
alert('Please select an image file.');
return;
}
const reader = new FileReader();
reader.onload = function(e) {
originalImage = e.target.result;
currentImage = originalImage;
imagePreview.src = currentImage;
};
reader.readAsDataURL(file);
// Reset the file input to allow selecting the same file again
fileInput.value = '';
}
// Handle drag over
function handleDragOver(e) {
e.preventDefault();
e.stopPropagation();
previewContainer.classList.add('drag-over');
uploadOverlay.classList.remove('hidden');
}
// Handle drag leave
function handleDragLeave(e) {
e.preventDefault();
e.stopPropagation();
previewContainer.classList.remove('drag-over');
uploadOverlay.classList.add('hidden');
}
// Handle drop
function handleDrop(e) {
e.preventDefault();
e.stopPropagation();
previewContainer.classList.remove('drag-over');
uploadOverlay.classList.add('hidden');
const dt = e.dataTransfer;
const file = dt.files[0];
// Create a mock event object for handleFileSelect
const mockEvent = {
target: { files: [file] },
dataTransfer: dt
};
handleFileSelect(mockEvent);
}
// Reset image to original
function resetImage() {
if (!originalImage) return;
currentImage = originalImage;
imagePreview.src = currentImage;
// Reset all sliders to default values
hdrIntensity.value = 50;
saturation.value = 50;
tones.value = 50;
colorTemp.value = 50;
brightness.value = 50;
contrast.value = 50;
updateAllSliderValues();
applyFilters();
}
// Update slider value displays
function updateSliderValue(e) {
const slider = e.target;
const valueElement = document.getElementById(`${slider.id}Value`);
if (valueElement) {
valueElement.textContent = `${slider.value}%`;
}
}
// Update all slider values at once
function updateAllSliderValues() {
hdrValue.textContent = `${hdrIntensity.value}%`;
saturationValue.textContent = `${saturation.value}%`;
tonesValue.textContent = `${tones.value}%`;
colorTempValue.textContent = `${colorTemp.value}%`;
brightnessValue.textContent = `${brightness.value}%`;
contrastValue.textContent = `${contrast.value}%`;
qualityValue.textContent = `${quality.value}%`;
}
// Apply filters to image
function applyFilters() {
if (!currentImage) return;
// Calculate filter values based on slider positions
const hdrIntensityValue = hdrIntensity.value;
const saturationValue = saturation.value;
const tonesValue = tones.value;
const colorTempValue = colorTemp.value;
const brightnessValue = brightness.value;
const contrastValue = contrast.value;
// Calculate temperature (warm/cool)
const tempKelvin = 6600 - (colorTempValue * 56); // Range from 1000K to 6600K
const tempRatio = tempKelvin / 6600;
const tempRed = tempRatio > 0.5 ? 1 : tempRatio * 2;
const tempBlue = tempRatio < 0.5 ? 1 : (1 - tempRatio) * 2;
// Apply all filters with proper calculations
const hdrFilter = `
contrast(${contrastValue}%)
brightness(${brightnessValue}%)
saturate(${saturationValue}%)
sepia(${(100 - tonesValue) * 0.3}%)
hue-rotate(${(colorTempValue - 50) * 3.6}deg)
drop-shadow(0 0 ${hdrIntensityValue * 0.1}px rgba(255,255,255,0.5))
`;
imagePreview.style.filter = hdrFilter;
}
// Save image with applied filters
function saveImage() {
if (!currentImage) {
alert('Please upload an image first.');
return;
}
// Show loading state
const originalText = saveBtn.innerHTML;
saveBtn.disabled = true;
saveBtn.innerHTML = '<span class="spinner"></span> Processing...';
// Simulate processing delay
setTimeout(() => {
// In a real app, you would generate the actual filtered image here
// For this demo, we'll just create a download link to the original image
const format = document.querySelector('input[name="format"]:checked').value;
const quality = document.getElementById('quality').value / 100;
// Create a canvas to draw the image (simulated processing)
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
ctx.filter = imagePreview.style.filter;
ctx.drawImage(img, 0, 0);
// Create download link
const downloadLink = document.getElementById('downloadLink');
downloadLink.innerHTML = '';
const a = document.createElement('a');
a.href = canvas.toDataURL(`image/${format}`, quality);
a.download = `hdr-photo-${Date.now()}.${format}`;
a.textContent = 'Download your HDR image';
a.className = 'text-blue-500 hover:text-blue-600 font-medium underline';
downloadLink.appendChild(a);
downloadLink.classList.remove('hidden');
// Reset button state
saveBtn.disabled = false;
saveBtn.innerHTML = originalText;
// Automatically click the download link
a.click();
};
img.src = currentImage;
}, 1500);
}
// Initialize the app
init();
}); |