Spaces:
Running
Running
File size: 5,972 Bytes
ad2644a f704523 ad2644a f704523 ad2644a f704523 ad2644a f704523 ad2644a f704523 ad2644a f704523 ad2644a f704523 ad2644a f704523 ad2644a f704523 ad2644a f704523 |
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 |
document.addEventListener('DOMContentLoaded', function() {
const uploadArea = document.querySelector('.border-dashed');
const fileInput = document.getElementById('image-upload');
const previewBtn = document.getElementById('preview-btn');
const downloadBtn = document.getElementById('download-btn');
const previewContainer = document.getElementById('preview-container');
const animationStyle = document.getElementById('animation-style');
const promptContainer = document.getElementById('prompt-container');
const animationPrompt = document.getElementById('animation-prompt');
const intensitySlider = document.querySelector('input[type="range"]');
const speedSlider = document.querySelectorAll('input[type="range"]')[1];
let currentImage = null;
let currentAnimation = null;
let animationFrames = [];
let frameIndex = 0;
let animationInterval;
// Show/hide prompt based on selection
animationStyle.addEventListener('change', function() {
if (this.value === 'custom') {
promptContainer.classList.remove('hidden');
} else {
promptContainer.classList.add('hidden');
}
});
// Handle drag and drop
uploadArea.addEventListener('click', () => fileInput.click());
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('bg-gray-700');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('bg-gray-700');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('bg-gray-700');
if (e.dataTransfer.files.length) {
handleFileUpload(e.dataTransfer.files[0]);
}
});
fileInput.addEventListener('change', (e) => {
if (e.target.files.length) {
handleFileUpload(e.target.files[0]);
}
});
function handleFileUpload(file) {
if (!file.type.match('image.*')) {
alert('Please upload an image file');
return;
}
const reader = new FileReader();
reader.onload = function(e) {
previewContainer.innerHTML = `<img src="${e.target.result}" alt="Uploaded preview" class="max-h-full max-w-full">`;
currentImage = e.target.result;
downloadBtn.disabled = false;
};
reader.readAsDataURL(file);
}
previewBtn.addEventListener('click', function() {
if (!currentImage) {
alert('Please upload an image first');
return;
}
const img = previewContainer.querySelector('img');
if (img) {
// Generate animation based on settings
const intensity = parseInt(intensitySlider.value);
const speed = parseInt(speedSlider.value);
const prompt = animationStyle.value === 'custom' ? animationPrompt.value : animationStyle.options[animationStyle.selectedIndex].text;
animationFrames = generateAnimationFrames(
currentImage,
prompt,
animationStyle.value,
intensity,
speed
);
playAnimation(animationFrames, speed);
// Enable download button
downloadBtn.disabled = false;
}
});
// Update animation when sliders change
intensitySlider.addEventListener('input', function() {
if (animationFrames.length > 0) {
const speed = parseInt(speedSlider.value);
animationFrames = generateAnimationFrames(
currentImage,
animationPrompt.value,
animationStyle.value,
parseInt(this.value),
speed
);
}
});
speedSlider.addEventListener('input', function() {
if (animationFrames.length > 0) {
const speed = parseInt(this.value);
playAnimation(animationFrames, speed);
}
});
function generateAnimationFrames(imageSrc, prompt, type, intensity = 5, speed = 5) {
// This would call an API in real implementation
// For demo, we generate basic frame transformations
const frames = [];
const frameCount = 10; // Number of animation frames
const intensityFactor = intensity / 5;
const speedFactor = speed / 5;
for (let i = 0; i < frameCount; i++) {
const progress = i / frameCount;
const rotation = Math.sin(progress * Math.PI * 2) * 5 * intensityFactor;
const scale = 1 + (Math.sin(progress * Math.PI) * 0.05 * intensityFactor);
const translateX = Math.sin(progress * Math.PI * 2) * 10 * intensityFactor;
const translateY = Math.sin(progress * Math.PI * 1.5) * 5 * intensityFactor;
frames.push({
transform: `rotate(${rotation}deg) scale(${scale}) translate(${translateX}px, ${translateY}px)`
});
}
return frames;
}
function playAnimation(frames, speed) {
clearInterval(animationInterval);
const img = previewContainer.querySelector('img');
if (!img) return;
frameIndex = 0;
const frameDelay = 1000 / (speed * 2); // Adjust speed based on slider
animationInterval = setInterval(() => {
img.style.transform = frames[frameIndex].transform;
frameIndex = (frameIndex + 1) % frames.length;
}, frameDelay);
}
downloadBtn.addEventListener('click', function() {
if (!currentImage) {
alert('No image to download');
return;
}
// In a real app, you would generate the animated GIF here
alert('Animation download would be generated here in a full implementation');
});
}); |