-
Notifications
You must be signed in to change notification settings - Fork 0
/
noga.html
757 lines (676 loc) · 34.3 KB
/
noga.html
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Glyph learner</title>
<meta name="viewport" content="width=device-width, user-scalable=no">
<link rel='shortcut icon' type='image/x-icon' href='glyph.ico' />
<script src="glyphs.js"></script>
<!--
Create new list of glyphs:
JSON.stringify(glyphs.sort(function(a,b){return a.family.localeCompare(b.family);}),function(key,value){if(key=='strokes')return undefined; else return value;})
Sort glyphs by family, then name[0]:
glyphs.sort(function(a,b){var c=a.family.localeCompare(b.family);if(c==0)c=a.name[0].localeCompare(b.name[0]);return c;})
-->
<style>
body {
background-color: #000;
}
svg {
background: url("background.png");
background-size: contain;
position: relative;
}
#adiv {
text-shadow: 0 0 6px #6be7bd;
background-color: #000;
color: #6be7bd;
font-size: 20pt;
text-align: center;
font-family: sans-serif;
font-weight: 200;
}
path {
stroke-linecap: round;
stroke-linejoin: round;
}
.btn {
border: solid 2px #00f3ff;
background-color: #19454a;
background: linear-gradient(to bottom, #08757b 0%,#1f464a 20%,#1f464a 80%,#08757b 100%);
color: #8cffff;
padding: 10px;
float: right;
font-size: 10pt;
font-family: sans-serif;
font-weight: bold;
margin-left: 5px;
text-shadow: 0px 0px 3px #08757b;
/*-webkit-transform: scale(1,1.4);
font-weight: normal;*/
}
.lbtn {
border: solid 3px #00f3ff;
background-color: #19454a;
color: #8cffff;
padding: 10px;
font-size: 10pt;
font-family: sans-serif;
font-weight: bold;
width: inherit;
margin-top: 3px;
}
.text {
color: #6be7c5;
text-align: center;
font-family: sans-serif;
}
.text a {
color: #00833b;
}
</style>
</head>
<body>
<div id="progress-container" style="width:80px; height:10px; border:1px solid #00e969; margin:3px; position: fixed; right: 0px; display: none">
<div id="progress-bar" style="width:70%; background-image:linear-gradient( 90deg, #00833b, #00ef73 ); height:inherit; float: left"></div>
<div id="anti-progress-bar" style="width:30%; background-image:linear-gradient( 90deg, #520000, #840000 ); height:inherit; float: left"></div>
</div>
<div id="spacer" class="text"></div>
<div style="background: #000" width="100%" id="adiv" onclick="textClick()">UNKNOWN</div>
<svg xmlns="http://www.w3.org/2000/svg" width="50" height="52" id="glyphsvg">
<path id="p" stroke="#aef" stroke-width="2" fill="none" pointer-events="none"/>
</svg>
<div style='overflow:auto' id="mainButtons">
<div class="btn" onclick="showNextGlyph()" id="nextBtn">NEXT</div>
<div class="btn" onclick="showCurrentGlyph()" id="showButton">SHOW</div>
<div class="btn" onclick="showLessons()" id="lessonsButton" style="float: left;margin-left:0px">LESSONS</div>
</div>
<div id='LessonsDiv' style='display:none'>
<div class="lbtn" onclick="startLesson(-1)" >No lesson - free play!</div>
<div class="text" id="aboutdiv"><br/>About:<br/>Version: 1.01<br/>Feedback: <a href="mailto:[email protected]">[email protected]</a><br/>Bugtracker <a href="https://github.com/jokedst/jokedst.github.io/issues">at github</a></div>
</div>
<script>
var r = document.getElementById('glyphsvg'),
p = document.getElementById('p'),
txt = document.getElementById('adiv'),
drawnPath = '',
isDown = false,
points = [], starti=0, stopi=0,
cutoff = 1000;
var targets = [{ x: 250, y: 30 },
{ x: 54, y: 143 },
{ x: 446, y: 143 },
{ x: 152, y: 200 },
{ x: 348, y: 200 },
{ x: 250, y: 257 },
{ x: 152, y: 313 },
{ x: 348, y: 313 },
{ x: 54, y: 370 },
{ x: 446, y: 370 },
{ x: 250, y: 483 }];
var drawcolor = { r: 255, g: 255, b: 0 },
okcolor = { r: 50, g: 255, b: 50 },
badcolor = { r: 255, g: 50, b: 50 };
var strokes = {};
var glyph = null,
glyphsShown = 0;
var lastTouchedTarget = -1,
vibrateOn = true;
var vibrate = navigator.vibrate || navigator.mozVibrate || navigator.webkitVibrate || null;
var currentLesson = -1, lesson = {};
var results = null, settings = {};
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
};
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(value);
};
function setProgressBar(percent) {
percent = Math.floor(percent);
document.getElementById('anti-progress-bar').style.width = (100 - percent) + '%';
document.getElementById('progress-bar').style.width = percent + '%';
document.getElementById('progress-container').style.display = '';
}
function hideProgressBar() {
document.getElementById('progress-container').style.display = 'none';
}
function interpolateColor(from, to, delta) {
var red = Math.round((from.r * (1.0 - delta)) + (to.r * delta));
var green = Math.round((from.g * (1.0 - delta)) + (to.g * delta));
var blue = Math.round((from.b * (1.0 - delta)) + (to.b * delta));
return 'rgb(' + red + ',' + green + ',' + blue + ')';
}
function obj2col(color) {
var s = 'rgb(' + color.r + ',' + color.g + ',' + color.b + ')';
return s;
}
function cssrules() {
var rules = {}; var ds = document.styleSheets, dsl = ds.length;
for (var i = 0; i < dsl; ++i) {
var dsi = ds[i].cssRules, dsil = dsi.length;
for (var j = 0; j < dsil; ++j) rules[dsi[j].selectorText] = dsi[j];
}
return rules;
};
function css_getclass(name) {
var ds = document.styleSheets;
for (var i = 0, dsl = ds.length; i < dsl; ++i) {
var dsi = ds[i].cssRules;
for (var j = 0, dsil = dsi.length; j < dsil; ++j) {
if (dsi[j].selectorText == name)
return dsi[j];
}
}
return null;
};
function randomizeArray(myArray) {
var i = myArray.length;
if (i == 0) return;
while (--i) {
var j = Math.floor(Math.random() * (i + 1));
var tempi = myArray[i];
var tempj = myArray[j];
myArray[i] = tempj;
myArray[j] = tempi;
}
};
window.onload = function () {
// This is a bunch of 'adapt to resolution' crap. Works sometimes :)
var w = document.getElementById('adiv').offsetWidth;
var h = Math.round(w * 518 / 500);
var winh = window.innerHeight;
// Detect landscape view
if (winh < h * 1.2) {
w = Math.round(winh *0.8 * 500 / 518);
h = Math.round(w * 518 / 500);
}
console.log(w);
r.style.width = w + 'px';
r.style.height = h + 'px';
// The cutoff is squared to avoid sqrt later
cutoff = Math.pow(w * 30.0 / 500.0, 2);
console.log('winh: ' + winh);
// Scale the target locations to new resolution
for (var t in targets) {
targets[t].x *= w / 500.0;
targets[t].y *= w / 500.0;
}
p.style.strokeWidth = Math.round(w / 50);
txt.style.fontSize = Math.round(w / 10) + 'px';
var txth = txt.offsetHeight;
var z = w * 0.128;
var btnstyle = css_getclass('.btn').style;
btnstyle.fontSize = Math.round(z / 3) + 'px';
btnstyle.padding = Math.round(z / 3) + 'px';
var btnh = document.getElementById('nextBtn').offsetHeight;
console.log('winh: ' + winh + ', btnh: ' + btnh + ', ' + ', txth: ' + txth);
console.log('toth: ' + (btnh + txth + h));
console.log('left: ' + (winh - (btnh + txth + h)));
document.getElementById('spacer').style.height = (winh - (btnh + txth + h) - 30) + 'px';
// Build up the lessons buttons
var lessonsDiv = document.getElementById('LessonsDiv');
var aboutdiv = document.getElementById('aboutdiv');
for (var l in lessons) {
//var lessonNumber = l;
var btn = document.createElement("div");
btn.appendChild(document.createTextNode(lessons[l].name));
btn.className = 'lbtn';
//btn.onclick = function () { var lessonNumber = l; startLesson(lessonNumber); };
btn.setAttribute('onClick', 'startLesson(' + l + ')');
//lessonsDiv.appendChild(btn);
lessonsDiv.insertBefore(btn, aboutdiv);
lessons[l].glyphs = [];
}
// Figure out shich strokes make up each glyph (e.g. 'f1t4' = from 1 to 4)
for (var g in glyphs) {
glyphs[g].strokes = {};
var lastt = glyphs[g].points[0];
for (var i = 1; i < glyphs[g].points.length; i++) {
var next = glyphs[g].points[i];
// To avoid dupes always take lowest number fist
glyphs[g].strokes['f' + Math.min(lastt, next) + 't' + Math.max(lastt, next)] = 1;
lastt = next;
}
// Add glyph to correct lesson
for (l = 0; l < lessons.length; l++) {
if (lessons[l].family == glyphs[g].family) {
lessons[l].glyphs.push(glyphs[g]);
break;
}
}
}
// Load old results (if any)
results = window.localStorage.getObject('results');
settings = window.localStorage.getObject('settings') || {}; // An empty object means default settings
};
function showLessons() {
txt.textContent = 'LESSONS';
document.getElementById('mainButtons').style.display = 'none';
document.getElementById('glyphsvg').style.display = 'none';
document.getElementById('spacer').style.display = 'none';
document.getElementById('LessonsDiv').style.display = '';
}
function startLesson(l) {
console.log('Starting lesson ' + l);
currentLesson = l;
document.getElementById('mainButtons').style.display = '';
document.getElementById('glyphsvg').style.display = '';
document.getElementById('spacer').style.display = '';
document.getElementById('LessonsDiv').style.display = 'none';
if (l == -1) {
// Free play!
document.getElementById('spacer').textContent = '';
txt.textContent = 'UNKNOWN';
hideProgressBar();
return;
}
lesson = { glyphsleft: lessons[l].glyphs.slice(0), peeked: false, glyphCount: lessons[l].glyphs.length };
document.getElementById('spacer').textContent = lessons[l].name.toUpperCase();
setProgressBar(0);
showNextGlyph();
}
function isTouchEvent(e) {
return e.type.match(/^touch/);
}
function _pointOnLine(line1, line2, pt) {
var isValid = false;
var ret = { x: 0, y: 0 };
// handle straight lines specially
if (line1.x == line2.x) {
ret = { x: line1.x, y: pt.y };
} else if (line1.y == line2.y) {
ret = { x: pt.x, y: line1.y };
} else {
var u = ((pt.x - line1.x) * (line2.x - line1.x)) + ((pt.y - line1.y) * (line2.y - line1.y));
var udenom = Math.pow(line2.x - line1.x, 2) + Math.pow(line2.y - line1.y, 2);
u /= udenom;
ret.x = line1.x + (u * (line2.x - line1.x));
ret.y = line1.y + (u * (line2.y - line1.y));
}
// if point is outside line segment, move to closest endpoint
if (line1.x < line2.x) {
if (ret.x < line1.x) {
return { x: line1.x, y: line1.y };
}
if (ret.x > line2.x) {
return { x: line2.x, y: line2.y };
}
} else if (line1.x > line2.x) {
if (ret.x > line1.x) {
return { x: line1.x, y: line1.y };
}
if (ret.x < line2.x) {
return { x: line2.x, y: line2.y };
}
}
if (line1.y < line2.y) {
if (ret.y < line1.y) {
return { x: line1.x, y: line1.y };
}
if (ret.y > line2.y) {
return { x: line2.x, y: line2.y };
}
} else if (line1.y > line2.y) {
if (ret.y > line1.y) {
return { x: line1.x, y: line1.y };
}
if (ret.y < line2.y) {
return { x: line2.x, y: line2.y };
}
}
return ret;
// This below if you want to send NULL if the point is outside the line
//var minx, maxx, miny, maxy;
//minx = Math.min(line1.x, line2.x);
//maxx = Math.max(line1.x, line2.x);
//miny = Math.min(line1.y, line2.y);
//maxy = Math.max(line1.y, line2.y);
//// if outside the segment, move inside
//if (ret.x >= minx && ret.x <= maxx) {
// // TODO
//}
//isValid = (ret.x >= minx && ret.x <= maxx) && (ret.y >= miny && ret.y <= maxy);
//return isValid ? ret : null;
}
function points2path(start, end, verbose) {
if (verbose) console.log('p2p s:' + start + " e:" + end);
if (start >= end) return 'M0,0';
var ret = '', first = true;
var lastp = { x: -1, y: -1 };
for (var i = start; i < end; i++) {
if (first)
ret += 'M' + points[i].x + ',' + points[i].y + ' ';
else {
if (points[i].x != lastp.x || points[i].y != lastp.y)
ret += 'L' + points[i].x + ',' + points[i].y + ' ';
}
lastp = points[i];
first = false;
}
if(verbose)console.log(ret);
return ret.length > 0 ? ret : 'M0,0';
}
function getCoords(e, that) {
if (isTouchEvent(e)) {
return { x: (e.targetTouches[0].pageX - r.offsetLeft), y: (e.targetTouches[0].pageY - r.offsetTop) };
//return e.targetTouches[0].clientX + ',' + e.targetTouches[0].clientY;
}
//console.log(e.clientX + ',' + e.pageX + ',' + r.offsetLeft);
//console.log(e.clientY + ',' + e.pageY + ',' + that.offsetTop);
return { x: (e.pageX - r.offsetLeft), y: (e.pageY - r.offsetTop) };
//return (e.pageX - r.offsetLeft) + ',' + (e.pageY - r.offsetTop);
//return e.clientX + ',' + e.clientY;
}
function down(e) {
var point = getCoords(e);
points = [point];
strokes = {};
glyph = null;
lastTouchedTarget = -1;
drawnPath = 'M' + point.x + ',' + point.y + ' ';
p.setAttribute('stroke', obj2col(drawcolor));
p.setAttribute('d', points2path(0, points.length));
isDown = true;
if (isTouchEvent(e)) e.preventDefault();
}
function move(e) {
if (isDown) {
var point = getCoords(e);
points.push(point);
drawnPath += 'L' + point.x + ',' + point.y + ' ';
//drawnPath += 'L' + getCoords(e, this) + ' ';
//p.setAttribute('d', points2path(0, points.length));
//console.log(drawnPath);
p.setAttribute('d', drawnPath);
// Is it on a new target? If so, vibrate.
if(vibrateOn && vibrate){
for (var t in targets) {
if (Math.pow(targets[t].x - point.x,2) + Math.pow(targets[t].y - point.y,2) <= cutoff) {
if(t != lastTouchedTarget){
lastTouchedTarget = t;
navigator.vibrate([30]);
}
break;
}
}
}
}
if (isTouchEvent(e))
e.preventDefault();
}
function up(e) {
if (!isDown) return;
isDown = false;
if (isTouchEvent(e))
e.preventDefault();
console.log("points.length = " + points.length);
var lastt = -1;
starti = 0;
for (var i = 0; i < points.length; i++) {
// Find closest target for each point
//console.log('i = ' + i);
var best = 999999, bestindex = -1;
for (var t in targets) {
var dx = targets[t].x - points[i].x;
var dy = targets[t].y - points[i].y;
var dist2 = dx * dx + dy * dy;
if (dist2 < best && dist2 <= cutoff) {
best = dist2;
bestindex = t;
}
}
if (bestindex != -1) {
// calculate vector to animate point along
points[i].vx = targets[bestindex].x - points[i].x;
points[i].vy = targets[bestindex].y - points[i].y;
// For now just set it, later animate
//points[i].x = targets[bestindex].x;
//points[i].y = targets[bestindex].y;
points[i].tx = targets[bestindex].x;
points[i].ty = targets[bestindex].y;
points[i].last = bestindex;
points[i].closeby = bestindex;
if (lastt != -1 && lastt != bestindex) {
// Stroke between targets detected
strokes['f' + Math.min(lastt, bestindex) + 't' + Math.max(lastt, bestindex)] = 1;
}
lastt = bestindex;
}else if (lastt != -1) {
points[i].last = lastt;
} else {
starti = i + 1;
}
}
// Now do it backwards to find next valid point
stopi = points.length;
lastt = -1;
for (i = points.length - 1; i >= starti; i--) {
//console.log('backtrack ' + i + ' of ' + points.length);
if (points[i].closeby !== undefined) {
lastt = points[i].closeby;
points[i].next = lastt;
} else if (lastt != -1) {
points[i].next = lastt;
} else {
stopi = i;
}
}
// Finally go through all (valid) points a last time to see where to interpolate them
for (i = starti; i < stopi; i++) {
//console.log('p' + i + ':{close:' + points[i].closeby + ', next:' + points[i].next + ', last:' + points[i].last)
if (points[i].closeby == undefined) {
var target;
if (points[i].last == points[i].next) {
// This is on a loop back to the same point
target = targets[points[i].last];
} else {
target = _pointOnLine(targets[points[i].last], targets[points[i].next], points[i]);
if (target === null) {
//console.log(pp(targets[points[i].last]) + ', ' + pp(targets[points[i].next]) + ', ' + pp(points[i]));
continue;
}
}
//console.log('target found: ' + pp(target));
points[i].vx = target.x - points[i].x;
points[i].vy = target.y - points[i].y;
// For now set, later animate
//points[i].x = target.x;
//points[i].y = target.y;
points[i].tx = target.x;
points[i].ty = target.y;
}
}
// Store start position for each point
for (i = starti; i < stopi; i++) {
points[i].ox = points[i].x;
points[i].oy = points[i].y;
}
// Glyph detection
var ss = '';
for (var s in strokes) ss += s + ',';
console.log('strokes: ' + ss);
var lastGlyph = glyph;
glyph = null;
for (var g in glyphs) {
var found = true;
for (var st in glyphs[g].strokes) {
//console.log('checking strokes for ' + st);
if (strokes[st] != 1) {
//console.log('NOPE!');
found = false;
break;
}
}
if (found) {
for (st in strokes) {
//console.log('checking glyph for ' + st);
if (!glyphs[g].strokes[st]) {
//console.log('NOPE!');
found = false;
break;
}
}
}
if (found) {
glyph = glyphs[g];
break;
}
}
lesson.newCorrect = false;
if (glyph != null) {
console.log('found ' + glyph.name[0]);
if (currentLesson != -1 && glyph.name[0] == lesson.glyphsleft[lesson.currentIndex].name[0]) {
console.log('drew correctly!');
txt.textContent = glyph.name[lesson.nameindex].toUpperCase();
if (!lesson.peeked) {
console.log('progress: ' + (lesson.glyphCount - lesson.glyphsleft.length) * 100.0 / lesson.glyphCount);
setProgressBar((lesson.glyphCount - lesson.glyphsleft.length + 1) * 100.0 / lesson.glyphCount);
}
if (!lesson.drewCorrect) {
// They have now drawn the same again (after completing it)
lesson.newCorrect = true;
}
lesson.drewCorrect = true;
} else {
var nameIndex = glyphsShown++ % glyph.name.length;
txt.textContent = glyph.name[nameIndex].toUpperCase();
lesson.drewIncorrectCount++;
}
} else {
txt.textContent = 'UNKNOWN';
lesson.drewIncorrectCount++;
}
//window.requestAnimationFrame(looper);
startLoop();
//console.log('starti ' + starti + ', stopi = ' + stopi);
//var path = points2path(starti, stopi, true);
//console.log(path);
//p.setAttribute('d', path);
}
function pp(p) {
return '{x:' + p.x + ', y:' + p.y + '}';
}
function showNextGlyph() {
if (currentLesson == -1) {
var index = glyphsShown % glyphs.length;
glyph = glyphs[index];
console.log('random glyph ' + index + ': ' + glyph);
txt.textContent = glyph.name[glyphsShown++ % glyph.name.length].toUpperCase();
} else {
if (lesson.currentIndex != undefined && !lesson.peeked && lesson.drewCorrect) {
// Mark this glyph as done
lesson.glyphsleft.splice(lesson.currentIndex, 1);
// Increase progress bar
}
if (lesson.glyphsleft.length == 0) {
txt.textContent = "LESSON DONE!";
glyph = null;
} else {
// Only switch glyph if there is more than one left.
var left = lesson.glyphsleft.length;
if (left != 1) {
// Never repeat the same glyph
var tempi = Math.floor((Math.random() * (lesson.glyphsleft.length-1)));
if (tempi >= lesson.currentIndex) tempi++;
lesson.currentIndex = tempi;
} else lesson.currentIndex = 0;
glyph = lesson.glyphsleft[lesson.currentIndex];
lesson.nameindex = Math.floor(Math.random() * glyph.name.length);
txt.textContent = glyph.name[lesson.nameindex].toUpperCase();
lesson.peeked = false;
lesson.drewCorrect = false;
lesson.drewIncorrectCount = 0;
}
}
if (settings.showOnNext) {
points = [];
for (var i = 0; i < glyph.points.length; i++) {
points.push(targets[glyph.points[i]]);
}
p.setAttribute('d', points2path(0, points.length));
} else {
points = [];
p.setAttribute('d', 'M0,0');
}
p.setAttribute('stroke', obj2col(okcolor));
}
function showCurrentGlyph() {
// TODO: when drawn wrong, show correct glyph
if (currentLesson != -1) {
glyph = lesson.glyphsleft[lesson.currentIndex];
txt.textContent = glyph.name[lesson.nameindex].toUpperCase();
}
if (!glyph) return;
lesson.peeked = true;
//txt.textContent = glyph.name[glyphsShown++ % glyph.name.length].toUpperCase();
points = [];
for (var i = 0; i < glyph.points.length; i++) {
points.push(targets[glyph.points[i]]);
}
p.setAttribute('d', points2path(0, points.length));
p.setAttribute('stroke', obj2col(okcolor));
}
function textClick(){
if(glyph != null && glyph.name.length > 1){
txt.textContent = glyph.name[glyphsShown++ % glyph.name.length].toUpperCase();
}
}
r.addEventListener('mousedown', down, false);
r.addEventListener('mousemove', move, false);
r.addEventListener('mouseup', up, false);
r.addEventListener('touchstart', down, false);
r.addEventListener('touchmove', move, false);
r.addEventListener('touchend', up, false);
r.addEventListener('mouseout', up, false);
var cc = 0, starttime = 0, lasttime = 0;
var started = new Date().getTime();
function startLoop() {
cc = 0;
starttime = lasttime = +new Date;
started = new Date().getTime();
window.requestAnimationFrame(looper, true);
}
var looptimeMs = 500.0;
function looper() {
var current = new Date().getTime(),
delta = current - started,
deltac = current - lasttime;
//console.log("frame " + cc + ", date = " + data + ', st = ' + starttime);
var dd = deltac / looptimeMs;
var dc = delta / looptimeMs;
if (dc > 1) dc = 1;
//console.log('delta: ' + delta + ', d2: ' + deltac + ', dd: '+dd + ', dc: '+dc);
if (delta >= looptimeMs) {
for (var j = starti; j < stopi; j++) {
points[j].x = points[j].tx;
points[j].y = points[j].ty;
}
txt.style.backgroundColor = '';
console.log('done looping for now');
} else {
for (var i = starti; i < stopi; i++) {
//points[i].x += points[i].vx * deltac;
//points[i].y += points[i].vy * deltac;
points[i].x += points[i].vx * dd;
points[i].y += points[i].vy * dd;
}
window.requestAnimationFrame(looper);
if (currentLesson != -1) {
var bounce = ((1 - (Math.cos(Math.PI * 2 * dc))) / 2);
var bounceByte = Math.floor(bounce * 150);
if (lesson.newCorrect) {
txt.style.backgroundColor = obj2col({ r: 0, g: bounceByte, b: 0 });
} else if (!lesson.drewCorrect) {
txt.style.backgroundColor = obj2col({ r: bounceByte, g: 0, b: 0 });
}
}
}
var path = points2path(starti, stopi);
var tocolor = glyph == null ? badcolor : okcolor;
p.setAttribute('stroke', interpolateColor(drawcolor, tocolor, dc));
p.setAttribute('d', path);
lasttime = current;
}
</script>
</body>
</html>