|
1 | | -document.addEventListener('DOMContentLoaded', function() { |
| 1 | +document.addEventListener('DOMContentLoaded', async function() { |
2 | 2 | const container = document.querySelector('.typing-container'); |
3 | | - const lines = document.querySelectorAll('.code-line'); |
4 | 3 |
|
5 | | - // Create cursor element |
6 | | - const cursor = document.createElement('div'); |
7 | | - cursor.className = 'typing-cursor'; |
8 | | - container.appendChild(cursor); |
9 | | - |
10 | | - let currentLine = 0; |
11 | | - |
12 | | - function updateCursorPosition() { |
13 | | - const activeLine = lines[currentLine]; |
14 | | - if (activeLine) { |
15 | | - const lineRect = activeLine.getBoundingClientRect(); |
16 | | - const containerRect = container.getBoundingClientRect(); |
17 | | - cursor.style.left = `${activeLine.offsetWidth}px`; |
18 | | - cursor.style.top = `${lineRect.top - containerRect.top}px`; |
19 | | - |
20 | | - // Change cursor color for output line |
21 | | - if (activeLine.classList.contains('code-output')) { |
22 | | - cursor.classList.add('at-output'); |
23 | | - } else { |
24 | | - cursor.classList.remove('at-output'); |
25 | | - } |
| 4 | + try { |
| 5 | + // Fetch and parse the JSON file |
| 6 | + const response = await fetch('/js/typing-sequence.json'); |
| 7 | + const data = await response.json(); |
| 8 | + |
| 9 | + if (!data || !data.sequences || data.sequences.length === 0) { |
| 10 | + console.error('No valid sequences found:', data); |
| 11 | + return; |
26 | 12 | } |
27 | | - } |
| 13 | + |
| 14 | + // Clear existing content and create lines from sequence |
| 15 | + container.innerHTML = ''; |
| 16 | + |
| 17 | + // Create all lines first |
| 18 | + data.sequences.forEach(sequence => { |
| 19 | + const line = document.createElement('div'); |
| 20 | + line.className = `code-line${sequence.type === 'output' ? ' code-output' : ''}`; |
| 21 | + line.textContent = sequence.content; |
| 22 | + container.appendChild(line); |
| 23 | + }); |
28 | 24 |
|
29 | | - function typeLine() { |
30 | | - if (currentLine < lines.length) { |
31 | | - // Start cursor animation for current line |
32 | | - const cursorInterval = setInterval(updateCursorPosition, 50); |
33 | | - |
34 | | - // After typing animation completes |
35 | | - setTimeout(() => { |
36 | | - clearInterval(cursorInterval); |
| 25 | + const lines = document.querySelectorAll('.code-line'); |
| 26 | + let currentLine = 0; |
| 27 | + |
| 28 | + async function typeLine() { |
| 29 | + if (currentLine < lines.length) { |
| 30 | + const line = lines[currentLine]; |
| 31 | + const sequence = data.sequences[currentLine]; |
| 32 | + |
| 33 | + // Start typing animation |
| 34 | + line.classList.add('visible'); |
| 35 | + |
| 36 | + // Simulate typing by incrementing visible length |
| 37 | + let visibleLength = 0; |
| 38 | + const typingInterval = setInterval(() => { |
| 39 | + visibleLength += 1; |
| 40 | + line.style.setProperty('--visible-length', visibleLength); |
| 41 | + if (visibleLength >= line.textContent.length) { |
| 42 | + clearInterval(typingInterval); |
| 43 | + } |
| 44 | + }, data.settings.typingSpeed / line.textContent.length); // Adjust for smoother typing |
| 45 | + |
| 46 | + // Wait for typing to complete |
| 47 | + await new Promise(resolve => setTimeout(resolve, data.settings.typingSpeed)); |
| 48 | + |
37 | 49 | currentLine++; |
| 50 | + |
38 | 51 | if (currentLine < lines.length) { |
39 | | - setTimeout(typeLine, 500); // Delay before next line |
| 52 | + await new Promise(resolve => setTimeout(resolve, sequence.delay)); |
| 53 | + await typeLine(); |
40 | 54 | } |
41 | | - }, 2000); // Match with CSS animation duration |
| 55 | + } |
42 | 56 | } |
43 | | - } |
44 | 57 |
|
45 | | - // Start typing after a short delay |
46 | | - setTimeout(typeLine, 500); |
| 58 | + // Start typing after initial delay |
| 59 | + setTimeout(() => typeLine(), 500); |
| 60 | + |
| 61 | + } catch (error) { |
| 62 | + console.error('Error in typing animation:', error); |
| 63 | + } |
47 | 64 | }); |
0 commit comments