-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtutorials.js
456 lines (417 loc) · 16 KB
/
tutorials.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
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
var tutorialMode = false;
var currentTutorial;
var currentTutorialIndex = 0;
var lastKnownCoord;
var tutorials = [];
var unit = 150;
//exports = module.exports;
// start the Tutorial
function startTutorial(canvas, ctx) {
tutorialMode = true;
// Initialize only the first time
if (tutorials.length == 0) {
initializeTutorials();
}
else {
// Retrieve the most recently failed tutorial
currentTutorialIndex = getLastFailedTutorial();
if (currentTutorialIndex == -1) {
setInstruction("You successfully passed all the tutorials. Yayy");
}
}
currentTutorial = tutorials[currentTutorialIndex];
setInstruction(`Tutorial ${currentTutorial.name}.`);
setInstruction(`${currentTutorial.draw_instructions}`);
}
// start the Tutorial
function doTutorial(canvas, ctx, index) {
tutorialMode = true;
// Initialize only the first time
if (tutorials.length == 0) {
initializeTutorials();
}
else {
if (tutorials.length >= index) {
// Retrieve the most recently failed tutorial
currentTutorialIndex = index - 1;
if (currentTutorialIndex == -1) {
setInstruction("You successfully passed all the tutorials. Yayy");
}
}
}
currentTutorial = tutorials[currentTutorialIndex];
setInstruction(`Tutorial ${currentTutorial.name}.`);
//setInstruction(`${currentTutorial.draw_instructions}`);
setTimeout(setInstruction, 2000, `${currentTutorial.draw_instructions}`);
}
function resetCounters() {
tutorialMode = false;
tutorials = [];
currentTutorialIndex = 0;
currentTutorial = null;
lastKnownCoord = { x: -1, y: -1 };
}
// Reset the tutorials
function resetTutorials() {
resetCounters();
setInstruction("Tutorials reset. Restart the tutorials by clicking on 'Start Tutorials'.");
}
//exports.initializeTutorials = function initializeTutorials() {
function initializeTutorials() {
// Horizontal line
var tutorial = new Tutorial(1, "Horizontal line", 1, "0", "h",
"Move straight one unit from left to right",
"The angle was incorrect. Redraw a horizontal line.",
"The length was incorrect. Redraw a horizontal line.",
"The orientation was incorrect. Redraw a horizontal line.", false, 0);
tutorials.push(tutorial);
// Vertical line
tutorial = new Tutorial(2, "Vertical line", 1, "90", "v",
"Move straight one unit from up to down",
"The angle was incorrect. Redraw a vertical line.",
"The length was incorrect. Redraw a vertical line.",
"The orientation was incorrect. Redraw a vertical line.", false, 0);
tutorials.push(tutorial);
// Left to right inclined line
tutorial = new Tutorial(3, "Inclined line", 1, "45", "dr",
"Move inclined, 45 degrees, one unit from left down to right up",
"The angle was incorrect. Redraw an inclined line.",
"The length was incorrect. Redraw an inclined line.",
"The orientation was incorrect. Redraw an inclined line.", false, 0);
tutorials.push(tutorial);
// Right to left inclined line
tutorial = new Tutorial(4, "Inclined line", 1, "45", "dl",
"Move inclined, 45 degrees, one unit from right down to left up",
"The angle was incorrect. Redraw an inclined line.",
"The length was incorrect. Redraw an inclined line.",
"The orientation was incorrect. Redraw an inclined line.", false, 0);
tutorials.push(tutorial);
// Connecting Points
var con2 = new Tutorial(5.1, "Connecting line", 1, "90", "v",
"Move straight one unit from up to down from the connecting point",
"The angle was incorrect. Redraw a vertical line from the connecting point.",
"The length was incorrect. Redraw a vertical line from the connecting point.",
"The orientation was incorrect. Redraw a vertical line.", true, 0);
var con1 = new Tutorial(5, "Connecting line", 1, "45", "h",
"Move straight one unit from left to right",
"The angle was incorrect. Redraw a horizontal line.",
"The length was incorrect. Redraw a horizontal line.",
"The orientation was incorrect. Redraw a horizontal line.", false, 0, con2);
tutorials.push(con1);
// Drawing arc with three connecting lines [inclined, straight and declined]
// Arc - Declined line
var arc3 = new Tutorial(6.2, "Arc line 3", 2, "45", "dl",
"Move declined, 45 degrees, 2 unit from the start to right down",
"The angle was incorrect. Redraw a declined line from the connecting point.",
"The length was incorrect. Redraw a declined line from the connecting point.",
"The orientation was incorrect. Redraw a declined line.", true, 0);
// Arc - Straight line
var arc2 = new Tutorial(6.1, "Arc line", 2, "0", "h",
"Move straight 2 unit from start to right",
"The angle was incorrect. Redraw a horizontal line from the connecting point.",
"The length was incorrect. Redraw a horizontal line from the connecting point.",
"The orientation was incorrect. Redraw a horizontal line.", true, 0, arc3);
// Arc - Inclined line
var arc1 = new Tutorial(6, "Arc line", 2, "45", "dr",
"Move inclined, 45 degrees, 2 unit from left down to right up",
"The angle was incorrect. Redraw an inclined line.",
"The length was incorrect. Redraw an inclined line.",
"The orientation was incorrect. Redraw an inclined line.", false, 0, arc2);
tutorials.push(arc1);
//triangle
var tri3 = new Tutorial(7.2, "Triangle line", 3, "45", "h",
"Move straight 3 unit from left to right",
"The angle was incorrect. Redraw a horizontal line from the connecting point.",
"The length was incorrect. Redraw a horizontal line from the connecting point.",
"The orientation was incorrect. Redraw a horizontal line.", true, 0);
var tri2 = new Tutorial(7.1, "Triangle line", 3, "45", "dr",
"Move inclined, 45 degrees, 3 unit from right up to left down",
"The angle was incorrect. Redraw an inclined line from the connecting point.",
"The length was incorrect. Redraw an inclined line from the connecting point.",
"The orientation was incorrect. Redraw an inclined line.", true, 0, tri3);
var tri1 = new Tutorial(7, "Triangle line", 3, "45", "dl",
"Move inclined, 45 degrees, 3 unit from right down to left up",
"The angle was incorrect. Redraw an inclined line.",
"The length was incorrect. Redraw an inclined line.",
"The orientation was incorrect. Redraw an inclined line.", false, 0, tri2);
tutorials.push(tri1);
//square
var sq4 = new Tutorial(8.3, "Square line", 3, "90", "v",
"Move straight 3 unit from down to up",
"The angle was incorrect. Redraw a vertical line from the connecting point.",
"The length was incorrect. Redraw a vertical line from the connecting point.",
"The orientation was incorrect. Redraw a vertical line.", true, 0);
var sq3 = new Tutorial(8.2, "Square line", 3, "0", "h",
"Move straight 3 unit from right to left",
"The angle was incorrect. Redraw a horizontal line from the connecting point.",
"The length was incorrect. Redraw a horizontal line from the connecting point.",
"The orientation was incorrect. Redraw a horizontal line.", true, 0, sq4);
var sq2 = new Tutorial(8.1, "Square line", 3, "90", "v",
"Move straight 3 unit from up to down",
"The angle was incorrect. Redraw a vertical line from the connecting point.",
"The length was incorrect. Redraw a vertical line from the connecting point.",
"The orientation was incorrect. Redraw a vertical line.", true, 0, sq3);
var sq1 = new Tutorial(8, "Square line", 3, "0", "h",
"Move straight 3 unit from left to right",
"The angle was incorrect. Redraw a horizontal line.",
"The length was incorrect. Redraw a horizontal line.",
"The orientation was incorrect. Redraw a horizontal line.", false, 0, sq2);
tutorials.push(sq1);
//hut
var tri2 = new Tutorial(9.5, "House line", 3, "45", "dr",
"Move inclined, 45 degrees, 3 units from right up to left down",
"The angle was incorrect. Redraw an inclined line from the connecting point.",
"The length was incorrect. Redraw an inclined line from the connecting point.",
"The orientation was incorrect. Redraw an inclined line.", true, 0);
var tri1 = new Tutorial(9.4, "House line", 3, "45", "dl",
"Move inclined, 45 degrees, 3 units from right down to left up",
"The angle was incorrect. Redraw an inclined line from the connecting point.",
"The length was incorrect. Redraw an inclined line from the connecting point.",
"The orientation was incorrect. Redraw an inclined line.", true, 0, tri2);
sq4 = new Tutorial(9.3, "House line", 3, "90", "v",
"Move straight 3 units from down to up",
"The angle was incorrect. Redraw a vertical line from the connecting point.",
"The length was incorrect. Redraw a vertical line from the connecting point.",
"The orientation was incorrect. Redraw a vertical line.", true, 0, tri1);
sq3 = new Tutorial(9.2, "House line", 3, "0", "h",
"Move straight 3 units from left to right",
"The angle was incorrect. Redraw the horizontal line from the connecting point.",
"The length was incorrect. Redraw the horizontal line from the connecting point.",
"The orientation was incorrect. Redraw a horizontal line.", true, 0, sq4);
sq2 = new Tutorial(9.1, "House line", 3, "90", "v",
"Move straight 3 units from up to down",
"The angle was incorrect. Redraw a vertical line from the connecting point.",
"The length was incorrect. Redraw a vertical line from the connecting point.",
"The orientation was incorrect. Redraw a vertical line.", true, 0, sq3);
sq1 = new Tutorial(9, "House line", 3, "0", "h",
"Move straight 3 units from right to left",
"The angle was incorrect. Redraw a horizontal line.",
"The length was incorrect. Redraw a horizontal line.",
"The orientation was incorrect. Redraw a horizontal line.", false, 0, sq2);
tutorials.push(sq1);
return tutorials;
}
function getLastFailedTutorial() {
// Iterate over the tutorials and get the last failed one in the list
for (i = 0; i < tutorials.length; i++) {
if (tutorials[i].score == 0) {
return i;
}
}
// When all the tutorials are already passed
return -1;
}
function tutorialModeCheck() {
if (tutorialMode && currentTutorial.score == 0) {
return true;
}
}
function getSlope(stack, retAbs = false) {
denom = stack[stack.length - 1].x - stack[0].x;
if (denom == 0) {
throw new Error('Invalid dividend');
}
slope = (stack[stack.length - 1].y - stack[0].y) / denom;
return retAbs ? Math.abs(slope) : slope;
}
function checkOrientation(stack) {
if (currentTutorial.orientation == "h") {
if (Math.abs(stack[stack.length - 1].x - stack[0].x) > Math.abs(stack[stack.length - 1].y - stack[0].y)) {
return true;
}
else {
return false;
}
}
else if (currentTutorial.orientation == "v") {
if (Math.abs(stack[stack.length - 1].x - stack[0].x) < Math.abs(stack[stack.length - 1].y - stack[0].y)) {
return true;
}
else {
return false;
}
}
else if (currentTutorial.orientation == "dl") {
// Returns the absolute slope
slope = getSlope(stack);
if (slope < 0) {
// If negative slope, then wrong direction
setInstruction("Line should be drawn from right down to left up.");
return false;
}
if (slope >= 0.75 && slope <= 1.25) {
return true;
}
else {
if (slope < 0.75) {
setInstruction("Line too low. Increase the angle of inclination.");
}
else {
setInstruction("Line too high. Decrease the angle of inclination.");
}
return false;
}
}
else if (currentTutorial.orientation == "dr") {
// Returns the absolute slope
slope = getSlope(stack);
if (slope > 0) {
// If positive slope, then wrong direction
setInstruction("Line should be drawn from left down to right up.");
return false;
}
if (slope >= -1.25 && slope <= -0.75) {
return true;
}
else {
if (slope < -1.25) {
setInstruction("Line too high. Increase the angle of inclination.");
}
else {
setInstruction("Line too low. Decrease the angle of inclination.");
}
return false;
}
}
}
function acceptableLength(length) {
var xunit = currentTutorial.length * unit;
if (length == xunit) {
return true;
}
var diff = 0
if (length > xunit) {
diff = length - xunit;
}
else {
diff = xunit - length;
}
var range = 20 / 100 * xunit;
if (diff > range) {
return false;
}
else {
return true;
}
}
function checkForTutorialScore(stack) {
if (!tutorialModeCheck()) {
// Return if this is not tutorial
return;
}
if (!acceptableLength(stack.length)) {
setInstruction(`${currentTutorial.length_error_instructions}`);
//setInstruction(`Clearing the last line.`);
//setTimeout(setInstruction, 4000, `Clearing the last line.`);
currentTutorial.score = 0;
undo(canvas, ctx);
return;
}
else {
currentTutorial.score = 1;
}
if (!checkOrientation(stack)) {
setInstruction(`${currentTutorial.orientation_error_instructions}`);
//setInstruction(`Clearing the last line.`);
//setTimeout(setInstruction, 4000, `Clearing the last line.`);
currentTutorial.score = 0;
undo(canvas, ctx);
return;
}
else {
currentTutorial.score = 1;
}
if (currentTutorial.score == 1) {
if (currentTutorial.nextTutorial) {
currentTutorial = currentTutorial.nextTutorial;
lastKnownCoord = { x: stack[stack.length - 1].x, y: stack[stack.length - 1].y };
setInstruction(`Success with Tutorial ${currentTutorial.name}.`);
//setInstruction(`${currentTutorial.draw_instructions}.`);
setTimeout(setInstruction, 2000, `${currentTutorial.draw_instructions}.`);
}
else {
setInstruction('Success.');
// Smoothen if it's an arc
if (currentTutorial.name == 'Arc line 3') {
smoothenArc();
}
resetCounters();
}
}
else {
setInstruction(`Repeating, Tutorial ${currentTutorial.name}`);
//setInstruction(`${currentTutorial.draw_instructions}`);
setTimeout(setInstruction, 2000, `${currentTutorial.draw_instructions}.`);
}
}
function checkIfCoordInRange(mouseX, mouseY, lastPoint) {
var xdiff = 0;
if (mouseX > lastPoint.x) {
xdiff = mouseX - lastPoint.x;
}
else {
xdiff = lastPoint.x - mouseX;
}
var ydiff = 0;
if (mouseY > lastPoint.y) {
ydiff = mouseY - lastPoint.y;
}
else {
ydiff = lastPoint.y - mouseY;
}
if (xdiff <= 10 && ydiff <= 10) {
setInstruction('');
return true;
}
else {
if ((mouseX - lastPoint.x) > 10) {
setInstruction('Move Left for the connecting point.');
return false;
}
else if ((lastPoint.x - mouseX) > 10) {
setInstruction('Move Right for the connecting point.');
return false;
}
if ((mouseY - lastPoint.y) > 10) {
setInstruction('Move Up for the connecting point.');
return false;
}
else if ((lastPoint.y - mouseY) > 10) {
setInstruction('Move Down for the connecting point.');
return false;
}
}
}
function checkForLineLength(stack) {
var xunit = currentTutorial.length * unit;
if (tutorialModeCheck() && stack.length === xunit) {
setInstruction('Stop.');
}
}
function checkForContinuePointMode(mouseX, mouseY) {
if (tutorialModeCheck() && currentTutorial.continueFromLast) {
drawMode = false;
document.getElementById('mode').innerText = 'Current Mode: Placement';
if (checkIfCoordInRange(mouseX, mouseY, lastKnownCoord)) {
drawMode = true;
document.getElementById('mode').innerText = 'Current Mode: Draw';
}
}
}
class Tutorial {
constructor(id, name, length, angle, orientation, draw_instructions, angle_error_instructions,
length_error_instructions, orientation_error_instructions, continueFromLast, score, nextTutorial) {
this.id = id;
this.name = name;
this.length = length;
this.angle = angle;
this.orientation = orientation;
this.draw_instructions = draw_instructions;
this.angle_error_instructions = angle_error_instructions;
this.length_error_instructions = length_error_instructions;
this.orientation_error_instructions = orientation_error_instructions;
this.continueFromLast = continueFromLast;
this.score = score;
this.nextTutorial = nextTutorial;
}
}