-
Notifications
You must be signed in to change notification settings - Fork 6
/
typed-text.html
226 lines (193 loc) · 5.23 KB
/
typed-text.html
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
<link rel="import" href="../polymer/polymer.html">
<!--
`typed-text`
An element that simulates typing
@demo demo/index.html
-->
<dom-module id="typed-text">
<template>
<style>
:host {
display: block;
box-sizing: border-box;
@apply --typed-text-options;
}
.cursor {
@apply --typed-text-cursor;
}
.blink {
-webkit-animation: 1s blink infinite;
-moz-animation: 1s blink infinite;
-ms-animation: 1s blink infinite;
-o-animation: 1s blink infinite;
animation: 1s blink infinite;
@apply --typed-text-cursor-blink;
}
@keyframes blink{
0% {
opacity: 1;
@apply --typed-text-cursor-blink-0;
}
50% {
opacity: 0;
@apply --typed-text-cursor-blink-50;
}
100% {
opacity: 1;
@apply --typed-text-cursor-blink-100;
}
}
@-webkit-keyframes blink{
0% {
opacity: 1;
@apply --typed-text-cursor-blink-0;
}
50% {
opacity: 0;
@apply --typed-text-cursor-blink-50;
}
100% {
opacity: 1;
@apply --typed-text-cursor-blink-100;
}
}
@-moz-keyframes blink{
0% {
opacity: 1;
@apply --typed-text-cursor-blink-0;
}
50% {
opacity: 0;
@apply --typed-text-cursor-blink-50;
}
100% {
opacity: 1;
@apply --typed-text-cursor-blink-100;
}
}
</style>
<div>{{typed}}<span class$="cursor [[_isBlinking(noblink)]]">{{cursor}}</span></div>
</template>
<script>
function strip(html){
var doc = new DOMParser().parseFromString(html, 'text/html');
return doc.body.textContent;
}
Polymer({
is: 'typed-text',
properties: {
// Array of user provided strings for typing
strings: {
type: Array,
value: function() {
// Uncomment below if you want the element strings property to override the inner HTML text
// return ['typed-text is awesome', 'it supports deleting!'];
return [];
}
},
// Loops between the strings
noloop: Boolean,
noblink: {
type: Boolean,
value: false
},
// Time to wait between typed text
wait: {
type: Number,
value: 2000
},
// Typing speed
speed: {
type: Number,
value: 60
},
// Deleting speed
delspeed: {
type: Number,
value: 40
},
// custom cursor
cursor: {
type: String,
value: '|' //▍alternative block
},
// Typed text
_typed: {
type: String,
value: ' '
},
// Index of Strings array
_arrayIndex: {
type: Number,
value: 0
},
// Index of typed string
_index: {
type: Number,
value: -1
},
/* If two consecutive strings have characters in common
* do not retype them
*/
noretype: Boolean,
_subindex: {
type: Number,
value: 0
}
},
// Element Lifecycle
ready: function() {
// Get strings from HTML if the `strings` array hasn't been set
if (this.strings.length == 0){
this.async(this._setStrings, 500);
}
this.async(this._typing, 500);
},
// Get strings from HTML
_setStrings: function() {
this.strings = strip(this.innerHTML).replace(/ +(?= )/g, '').split("\n ");
this.strings = this.strings.filter(function(str) {
return /\S/.test(str);
});
},
// Element Behavior
_typing: function() {
requestAnimationFrame(function () {
this._index += 1;
this.typed = this.strings[this._arrayIndex].substr(0, this._index);
if(this._index <= this.strings[this._arrayIndex].length){
this.async(this._typing, this.speed);
} else {
if(!this.noloop || (this._arrayIndex != this.strings.length - 1)) {
this.async(this._deleting, this.wait);
}
}
}.bind(this));
},
_deleting: function() {
requestAnimationFrame(function () {
this._index -= 1;
if(this._index >= this._subindex) {
this.typed = this.strings[this._arrayIndex].substr(0, this._index - 1);
this.async(this._deleting, this.delspeed);
} else {
this._arrayIndex = (this._arrayIndex + 1)%this.strings.length;
this._calculateSubIndex(this._arrayIndex);
this._typing();
}
}.bind(this));
},
_calculateSubIndex: function(arrayIndex) {
var i = 0;
var next = (this._arrayIndex + 1)%this.strings.length;
while(this.strings[arrayIndex][i] == this.strings[next][i] && this.strings.length != 1) {
i++;
}
this._subindex = i;
},
_isBlinking: function() {
return !this.noblink && 'blink';
}
});
</script>
</dom-module>