forked from jayKayEss/Flapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflapdemo.js
More file actions
156 lines (117 loc) · 3.67 KB
/
Copy pathflapdemo.js
File metadata and controls
156 lines (117 loc) · 3.67 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
var FlapBuffer = function(wrap, num_lines) {
this.wrap = wrap;
this.num_lines = num_lines;
this.line_buffer = '';
this.buffers = [[]];
this.cursor = 0;
};
FlapBuffer.prototype = {
pushLine: function(line) {
if (this.buffers[this.cursor].length < this.num_lines) {
this.buffers[this.cursor].push(line);
} else {
this.buffers.push([]);
this.cursor++;
this.pushLine(line);
}
},
pushWord: function(word) {
if (this.line_buffer.length == 0) {
this.line_buffer = word;
} else if ((word.length + this.line_buffer.length + 1) <= this.wrap) {
this.line_buffer += ' ' + word;
} else {
this.pushLine(this.line_buffer);
this.line_buffer = word;
}
},
flush: function() {
if (this.line_buffer.length) {
this.pushLine(this.line_buffer);
this.line_buffer = '';
}
},
};
var FlapDemo = function(display_selector, input_selector, click_selector) {
var _this = this;
var onAnimStart = function(e) {
var $display = $(e.target);
$display.prevUntil('.flapper', '.activity').addClass('active');
};
var onAnimEnd = function(e) {
var $display = $(e.target);
$display.prevUntil('.flapper', '.activity').removeClass('active');
};
this.opts = {
chars_preset: 'alphanum',
align: 'left',
width: 20,
on_anim_start: onAnimStart,
on_anim_end: onAnimEnd
};
this.timers = [];
this.$displays = $(display_selector);
this.num_lines = this.$displays.length;
this.line_delay = 300;
this.screen_delay = 7000;
this.$displays.flapper(this.opts);
this.$typesomething = $(input_selector);
$(click_selector).click(function(e){
var text = _this.cleanInput(_this.$typesomething.val());
_this.$typesomething.val('');
if (text.match(/what is the point/i) || text.match(/what's the point/i)) {
text = "WHAT'S THE POINT OF YOU?";
}
var buffers = _this.parseInput(text);
_this.stopDisplay();
_this.updateDisplay(buffers);
e.preventDefault();
});
};
FlapDemo.prototype = {
cleanInput: function(text) {
return text.trim().toUpperCase();
},
parseInput: function(text) {
var buffer = new FlapBuffer(this.opts.width, this.num_lines);
var lines = text.split(/\n/);
for (i in lines) {
var words = lines[i].split(/\s/);
for (j in words) {
buffer.pushWord(words[j]);
}
buffer.flush();
}
buffer.flush();
return buffer.buffers;
},
stopDisplay: function() {
for (i in this.timers) {
clearTimeout(this.timers[i]);
}
this.timers = [];
},
updateDisplay: function(buffers) {
var _this = this;
var timeout = 100;
for (i in buffers) {
_this.$displays.each(function(j) {
var $display = $(_this.$displays[j]);
(function(i,j) {
_this.timers.push(setTimeout(function(){
if (buffers[i][j]) {
$display.val(buffers[i][j]).change();
} else {
$display.val('').change();
}
}, timeout));
} (i, j));
timeout += _this.line_delay;
});
timeout += _this.screen_delay;
}
}
};
$(document).ready(function(){
new FlapDemo('input.display', '#typesomething', '#showme');
});