Skip to content

Commit 8b27aa1

Browse files
committed
making it take the sequence from the json file and faster speed
1 parent d6565d0 commit 8b27aa1

File tree

3 files changed

+108
-48
lines changed

3 files changed

+108
-48
lines changed

js/typing-sequence.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"sequences": [
3+
{
4+
"type": "code",
5+
"content": "import me",
6+
"delay": 500
7+
},
8+
{
9+
"type": "code",
10+
"content": "me.whoIam()",
11+
"delay": 500
12+
},
13+
{
14+
"type": "output",
15+
"content": ">>> \"Rohit\"",
16+
"delay": 500
17+
},
18+
{
19+
"type": "output",
20+
"content": ">>> \"2021 IIT Jammu CS Gradaute\"",
21+
"delay": 500
22+
},
23+
{
24+
"type": "output",
25+
"content": ">>> \"AI Enginner at Captury GmbH since 2021 building computer vision solutions\"",
26+
"delay": 500
27+
},
28+
{
29+
"type": "code",
30+
"content": "me.hobby()",
31+
"delay": 500
32+
}
33+
34+
],
35+
"settings": {
36+
"typingSpeed": 1,
37+
"cursorColor": {
38+
"code": "#00ff9d",
39+
"output": "#ff9d00"
40+
}
41+
}
42+
}

js/typing.js

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,64 @@
1-
document.addEventListener('DOMContentLoaded', function() {
1+
document.addEventListener('DOMContentLoaded', async function() {
22
const container = document.querySelector('.typing-container');
3-
const lines = document.querySelectorAll('.code-line');
43

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;
2612
}
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+
});
2824

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+
3749
currentLine++;
50+
3851
if (currentLine < lines.length) {
39-
setTimeout(typeLine, 500); // Delay before next line
52+
await new Promise(resolve => setTimeout(resolve, sequence.delay));
53+
await typeLine();
4054
}
41-
}, 2000); // Match with CSS animation duration
55+
}
4256
}
43-
}
4457

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+
}
4764
});

styles.css

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,25 +90,22 @@ body {
9090
}
9191

9292
.code-line {
93+
display: block;
9394
color: #00ff9d;
9495
white-space: pre;
9596
margin-bottom: 0.5rem;
9697
position: relative;
9798
width: 0;
9899
overflow: hidden;
99-
animation: typing 2s steps(20, end) forwards;
100-
}
101-
102-
.code-line:nth-child(1) {
103-
animation-delay: 0.5s;
100+
font-family: 'Consolas', 'Monaco', monospace;
101+
font-size: 1.2rem;
104102
}
105103

106-
.code-line:nth-child(2) {
107-
animation-delay: 2.5s;
104+
.code-line.visible {
105+
animation: typing 0.5s steps(20, end) forwards;
108106
}
109107

110-
.code-line:nth-child(3) {
111-
animation-delay: 4.5s;
108+
.code-line.code-output {
112109
color: #ff9d00;
113110
}
114111

@@ -125,8 +122,12 @@ body {
125122
}
126123

127124
@keyframes typing {
128-
from { width: 0 }
129-
to { width: 100% }
125+
from {
126+
width: 0;
127+
}
128+
to {
129+
width: 100%;
130+
}
130131
}
131132

132133
@keyframes blink {

0 commit comments

Comments
 (0)