hdr-master-blaster / script.js
sendax's picture
l app non funziona controlla
4c0a883 verified
Raw History Blame Contribute Delete
9.73 kB
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();
});