File size: 14,666 Bytes
ecc1f3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
// Main application logic
document.addEventListener('DOMContentLoaded', function() {
    // Style selection
    const styleButtons = document.querySelectorAll('.style-btn');
    let selectedStyle = 'realistic';
    
    styleButtons.forEach(btn => {
        btn.addEventListener('click', () => {
            styleButtons.forEach(b => b.classList.remove('active'));
            btn.classList.add('active');
            selectedStyle = btn.dataset.style;
        });
    });
    
    // Set default active style
    document.querySelector('[data-style="realistic"]').classList.add('active');
    
    // Generate button functionality
    const generateBtn = document.getElementById('generate-btn');
    const textInput = document.getElementById('text-input');
    const progressContainer = document.getElementById('progress-container');
    const progressBar = document.getElementById('progress-bar');
    const progressPercent = document.getElementById('progress-percent');
    const previewContainer = document.getElementById('preview-container');
    const canvas = document.getElementById('3d-canvas');
    const actionButtons = document.getElementById('action-buttons');
    const renderTime = document.getElementById('render-time');
    
    generateBtn.addEventListener('click', async () => {
        const text = textInput.value.trim();
        
        if (!text) {
            showNotification('Please enter a description for your 3D design', 'warning');
            return;
        }
        
        // Show progress
        progressContainer.classList.remove('hidden');
        generateBtn.disabled = true;
        generateBtn.classList.add('opacity-50', 'cursor-not-allowed');
        
        // Simulate generation progress
        let progress = 0;
        const startTime = Date.now();
        
        const progressInterval = setInterval(() => {
            progress += Math.random() * 15;
            if (progress > 90) progress = 90;
            
            progressBar.style.width = `${progress}%`;
            progressPercent.textContent = `${Math.round(progress)}%`;
        }, 200);
        
        // Simulate API call
        setTimeout(() => {
            clearInterval(progressInterval);
            progressBar.style.width = '100%';
            progressPercent.textContent = '100%';
            
            // Generate 3D visualization
            generate3DVisualization(text, selectedStyle);
            
            // Calculate render time
            const endTime = Date.now();
            const renderDuration = ((endTime - startTime) / 1000).toFixed(1);
            renderTime.textContent = `Rendered in ${renderDuration}s`;
            
            // Show result
            setTimeout(() => {
                progressContainer.classList.add('hidden');
                generateBtn.disabled = false;
                generateBtn.classList.remove('opacity-50', 'cursor-not-allowed');
                actionButtons.classList.remove('hidden');
                canvas.classList.remove('hidden');
                previewContainer.querySelector('.text-center').classList.add('hidden');
                
                // Add to recent creations
                addToRecentCreations(text);
            }, 500);
        }, 3000);
    });
    // Enhanced 3D Visualization
    function generate3DVisualization(description, style) {
        const ctx = canvas.getContext('2d');
        canvas.width = canvas.offsetWidth;
        canvas.height = canvas.offsetHeight;
        
        // Store the visualization data for animation
        canvas.dataset.description = description;
        canvas.dataset.style = style;
        canvas.dataset.rotation = '0';
        
        draw3DModel(ctx, canvas.width, canvas.height, style, 0);
    }
    
    function draw3DModel(ctx, width, height, style, rotation) {
        // Clear canvas
        ctx.clearRect(0, 0, width, height);
        
        // Create gradient background based on style
        let gradient;
        switch(style) {
            case 'cartoon':
                gradient = ctx.createLinearGradient(0, 0, width, height);
                gradient.addColorStop(0, '#fef3c7');
                gradient.addColorStop(0.5, '#fed7aa');
                gradient.addColorStop(1, '#fbbf24');
                break;
            case 'minimalist':
                gradient = ctx.createLinearGradient(0, 0, width, height);
                gradient.addColorStop(0, '#f3f4f6');
                gradient.addColorStop(0.5, '#e5e7eb');
                gradient.addColorStop(1, '#d1d5db');
                break;
            case 'fantasy':
                gradient = ctx.createRadialGradient(width/2, height/2, 0, width/2, height/2, width/2);
                gradient.addColorStop(0, '#ddd6fe');
                gradient.addColorStop(0.3, '#f9a8d4');
                gradient.addColorStop(0.6, '#c084fc');
                gradient.addColorStop(1, '#818cf8');
                break;
            default: // realistic
                gradient = ctx.createLinearGradient(0, 0, width, height);
                gradient.addColorStop(0, '#0f172a');
                gradient.addColorStop(0.5, '#1e293b');
                gradient.addColorStop(1, '#334155');
        }
        
        ctx.fillStyle = gradient;
        ctx.fillRect(0, 0, width, height);
        
        // Add grid for depth perception
        ctx.strokeStyle = style === 'minimalist' ? 'rgba(0, 0, 0, 0.1)' : 'rgba(255, 255, 255, 0.05)';
        ctx.lineWidth = 1;
        const gridSize = 30;
        
        for (let x = 0; x <= width; x += gridSize) {
            ctx.beginPath();
            ctx.moveTo(x, 0);
            ctx.lineTo(x, height);
            ctx.stroke();
        }
        
        for (let y = 0; y <= height; y += gridSize) {
            ctx.beginPath();
            ctx.moveTo(0, y);
            ctx.lineTo(width, y);
            ctx.stroke();
        }
        
        const centerX = width / 2;
        const centerY = height / 2;
        
        // Main 3D object - More complex and realistic
        ctx.save();
        ctx.translate(centerX, centerY);
        
        // Rotate the entire scene
        ctx.rotate(rotation);
        
        // Draw 3D cube with proper perspective
        const size = 100;
        const depth = 60;
        
        // Back faces (darker)
        ctx.fillStyle = style === 'cartoon' ? 'rgba(251, 191, 36, 0.4)' : 
                       style === 'minimalist' ? 'rgba(156, 163, 175, 0.3)' :
                       style === 'fantasy' ? 'rgba(168, 85, 247, 0.4)' :
                       'rgba(14, 165, 233, 0.3)';
        ctx.strokeStyle = style === 'cartoon' ? 'rgba(251, 191, 36, 0.8)' :
                          style === 'minimalist' ? 'rgba(75, 85, 99, 0.8)' :
                          style === 'fantasy' ? 'rgba(168, 85, 247, 0.8)' :
                          'rgba(14, 165, 233, 0.8)';
        ctx.lineWidth = 2;
        
        // Back face
        ctx.beginPath();
        ctx.rect(-size/2 - depth/3, -size/2 - depth/3, size, size);
        ctx.fill();
        ctx.stroke();
        
        // Side faces
        ctx.fillStyle = style === 'cartoon' ? 'rgba(251, 191, 36, 0.5)' : 
                       style === 'minimalist' ? 'rgba(156, 163, 175, 0.4)' :
                       style === 'fantasy' ? 'rgba(236, 72, 153, 0.5)' :
                       'rgba(217, 70, 239, 0.4)';
        
        // Right face
        ctx.beginPath();
        ctx.moveTo(size/2, -size/2);
        ctx.lineTo(size/2 + depth/3, -size/2 - depth/3);
        ctx.lineTo(size/2 + depth/3, size/2 - depth/3);
        ctx.lineTo(size/2, size/2);
        ctx.closePath();
        ctx.fill();
        ctx.stroke();
        
        // Top face
        ctx.beginPath();
        ctx.moveTo(-size/2, -size/2);
        ctx.lineTo(-size/2 - depth/3, -size/2 - depth/3);
        ctx.lineTo(size/2 - depth/3, -size/2 - depth/3);
        ctx.lineTo(size/2, -size/2);
        ctx.closePath();
        ctx.fill();
        ctx.stroke();
        
        // Front face (brightest)
        ctx.fillStyle = style === 'cartoon' ? 'rgba(251, 191, 36, 0.7)' : 
                       style === 'minimalist' ? 'rgba(156, 163, 175, 0.6)' :
                       style === 'fantasy' ? 'rgba(236, 72, 153, 0.7)' :
                       'rgba(14, 165, 233, 0.6)';
        
        ctx.beginPath();
        ctx.rect(-size/2, -size/2, size, size);
        ctx.fill();
        ctx.stroke();
        
        // Add decorative elements based on style
        if (style === 'fantasy') {
            // Add sparkles
            ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
            for (let i = 0; i < 5; i++) {
                const sparkleX = (Math.random() - 0.5) * size * 1.5;
                const sparkleY = (Math.random() - 0.5) * size * 1.5;
                const sparkleSize = Math.random() * 3 + 1;
                
                ctx.beginPath();
                ctx.arc(sparkleX, sparkleY, sparkleSize, 0, Math.PI * 2);
                ctx.fill();
            }
        } else if (style === 'cartoon') {
            // Add cartoon eyes
            ctx.fillStyle = 'white';
            ctx.beginPath();
            ctx.arc(-20, -10, 12, 0, Math.PI * 2);
            ctx.arc(20, -10, 12, 0, Math.PI * 2);
            ctx.fill();
            
            ctx.fillStyle = 'black';
            ctx.beginPath();
            ctx.arc(-20, -10, 6, 0, Math.PI * 2);
            ctx.arc(20, -10, 6, 0, Math.PI * 2);
            ctx.fill();
        }
        
        ctx.restore();
        
        // Add shadow
        ctx.save();
        ctx.translate(centerX, centerY + 100);
        ctx.scale(1, 0.3);
        ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
        ctx.beginPath();
        ctx.ellipse(0, 0, size/2, size/4, 0, 0, Math.PI * 2);
        ctx.fill();
        ctx.restore();
        
        // Add text overlay
        ctx.fillStyle = style === 'minimalist' ? 'rgba(0, 0, 0, 0.7)' : 'rgba(255, 255, 255, 0.9)';
        ctx.font = 'bold 14px sans-serif';
        ctx.textAlign = 'center';
        ctx.fillText('3D Model Generated!', centerX, height - 20);
    }
// Add to recent creations
    function addToRecentCreations(description) {
        const recentContainer = document.getElementById('recent-creations');
        const newItem = document.createElement('div');
        newItem.className = 'bg-gray-800/50 rounded-lg overflow-hidden hover:scale-105 transition-transform duration-300 cursor-pointer opacity-0';
        
        const randomSeed = Math.floor(Math.random() * 100);
        newItem.innerHTML = `
            <img src="http://static.photos/abstract/320x240/${randomSeed}" alt="Creation" class="w-full h-40 object-cover">
            <div class="p-3">
                <p class="text-sm text-gray-300 truncate">${description.substring(0, 30)}${description.length > 30 ? '...' : ''}</p>
                <p class="text-xs text-gray-500">Just now</p>
            </div>
        `;
        
        recentContainer.insertBefore(newItem, recentContainer.firstChild);
        
        // Animate in
        setTimeout(() => {
            newItem.classList.remove('opacity-0');
            newItem.classList.add('opacity-100');
        }, 100);
        
        // Remove last item if more than 4
        if (recentContainer.children.length > 4) {
            recentContainer.removeChild(recentContainer.lastChild);
        }
    }
    
    // Notification system
    function showNotification(message, type = 'info') {
        const notification = document.createElement('div');
        notification.className = `fixed top-20 right-4 px-6 py-3 rounded-lg shadow-lg transform translate-x-full transition-transform duration-300 z-50`;
        
        const bgColor = type === 'warning' ? 'bg-yellow-500' : type === 'error' ? 'bg-red-500' : 'bg-primary-500';
        notification.classList.add(bgColor);
        
        notification.innerHTML = `
            <div class="flex items-center gap-2 text-white">
                <i data-feather="${type === 'warning' ? 'alert-triangle' : 'info'}" class="w-5 h-5"></i>
                <span>${message}</span>
            </div>
        `;
        
        document.body.appendChild(notification);
        feather.replace();
        
        // Animate in
        setTimeout(() => {
            notification.classList.remove('translate-x-full');
            notification.classList.add('translate-x-0');
        }, 100);
        
        // Remove after 3 seconds
        setTimeout(() => {
            notification.classList.add('translate-x-full');
            setTimeout(() => {
                notification.remove();
            }, 300);
        }, 3000);
    }
    // Enhanced Canvas animation loop
    let animationId;
    function animateCanvas() {
        if (!canvas.classList.contains('hidden') && canvas.dataset.style) {
            const rotation = (Date.now() / 1000) % (Math.PI * 2);
            const ctx = canvas.getContext('2d');
            
            draw3DModel(
                ctx, 
                canvas.width, 
                canvas.height, 
                canvas.dataset.style, 
                rotation * 0.5
            );
        }
        animationId = requestAnimationFrame(animateCanvas);
    }
    
    // Start animation loop
    animateCanvas();
    
    // Clean up animation when canvas is hidden
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
                if (canvas.classList.contains('hidden')) {
                    cancelAnimationFrame(animationId);
                } else if (canvas.dataset.style) {
                    animateCanvas();
                }
            }
        });
    });
    
    observer.observe(canvas, { attributes: true });
});

// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        const target = document.querySelector(this.getAttribute('href'));
        if (target) {
            target.scrollIntoView({ behavior: 'smooth' });
        }
    });
});

// Intersection Observer for animations
const observerOptions = {
    threshold: 0.1,
    rootMargin: '0px 0px -50px 0px'
};

const observer = new IntersectionObserver((entries) => {
    entries.forEach(entry => {
        if (entry.isIntersecting) {
            entry.target.classList.add('animate-fadeIn');
        }
    });
}, observerOptions);

document.querySelectorAll('.section-animate').forEach(el => {
    observer.observe(el);
});