File size: 8,205 Bytes
5fb9103 | 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 | document.addEventListener('DOMContentLoaded', function() {
// DOM Elements
const dropZone = document.getElementById('dropZone');
const fileInput = document.getElementById('fileInput');
const convertBtn = document.getElementById('convertBtn');
const previewContainer = document.getElementById('previewContainer');
const emptyPreview = document.getElementById('emptyPreview');
const downloadButtons = document.getElementById('downloadButtons');
const modeBtns = document.querySelectorAll('.mode-btn');
const downloadBtns = document.querySelectorAll('.download-btn');
// Settings elements
const colorCount = document.getElementById('colorCount');
const colorCountValue = document.getElementById('colorCountValue');
const curveTolerance = document.getElementById('curveTolerance');
const curveToleranceValue = document.getElementById('curveToleranceValue');
const pathSimplify = document.getElementById('pathSimplify');
const pathSimplifyValue = document.getElementById('pathSimplifyValue');
const removeBackground = document.getElementById('removeBackground');
// State
let currentFile = null;
let currentMode = 'logo';
let convertedSVG = null;
// Event Listeners
dropZone.addEventListener('click', () => fileInput.click());
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('drop-zone-active');
});
dropZone.addEventListener('dragleave', () => {
dropZone.classList.remove('drop-zone-active');
});
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('drop-zone-active');
if (e.dataTransfer.files.length) {
handleFileUpload(e.dataTransfer.files[0]);
}
});
fileInput.addEventListener('change', () => {
if (fileInput.files.length) {
handleFileUpload(fileInput.files[0]);
}
});
modeBtns.forEach(btn => {
btn.addEventListener('click', () => {
currentMode = btn.dataset.mode;
modeBtns.forEach(b => b.classList.remove('border-blue-500', 'bg-blue-100'));
btn.classList.add('border-blue-500', 'bg-blue-100');
});
});
// Settings sliders
colorCount.addEventListener('input', () => {
colorCountValue.textContent = colorCount.value;
});
curveTolerance.addEventListener('input', () => {
curveToleranceValue.textContent = curveTolerance.value;
});
pathSimplify.addEventListener('input', () => {
pathSimplifyValue.textContent = pathSimplify.value;
});
convertBtn.addEventListener('click', convertToVector);
downloadBtns.forEach(btn => {
btn.addEventListener('click', () => {
if (!convertedSVG) return;
const format = btn.dataset.format;
if (format === 'copy') {
navigator.clipboard.writeText(convertedSVG)
.then(() => showToast('SVG copied to clipboard!', 'success'))
.catch(() => showToast('Failed to copy SVG', 'error'));
} else {
downloadVector(format);
}
});
});
// Functions
function handleFileUpload(file) {
// Validate file
const validTypes = ['image/jpeg', 'image/jpg', 'image/png'];
if (!validTypes.includes(file.type)) {
showToast('Please upload a JPG or PNG image', 'error');
return;
}
if (file.size > 25 * 1024 * 1024) { // 25MB
showToast('File is too large (max 25MB)', 'error');
return;
}
currentFile = file;
// Create preview
const reader = new FileReader();
reader.onload = (e) => {
emptyPreview.style.display = 'none';
previewContainer.innerHTML = `
<div class="image-preview">
<img src="${e.target.result}" alt="Preview" id="originalImage">
</div>
`;
};
reader.readAsDataURL(file);
}
function convertToVector() {
if (!currentFile) {
showToast('Please upload an image first', 'error');
return;
}
// Simulate conversion (in a real app, this would use WASM or API)
showToast('Converting to vector...', 'info');
convertBtn.disabled = true;
convertBtn.innerHTML = '<span class="loading-spinner"></span> Processing...';
// Fake delay to simulate processing
setTimeout(() => {
convertedSVG = `<svg>...</svg>`; // In a real app, this would be the actual SVG
// Show download buttons
downloadButtons.style.display = 'grid';
// Update preview to show split view
const originalSrc = document.getElementById('originalImage').src;
previewContainer.innerHTML = `
<div class="split-view">
<div class="original">
<img src="${originalSrc}" alt="Original">
</div>
<div class="divider">
<div class="divider-handle">
<i data-feather="chevrons-right" class="w-3 h-3"></i>
</div>
</div>
<div class="vector">
<img src="data:image/svg+xml;base64,${btoa(convertedSVG)}" alt="Vector">
</div>
</div>
`;
feather.replace();
setupSplitViewDrag();
showToast('Conversion complete!', 'success');
convertBtn.disabled = false;
convertBtn.innerHTML = '<i data-feather="wand" class="w-5 h-5 mr-2"></i> Convert to Vector';
}, 2000);
}
function setupSplitViewDrag() {
const divider = document.querySelector('.divider');
const handle = document.querySelector('.divider-handle');
const container = document.querySelector('.split-view');
let isDragging = false;
handle.addEventListener('mousedown', () => {
isDragging = true;
document.body.style.cursor = 'ew-resize';
});
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const containerRect = container.getBoundingClientRect();
const percent = (e.clientX - containerRect.left) / containerRect.width;
if (percent > 0.2 && percent < 0.8) {
container.style.gridTemplateColumns = `${percent * 100}% 2px ${(1 - percent) * 100}%`;
}
});
document.addEventListener('mouseup', () => {
isDragging = false;
document.body.style.cursor = '';
});
}
function downloadVector(format) {
// In a real app, this would generate the appropriate format
showToast(`Preparing ${format.toUpperCase()} download...`, 'info');
setTimeout(() => {
const a = document.createElement('a');
a.href = URL.createObjectURL(new Blob([convertedSVG], { type: 'text/plain' }));
a.download = `vectorized.${format}`;
a.click();
showToast(`${format.toUpperCase()} downloaded!`, 'success');
}, 500);
}
function showToast(message, type) {
const toast = document.createElement('div');
toast.className = `fixed bottom-4 right-4 px-4 py-2 rounded-md shadow-md text-white ${
type === 'error' ? 'bg-red-500' :
type === 'success' ? 'bg-green-500' :
'bg-blue-500'
}`;
toast.textContent = message;
document.body.appendChild(toast);
setTimeout(() => {
toast.classList.add('opacity-0', 'transition-opacity', 'duration-300');
setTimeout(() => toast.remove(), 300);
}, 3000);
}
}); |