-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.js
More file actions
282 lines (223 loc) · 6.83 KB
/
editor.js
File metadata and controls
282 lines (223 loc) · 6.83 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
var canvas = document.getElementById("textarea");
var ctx = canvas.getContext("2d");
// resize the canvas to fill browser window dynamically
window.addEventListener('resize', resizeCanvas, false);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx.font = "bold 200px sans-serif";
// put the clock within the new window
clock_r = 50;
clock_x = window.innerWidth - clock_r - 15;
clock_y = clock_r + 15;
text_y = 400;
}
var textWritten;
var textVisible;
// prompt stuff
var externWordList = words;
var promptID;
var lastPromptCharIndex;
var promptTypingSpeed;
var userHasTyped;
// thresholding
var promptThreshHold;
var threshHold;
var userThreshHold;
var charPerSecondWindow;
var avgTimeBetweenKeystrokes;
var targetAvg;
var timeStampOfKeystroke;
var startTime;
var prevTime;
var curTime;
var timeLimit;
var lightness;
var baseHue;
var baseSat;
var fadeTime; // ms
var finalRGB;
var finalRGBString;
var clock_r;
var clock_x;
var clock_y;
var clock_arc;
var text_y;
function init() {
textWritten = '';
textVisible = '';
// prompt stuff
promptID = getPromptID(externWordList);
lastPromptCharIndex = 0;
promptTypingSpeed = 80; // ms per char
userHasTyped = false;
// thresholding
promptThreshHold = externWordList[promptID].length + 2;
threshHold = promptThreshHold;
userThreshHold = 7;
charPerSecondWindow = 10; // looks at the last ten characters typed to get the cps
avgTimeBetweenKeystrokes = 0; // ms
targetAvg = 60; // ms between keys, really cruising.
timeStampOfKeystroke = []; // maps the n-th character typed to the elapsed time when it was typed (indexed by nth char)
startTime = new Date().getTime();
prevTime = startTime;
curTime = startTime;
timeLimit = 5000.0; // three minutes
lightness = 0;
baseHue = Math.random();
baseSat = 1;
fadeTime = 1000; // ms
finalRGB = [0,0,0];
finalRGBString = '';
clock_arc = (Math.PI * 2) * ( (curTime - startTime) / timeLimit) - (Math.PI / 2);
}
/**
* Converts an HSL color value to RGB. Conversion formula
* adapted from http://en.wikipedia.org/wiki/HSL_color_space.
* Assumes h, s, and l are contained in the set [0, 1] and
* returns r, g, and b in the set [0, 255].
*
* @param Number h The hue
* @param Number s The saturation
* @param Number l The lightness
* @return Array The RGB representation
*/
function hslToRgb(h, s, l){
var r, g, b;
if(s == 0){
r = g = b = l; // achromatic
}else{
function hue2rgb(p, q, t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s;
var p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return [r * 255, g * 255, b * 255];
}
function getPromptID(wordArray) {
return Math.floor(Math.random() * wordArray.length) ;
}
function printPromptChar(charIndex, id) {
var theWord = '"' + externWordList[id] + '"';
if (charIndex < theWord.length) {
printChar(theWord.charAt(charIndex));
}
return ++charIndex;
}
function componentsToRGBString(components) {
return "rgb(" + parseInt(components[0]) + "," + parseInt(components[1]) + "," + parseInt(components[2]) + ")";
}
function loop () {
if (!userHasTyped) {
if ((curTime - startTime) / (promptTypingSpeed) >= lastPromptCharIndex) {
lastPromptCharIndex = printPromptChar(lastPromptCharIndex, promptID);
}
}
prevTime = curTime;
curTime = new Date().getTime();
var dt = curTime - prevTime;
clock_arc = (Math.PI * 2) * ( (curTime - startTime) / timeLimit) - (Math.PI / 2);
if (clock_arc >= (Math.PI * 2 - Math.PI / 2)) {
init();
}
draw(dt);
setTimeout(loop,0);
}
function preventBackspaceHandler(evt) {
evt = evt || window.event;
if (evt.keyCode == 8 || evt.keyCode == 27) {
canvas.onkeypress(evt);
return false;
}
}
function calcCharPerSecond() {
if (timeStampOfKeystroke.length == 0) {
return;
}
var sample;
if (timeStampOfKeystroke.length < charPerSecondWindow) {
sample = timeStampOfKeystroke;
} else {
sample = timeStampOfKeystroke.slice(timeStampOfKeystroke.length - charPerSecondWindow);
}
var gapSum = 0;
for (var i = 0; i < sample.length - 1; i++) {
// Go through each 'gap' in the timestamps, add it up, set average to average of gaps
gapSum += (sample[i+1] - sample[i]);
}
avgTimeBetweenKeystrokes = gapSum / sample.length;
}
function printChar(charStr) {
timeStampOfKeystroke.push(curTime);
calcCharPerSecond();
textWritten += charStr;
}
function keypressHandler(evt) {
if (!userHasTyped) {
textWritten = '';
timeStampOfKeystroke = [];
threshHold = userThreshHold;
userHasTyped = true;
}
if (evt.keyCode == 8) {
// delete
textWritten = textWritten.slice(0,-1);
timeStampOfKeystroke.pop();
// don't recalc average, dude is just a slowpoke
return;
}
if (evt.keyCode == 27) {
// escape
init();
return;
}
var charCode = evt.which;
var charStr = String.fromCharCode(charCode);
printChar(charStr);
}
function draw(dt) {
textVisible = textWritten.substring(textWritten.length - threshHold);
ctx.fillStyle = '#222';
ctx.fillRect(0, 0, canvas.width, canvas.height);
var offset = 0;
for (var i = 0; i < textVisible.length; i++) {
var character = textVisible.charAt(i);
var timeStampOffset = 0;
if (timeStampOfKeystroke.length > threshHold) {
timeStampOffset = timeStampOfKeystroke.length - threshHold;
}
var characterTimeStamp = timeStampOfKeystroke[i + timeStampOffset];
var elapsedTime = curTime - characterTimeStamp;
var charPerSecondModifier = targetAvg / avgTimeBetweenKeystrokes;
lightness = (charPerSecondModifier > 1 ? 1 : charPerSecondModifier) * 1 - (elapsedTime / fadeTime);
var cappedLightness = lightness > 0 ? lightness : 0;
var components = hslToRgb(baseHue, baseSat, cappedLightness);
ctx.fillStyle = componentsToRGBString(components);
ctx.fillText(character,offset,text_y);
offset += ctx.measureText(character).width;
}
ctx.fillStyle = componentsToRGBString(hslToRgb(baseHue, baseSat, 0.5));
ctx.beginPath();
ctx.moveTo(clock_x, clock_y);
ctx.lineTo(clock_x, clock_y + clock_r);
ctx.arc(clock_x, clock_y, clock_r, -(Math.PI / 2), clock_arc, false);
ctx.lineTo(clock_x, clock_y);
ctx.closePath();
ctx.fill();
}
/////////////////////////////////////////////
resizeCanvas();
canvas.focus();
document.onkeydown = preventBackspaceHandler;
canvas.onkeypress = keypressHandler;
init();
loop();