-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypewriter-component.js
More file actions
141 lines (119 loc) · 4.52 KB
/
typewriter-component.js
File metadata and controls
141 lines (119 loc) · 4.52 KB
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
class TypewriterEffect extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
static get observedAttributes() {
return ['phrases', 'font-size', 'color', 'font-family', 'cursor-color', 'font-weight'];
}
connectedCallback() {
this.render();
this.initTypewriter();
}
disconnectedCallback() {
if (this.cursorInterval) {
clearInterval(this.cursorInterval);
}
if (this.typingTimeout) {
clearTimeout(this.typingTimeout);
}
}
render() {
const fontSize = this.getAttribute('font-size') || '3rem';
const color = this.getAttribute('color') || '#333333';
const fontFamily = this.getAttribute('font-family') || '"Courier New", monospace';
const fontWeight = this.getAttribute('font-weight') || 'normal';
const cursorColor = this.getAttribute('cursor-color') || color;
this.shadowRoot.innerHTML = `
<style>
:host {
display: inline-block;
font-family: ${fontFamily};
font-size: ${fontSize};
font-weight: ${fontWeight};
color: ${color};
letter-spacing: 0.1em;
word-wrap: break-word;
line-height: 1.2;
}
.cursor {
position: relative;
font-weight: bold;
opacity: 1;
color: ${cursorColor};
left: -0.1em;
}
</style>
<span class="text-container">
<span id="text"></span><span class="cursor">|</span>
</span>
`;
}
initTypewriter() {
// Parse phrases
const phrasesAttr = this.getAttribute('phrases');
try {
this.phrases = phrasesAttr ? JSON.parse(phrasesAttr) : ['Hello World!'];
} catch (e) {
this.phrases = ['Hello World!'];
}
// Get elements
this.textDisplay = this.shadowRoot.getElementById('text');
this.cursor = this.shadowRoot.querySelector('.cursor');
// State variables - exactly like original
this.currentPhrase = [];
this.i = 0;
this.j = 0;
this.isDeleting = false;
this.isEnd = false;
this.cursorVisible = true;
// Cursor animation setup - exactly like original
const animateCursor = () => {
this.cursorVisible = !this.cursorVisible;
this.cursor.style.opacity = this.cursorVisible ? "1" : "0";
};
this.cursorInterval = setInterval(animateCursor, 400);
this.cursor.style.transition = "all 0.15s ease";
// Start typewriter
this.loopThroughPhrases();
}
loopThroughPhrases() {
this.isEnd = false;
if (this.i < this.phrases.length) {
// TYPING EFFECT
if (!this.isDeleting && this.j <= this.phrases[this.i].length) {
this.currentPhrase.push(this.phrases[this.i][this.j]);
this.j++;
this.textDisplay.innerHTML = this.currentPhrase.join("");
// Make cursor visible during typing
this.cursor.style.opacity = "1";
}
// DELETING EFFECT
if (this.isDeleting && this.j <= this.phrases[this.i].length) {
this.currentPhrase.pop();
this.j--;
this.textDisplay.innerHTML = this.currentPhrase.join("");
}
// End of phrase reached
if (this.j == this.phrases[this.i].length) {
this.isEnd = true;
this.isDeleting = true;
}
// All characters deleted
if (this.isDeleting && this.j === 0) {
this.currentPhrase = [];
this.isDeleting = false;
this.i++;
if (this.i === this.phrases.length) {
this.i = 0;
}
}
}
// Timing control - EXACTLY like original
const spedUp = Math.random() * 150;
const normalSpeed = Math.random() * 300;
const time = this.isEnd ? 2000 : this.isDeleting ? spedUp : normalSpeed;
this.typingTimeout = setTimeout(() => this.loopThroughPhrases(), time);
}
}
customElements.define('typewriter-effect', TypewriterEffect);