Download script.js from pranto077/pixel-to-vector-magic-wand: direct link, hf CLI and curl.
- Browser
- Download file 8.21 kB
-
https://huggingface.co/spaces/pranto077/pixel-to-vector-magic-wand/resolve/main/script.js
- Command line
-
hf download hf://spaces/pranto077/pixel-to-vector-magic-wand/script.js
-
curl -L -o script.js https://huggingface.co/spaces/pranto077/pixel-to-vector-magic-wand/resolve/main/script.js
8.21 kB
| 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); | |
| } | |
| }); |