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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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497

document.addEventListener('DOMContentLoaded', function() {
    // DOM Elements
    const spinner = document.getElementById('spinner');
    const animationContainer = document.getElementById('animation-container');
    const playBtn = document.getElementById('play-btn');
    const spinnerType = document.getElementById('spinner-type');
    const spinnerColor = document.getElementById('spinner-color');
    const spinnerColor2 = document.getElementById('spinner-color-2');
    const spinnerSize = document.getElementById('spinner-size');
    const sizeValue = document.getElementById('size-value');
    const spinnerSpeed = document.getElementById('spinner-speed');
    const speedValue = document.getElementById('speed-value');
    const animationStyle = document.getElementById('animation-style');
    const customSvgUpload = document.getElementById('custom-svg-upload');
    const svgUpload = document.getElementById('svg-upload');
    const particleControls = document.getElementById('particle-controls');
    const particleCount = document.getElementById('particle-count');
    const particleSize = document.getElementById('particle-size');
    
    // Export buttons
    const exportCssBtn = document.getElementById('export-css');
    const exportHtmlBtn = document.getElementById('export-html');
    const exportJsBtn = document.getElementById('export-js');
    const exportReactBtn = document.getElementById('export-react');
    const exportVueBtn = document.getElementById('export-vue');
    const exportGifBtn = document.getElementById('export-gif');
    const exportSvgBtn = document.getElementById('export-svg');
    const exportLottieBtn = document.getElementById('export-lottie');
    
    const animationCards = document.querySelectorAll('.animation-card');
    // Current animation state
    let currentAnimation = 'circle';
    let isPlaying = true;
    let customSvgPath = null;
    let animationTimeline = null;
    // Initialize
    updateSizeDisplay();
    updateSpeedDisplay();
    feather.replace();
    
    // Setup Konva stage for advanced animations
    const stage = new Konva.Stage({
        container: animationContainer,
        width: animationContainer.clientWidth,
        height: animationContainer.clientHeight
    });
    
    const layer = new Konva.Layer();
    stage.add(layer);
    // Event Listeners
    spinnerType.addEventListener('change', function() {
        updateSpinnerType();
        toggleAdvancedControls();
    });
    
    spinnerColor.addEventListener('input', updateSpinnerColor);
    spinnerColor2.addEventListener('input', updateSpinnerColor);
    spinnerSize.addEventListener('input', updateSpinnerSize);
    spinnerSpeed.addEventListener('input', updateSpinnerSpeed);
    animationStyle.addEventListener('change', updateAnimationStyle);
    svgUpload.addEventListener('change', handleSvgUpload);
    particleCount.addEventListener('input', updateParticles);
    particleSize.addEventListener('input', updateParticles);
    
    playBtn.addEventListener('click', toggleAnimation);
    
    exportCssBtn.addEventListener('click', exportCSS);
    exportHtmlBtn.addEventListener('click', exportHTML);
    exportJsBtn.addEventListener('click', exportJS);
    exportReactBtn.addEventListener('click', exportReact);
    exportVueBtn.addEventListener('click', exportVue);
    exportGifBtn.addEventListener('click', exportGIF);
    exportSvgBtn.addEventListener('click', exportSVG);
    exportLottieBtn.addEventListener('click', exportLottie);
// Add click events to gallery items
    animationCards.forEach(card => {
        card.addEventListener('click', function() {
            const type = this.getAttribute('data-type');
            spinnerType.value = type;
            updateSpinnerType();
        });
    });
    // Toggle advanced controls based on selected animation type
    function toggleAdvancedControls() {
        customSvgUpload.classList.add('hidden');
        particleControls.classList.add('hidden');
        
        if (currentAnimation === 'custom') {
            customSvgUpload.classList.remove('hidden');
        } else if (currentAnimation === 'particles') {
            particleControls.classList.remove('hidden');
        }
    }
    
    // Handle SVG upload
    function handleSvgUpload(e) {
        const file = e.target.files[0];
        if (!file) return;
        
        const reader = new FileReader();
        reader.onload = function(event) {
            customSvgPath = event.target.result;
            updateSpinnerType();
        };
        reader.readAsText(file);
    }
    
    // Update particle animation
    function updateParticles() {
        if (currentAnimation === 'particles') {
            updateSpinnerType();
        }
    }
    
    // Update animation easing style
    function updateAnimationStyle() {
        if (animationTimeline) {
            animationTimeline.easing(animationStyle.value);
        }
    }
    
    // Main function to update spinner type
    function updateSpinnerType() {
currentAnimation = spinnerType.value;
        animationContainer.innerHTML = '';
        
        let newSpinner;
        switch(currentAnimation) {
            case 'circle':
                newSpinner = document.createElement('div');
                newSpinner.className = 'rounded-full border-4 border-t-transparent animate-spin';
                newSpinner.style.borderColor = spinnerColor.value;
                newSpinner.style.width = `${spinnerSize.value}px`;
                newSpinner.style.height = `${spinnerSize.value}px`;
                newSpinner.style.animationDuration = `${spinnerSpeed.value}s`;
                break;
                
            case 'dots':
                newSpinner = document.createElement('div');
                newSpinner.className = 'flex gap-2 items-center';
                
                for (let i = 0; i < 3; i++) {
                    const dot = document.createElement('div');
                    dot.className = 'rounded-full animate-bounce';
                    dot.style.backgroundColor = spinnerColor.value;
                    dot.style.width = `${spinnerSize.value/5}px`;
                    dot.style.height = `${spinnerSize.value/5}px`;
                    dot.style.animationDuration = `${spinnerSpeed.value}s`;
                    dot.style.animationDelay = `${i * 0.2}s`;
                    newSpinner.appendChild(dot);
                }
                break;
                
            case 'bar':
                newSpinner = document.createElement('div');
                newSpinner.className = 'w-3/4 h-2 bg-gray-200 rounded-full overflow-hidden';
                
                const innerBar = document.createElement('div');
                innerBar.className = 'h-full rounded-full animate-progress';
                innerBar.style.backgroundColor = spinnerColor.value;
                innerBar.style.animationDuration = `${spinnerSpeed.value}s`;
                
                newSpinner.appendChild(innerBar);
                break;
                
            case 'pulse':
                newSpinner = document.createElement('div');
                newSpinner.className = 'rounded-full animate-pulse';
                newSpinner.style.backgroundColor = spinnerColor.value;
                newSpinner.style.width = `${spinnerSize.value}px`;
                newSpinner.style.height = `${spinnerSize.value}px`;
                newSpinner.style.animationDuration = `${spinnerSpeed.value}s`;
                break;
                
            case 'bounce':
                newSpinner = document.createElement('div');
                newSpinner.className = 'rounded-full animate-custom-bounce';
                newSpinner.style.backgroundColor = spinnerColor.value;
                newSpinner.style.width = `${spinnerSize.value}px`;
                newSpinner.style.height = `${spinnerSize.value}px`;
                newSpinner.style.animationDuration = `${spinnerSpeed.value}s`;
                break;
        }
        
        animationContainer.appendChild(newSpinner);
        spinner = newSpinner;
    }

    function updateSpinnerColor() {
        const elements = animationContainer.querySelectorAll('div');
        elements.forEach(el => {
            if (el.style.borderColor) {
                el.style.borderColor = spinnerColor.value;
            }
            if (el.style.backgroundColor) {
                el.style.backgroundColor = spinnerColor.value;
            }
        });
    }

    function updateSpinnerSize() {
        sizeValue.textContent = spinnerSize.value;
        
        const elements = animationContainer.querySelectorAll('div');
        elements.forEach(el => {
            if (currentAnimation === 'dots') {
                el.style.width = `${spinnerSize.value/5}px`;
                el.style.height = `${spinnerSize.value/5}px`;
            } else {
                if (el.style.width) el.style.width = `${spinnerSize.value}px`;
                if (el.style.height) el.style.height = `${spinnerSize.value}px`;
            }
        });
    }

    function updateSpinnerSpeed() {
        speedValue.textContent = spinnerSpeed.value;
        
        const elements = animationContainer.querySelectorAll('div');
        elements.forEach(el => {
            if (el.style.animationDuration) {
                el.style.animationDuration = `${spinnerSpeed.value}s`;
            }
        });
    }

    function updateSizeDisplay() {
        sizeValue.textContent = spinnerSize.value;
    }

    function updateSpeedDisplay() {
        speedValue.textContent = spinnerSpeed.value;
    }

    function toggleAnimation() {
        const elements = animationContainer.querySelectorAll('div');
        
        if (isPlaying) {
            elements.forEach(el => {
                el.style.animationPlayState = 'paused';
            });
            playBtn.innerHTML = '<i data-feather="play"></i> Play';
            isPlaying = false;
        } else {
            elements.forEach(el => {
                el.style.animationPlayState = 'running';
            });
            playBtn.innerHTML = '<i data-feather="pause"></i> Pause';
            isPlaying = true;
        }
        feather.replace();
    }
    function createParticleAnimation() {
        layer.destroyChildren();
        
        const count = parseInt(particleCount.value);
        const size = parseInt(particleSize.value);
        const centerX = stage.width() / 2;
        const centerY = stage.height() / 2;
        const radius = Math.min(stage.width(), stage.height()) * 0.3;
        
        const particles = [];
        
        for (let i = 0; i < count; i++) {
            const angle = (i / count) * Math.PI * 2;
            const x = centerX + Math.cos(angle) * radius;
            const y = centerY + Math.sin(angle) * radius;
            
            const particle = new Konva.Circle({
                x: x,
                y: y,
                radius: size,
                fill: spinnerColor.value,
                opacity: 0.7
            });
            
            layer.add(particle);
            particles.push(particle);
        }
        
        animationTimeline = anime.timeline({
            duration: parseInt(spinnerSpeed.value) * 1000,
            easing: animationStyle.value,
            loop: true
        });
        
        particles.forEach((particle, i) => {
            const angle = (i / particles.length) * Math.PI * 2;
            animationTimeline.add({
                targets: particle,
                x: centerX + Math.cos(angle + Math.PI) * radius,
                y: centerY + Math.sin(angle + Math.PI) * radius,
                fill: spinnerColor2.value,
                duration: parseInt(spinnerSpeed.value) * 1000,
                offset: 0
            });
        });
        
        layer.draw();
    }

    function exportCSS() {
let cssCode = '';
        
        switch(currentAnimation) {
            case 'circle':
                cssCode = `/* Circle Spinner */
.spinner {
    width: ${spinnerSize.value}px;
    height: ${spinnerSize.value}px;
    border: 4px solid ${spinnerColor.value};
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin ${spinnerSpeed.value}s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}`;
                break;
                
            case 'dots':
                cssCode = `/* Bouncing Dots */
.dots-container {
    display: flex;
    gap: 8px;
    align-items: center;
}

.dot {
    width: ${spinnerSize.value/5}px;
    height: ${spinnerSize.value/5}px;
    background-color: ${spinnerColor.value};
    border-radius: 50%;
    animation: bounce ${spinnerSpeed.value}s infinite;
}

.dot:nth-child(2) {
    animation-delay: 0.2s;
}

.dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-15px); }
}`;
                break;
                
            case 'bar':
                cssCode = `/* Progress Bar */
.progress-container {
    width: 75%;
    height: 8px;
    background-color: #e5e7eb;
    border-radius: 4px;
    overflow: hidden;
}

.progress-bar {
    height: 100%;
    background-color: ${spinnerColor.value};
    border-radius: 4px;
    animation: progress ${spinnerSpeed.value}s ease-in-out infinite;
}

@keyframes progress {
    0% { width: 0%; }
    100% { width: 100%; }
}`;
                break;
                
            case 'pulse':
                cssCode = `/* Pulse Animation */
.pulse {
    width: ${spinnerSize.value}px;
    height: ${spinnerSize.value}px;
    background-color: ${spinnerColor.value};
    border-radius: 50%;
    animation: pulse ${spinnerSpeed.value}s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); opacity: 1; }
    50% { transform: scale(0.8); opacity: 0.5; }
}`;
                break;
                
            case 'bounce':
                cssCode = `/* Bounce Animation */
.bounce {
    width: ${spinnerSize.value}px;
    height: ${spinnerSize.value}px;
    background-color: ${spinnerColor.value};
    border-radius: 50%;
    animation: customBounce ${spinnerSpeed.value}s infinite;
}

@keyframes customBounce {
    0%, 100% { transform: translateY(0) scale(1); }
    25% { transform: translateY(-20px) scale(1.1); }
    50% { transform: translateY(0) scale(1); }
    75% { transform: translateY(-10px) scale(1.05); }
}`;
                break;
        }
        
        // Create download link
        const blob = new Blob([cssCode], { type: 'text/css' });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = 'spinner-animation.css';
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
    }
    function exportHTML() {
let htmlCode = '';
        
        switch(currentAnimation) {
            case 'circle':
                htmlCode = `<div class="spinner"></div>`;
                break;
                
            case 'dots':
                htmlCode = `<div class="dots-container">
    <div class="dot"></div>
    <div class="dot"></div>
    <div class="dot"></div>
</div>`;
                break;
                
            case 'bar':
                htmlCode = `<div class="progress-container">
    <div class="progress-bar"></div>
</div>`;
                break;
                
            case 'pulse':
                htmlCode = `<div class="pulse"></div>`;
                break;
                
            case 'bounce':
                htmlCode = `<div class="bounce"></div>`;
                break;
        }
        
        // Create download link
        const blob = new Blob([htmlCode], { type: 'text/html' });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = 'spinner-animation.html';
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
    }
    function exportJS() {
        let jsCode = '';
        // Generate JavaScript code for the animation
        // This would use the animation parameters to create a vanilla JS implementation
        alert('JavaScript export would generate code using the current animation parameters');
    }

    function exportReact() {
        let reactCode = '';
        // Generate React component code for the animation
        alert('React export would generate a React component with the current animation');
    }

    function exportVue() {
        let vueCode = '';
        // Generate Vue component code for the animation
        alert('Vue export would generate a Vue component with the current animation');
    }

    function exportSVG() {
        // Generate SVG markup for the current animation
        alert('SVG export would generate SVG markup for the current animation');
    }

    function exportLottie() {
        // Generate Lottie JSON for the current animation
        alert('Lottie export would generate JSON for use with Lottie/Bodymovin');
    }

    function exportGIF() {
        // Use gif.js or similar library to capture frames and create GIF
        alert('GIF export would capture frames and generate an animated GIF');
    }
});