-
Notifications
You must be signed in to change notification settings - Fork 0
/
typewriter.js
173 lines (143 loc) · 4.02 KB
/
typewriter.js
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
// Typewriter for p5js
// by Olaf Val
// CC BY 4.0
function Typewriter() {
this.TyWrTextAll = [];
this.TyWrTextPrint = [];
this.TyWrTextDisplay = '';
this.TyWrL = 0;
this.TyWrLine = 0;
this.TyWrS = ''
this.TyWrTimer = 0;
this.TyWrSpeed = 200;
this.TyWrAlign = 'LEFT';
this.TyWrX;
this.TyWrY;
this.TyWrW;
this.TyWrH;
this.testLine = '';
this.nextBreak = 0;
this.TyBreakNr = 0;
this.twAlign = function(t) {
if (t == 'LEFT') {
this.TyWrAlign = 'LEFT';
}
if (t == 'CENTER') {
this.TyWrAlign = 'CENTER';
}
if (t == 'RIGHT') {
this.TyWrAlign = 'RIGHT';
}
}
this.twRestart = function() {
this.TyWrL = 0;
this.TyWrTextPrint = [];
this.TyWrTextDisplay = '';
this.TyBreakNr = 0;
}
this.twSpeed = function(s) {
this.TyWrSpeed = s;
}
this.twLines = function() {
this.TyBreakNr = 1;
for(var f5 = 0; f5 < this.TyWrTextDisplay.length; f5++){
if(this.TyWrTextDisplay[f5] == '\n'){
this.TyBreakNr++;
}
}
return this.TyBreakNr;
}
this.twCompleted = function() {
if (this.TyWrL < this.TyWrTextAll.length) {
return false;
} else {
if (this.TyWrTextAll.length > 0) {
return true;
}
}
}
this.twBox = function(x, y, w, h) {
this.TyWrX = x;
this.TyWrY = y;
this.TyWrW = w;
this.TyWrH = h;
}
this.twType = function(TyWrText, x, y) {
var boxMode;
if (this.TyWrW == undefined) {
this.TyWrX = x;
this.TyWrY = y;
boxMode = 0;
} else {
boxMode = 1;
}
textAlign(LEFT);
if (boxMode == 0) {
if (this.TyWrAlign == 'LEFT') {
text(this.TyWrTextDisplay, this.TyWrX, this.TyWrY + textSize());
}
if (this.TyWrAlign == 'RIGHT') {
text(this.TyWrTextDisplay, x - textWidth(this.TyWrTextDisplay), y);
}
if (this.TyWrAlign == 'CENTER') {
text(this.TyWrTextDisplay, x - textWidth(this.TyWrTextDisplay) / 2, y);
}
} else {
text(this.TyWrTextDisplay, this.TyWrX, this.TyWrY + textSize());
}
if (millis() > this.TyWrTimer + this.TyWrSpeed) {
this.TyWrTimer = millis();
this.TyWrTextAll = TyWrText;
if (this.TyWrL < TyWrText.length) {
if (this.TyWrSpeed == 0) {
this.TyWrTextPrint = TyWrText;
} else {
append(this.TyWrTextPrint, TyWrText[this.TyWrL]);
}
// go through all letters of the current printString
this.TyWrTextDisplay = '';
this.TyWrLine = 0;
for (var f1 = 0; f1 < this.TyWrTextPrint.length; f1++) {
if (this.TyWrTextPrint[f1] == ' ') {
this.nextBreak = 0;
// find nextBreak for the end of the test line
for (var f2 = 1; f2 < TyWrText.length - f1; f2++) {
if (TyWrText[f1 + f2] == ' ') {
this.nextBreak = f1 + f2;
break;
}
}
// build testLine
this.testLine = '';
for (f4 = this.TyWrLine; f4 < this.nextBreak; f4++) {
this.testLine = this.testLine + TyWrText[f4];
}
//test if testLine fitts?
if (textWidth(this.testLine) >= this.TyWrW) {
// add beack
this.TyWrTextDisplay = this.TyWrTextDisplay + '\n';
this.TyWrLine = f1;
} else {
// add without beack
this.TyWrTextDisplay = this.TyWrTextDisplay + this.TyWrTextPrint[f1];
}
} else {
if ((this.TyWrTextPrint[f1] == '<') &&
(TyWrText[f1 + 1] == 'b') &&
(TyWrText[f1 + 2] == 'r') &&
(TyWrText[f1 + 3] == '>')) {
// add beack
this.TyWrTextDisplay = this.TyWrTextDisplay + '\n';
f1 = f1 + 3;
this.TyWrLine = f1;
} else {
// add without beack
this.TyWrTextDisplay = this.TyWrTextDisplay + this.TyWrTextPrint[f1];
}
}
}
this.TyWrL++;
}
}
}
}