forked from craiggoddenpayne/AgentXIII
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeWriter.js
76 lines (64 loc) · 2.26 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
function TypeWriter() {
TypeWriter.prototype = this;
}
TypeWriter.prototype._text = "";
TypeWriter.prototype.TextColor = "white";
TypeWriter.prototype.Settings = null;
TypeWriter.prototype.Context = null;
TypeWriter.prototype.Initialise = function () {
TypeWriter.prototype.Settings = new TypeWriterSettings();
setInterval(TypeWriter.prototype.Tick, 50);
};
TypeWriter.prototype.TypeText = function (settings, context) {
TypeWriter.prototype.Context = context;
TypeWriter.prototype.Settings = settings;
};
TypeWriter.prototype._charCount =0;
TypeWriter.prototype._ticksForRender =0;
TypeWriter.prototype._renderUp = true;
TypeWriter.prototype.Tick = function() {
var allText = TypeWriter.prototype.Settings.Text;
if (TypeWriter.prototype._charCount < allText.length) {
TypeWriter.prototype._text += allText[TypeWriter.prototype._charCount];
TypeWriter.prototype._charCount += 1;
}
};
TypeWriter.prototype.Render = function (context, width) {
context.fillStyle = TypeWriter.prototype.TextColor;
context.font = "bold 18px Courier New";
var lines = TypeWriter.prototype._convertToLines(context,
TypeWriter.prototype._text,
width);
for(var i=0; i < lines.length; i++){
context.fillText(lines[i], 250, (20 * (i+1))+ 50);
}
};
TypeWriter.prototype.Clear = function() {
TypeWriter.prototype._charCount = 0;
TypeWriter.prototype._ticksForRender = 0;
TypeWriter.prototype._text = "";
};
TypeWriter.prototype._convertToLines = function(context, text, maxWidth){
var words = text.split(" ");
var lines = [""];
for(var n = 0; n < words.length; n++) {
var testLine = lines[lines.length-1] + words[n] + " ";
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if(testWidth > maxWidth) {
lines[lines.length] = words[n] + " ";
}
else {
lines[lines.length-1] = testLine;
}
}
return lines;
};
function TypeWriterSettings() {
TypeWriterSettings.prototype= this;
return this;
}
TypeWriterSettings.prototype= {
Text: "",
Speed: 100
}