forked from Miserlou/Glance-Bookmarklet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spritz.js
173 lines (142 loc) · 4.41 KB
/
spritz.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
/*
Copyright 2014 Mark Bradshaw ([email protected]), Joel Fouse ([email protected])
*/
(function() {
var open_spritz = {};
var internals = {
position: 0,
sentence_break: [],
split_text: [],
state: 'stopped',
wpm: 250
};
/******** PUBLIC *********/
open_spritz.assignElement = function(el) {
internals.el = el;
};
open_spritz.setWPM = function(wpm) {
if (wpm === 0) {
return;
}
internals.wpm = wpm;
};
open_spritz.spritzify = function(text) {
if (internals.el === undefined) {
console.log('need an element');
return;
}
if (text === '' || text === undefined) {
console.log('need some text');
return;
}
internals.text = text;
internals.split_text = text.split(/\s+/);
internals.sentence_break.length = 0;
internals.position = 0;
setTimeout(open_spritz.start, 1000);
};
open_spritz.start = function() {
if (internals.state === 'running') {
return;
}
internals.state = 'running';
displayWord();
};
open_spritz.stop = function() {
internals.state = 'stopped';
};
open_spritz.toggle = function() {
if (internals.state === 'stopped') {
open_spritz.start();
} else {
open_spritz.stop();
}
};
open_spritz.getState = function (){
return internals.state;
};
open_spritz.getPosition = function() {
return internals.position;
};
open_spritz.setPosition = function(position) {
internals.position = position;
if (position > internals.split_text.length - 1) {
internals.position = internals.split_text.length - 1;
}
};
open_spritz.getLength = function() {
return internals.split_text.length;
};
/******* PRIVATE **********/
function displayWord() {
if (internals.state === 'stopped') {
return;
}
if (internals.position >= internals.split_text.length) {
internals.state = 'stopped';
if (internals.onEnd !== undefined && typeof internals.onEnd === 'function') {
internals.onEnd();
}
return;
}
var word = internals.split_text[internals.position++];
var pivot = pivotLetter(word);
var startPad = repeat(' ', 11 - pivot);
var start = word.slice(0, pivot - 1);
var letter = word.slice(pivot - 1, pivot);
var end = word.slice(pivot, word.length);
var endPad = repeat(' ', 11 - (word.length - pivot));
internals.el.innerHTML = startPad + start + '<span class="OSCursor">' + letter + '</span>' + end + endPad;
// standard delay
var delay = 60 * 1000 / internals.wpm;
var lastLetter = word.slice(word.length - 1);
if ([',', ':'].indexOf(lastLetter) !== -1) {
// delay += delay * 1.3;
}
// Looking for punction in the last two characters. Could be !" or ?) or just . at the end. We need a pause for that.
if (word.length > 2) {
var lastTwoLetters = word.slice(word.length - 2);
if (['?', '!', '.', ';'].indexOf(lastTwoLetters) !== -1) {
// delay += delay * 1.8;
internals.sentence_break.push(internals.position - 1);
}
}
setTimeout(displayWord, delay);
}
function pivotLetter(word) {
var bestLetter;
switch (word.length) {
case 1:
bestLetter = 1; // first
break;
case 2:
case 3:
case 4:
case 5:
bestLetter = 2; // second
break;
case 6:
case 7:
case 8:
case 9:
bestLetter = 3; // third
break;
case 10:
case 11:
case 12:
case 13:
bestLetter = 4; // fourth
break;
default:
bestLetter = 5; // fifth
}
return bestLetter;
}
function repeat(letter, num) {
if (num < 1) {
return new Array(Math.abs(num) + 1).join(letter);
}
return new Array(num + 1).join(letter);
}
window.open_spritz = open_spritz;
})();