-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoten.js
312 lines (258 loc) · 9.71 KB
/
noten.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
"use strict";
var VF = Vex.Flow;
//Setting up note rendering
function setupVF(note){
if(document.querySelector("#scoreDiv").children.length > 0)
document.querySelector("#scoreDiv").removeChild(document.querySelector("#scoreDiv svg"));
var div = document.querySelector("#scoreDiv")
var renderer = new VF.Renderer(div, VF.Renderer.Backends.SVG);
renderer.resize(150, 120);
var context = renderer.getContext();
var stave = new VF.Stave(10, 0, 100);
stave.addClef(quizClef);
var notes = [
new VF.StaveNote({clef: quizClef, keys: [note], duration: "q" }),
//placeholders, not acutally visible, lib requires them for some reason, ignore
new VF.StaveNote({clef: "treble", keys: ["d/4"], duration: "q" }),
new VF.StaveNote({clef: "treble", keys: ["b/4"], duration: "qr" }),
new VF.StaveNote({clef: "treble", keys: ["c/4", "e/4", "g/4"], duration: "q" })
];
var voice = new VF.Voice({num_beats: 4, beat_value: 4});
voice.addTickables(notes);
var formatter = new VF.Formatter().joinVoices([voice]).format([voice], 400);
//console.log("Resized.");
context.setViewBox(0, 25, 90, 90);
stave.setContext(context).draw();
voice.draw(context, stave);
/*
if(window.innerWidth > 1199){
//Desktop
context.setViewBox(0, 25, 90, 90); //size
document.querySelector("svg").width = "150";
document.querySelector("svg").height = "120";
} else{
//Mobile
//context.setViewBox(0, 25, 60, 60); //size
document.querySelector("svg").width = "200";
document.querySelector("svg").height = "200";
}*/
}
/******************************************************************************************************/
// Globals
/******************************************************************************************************/
var piechart = document.querySelector("#piechart");
var pieChartContext = piechart.getContext("2d");
const answerHandler = document.querySelectorAll(".answer");
var quizClef = "treble";
/*
var possibleNotes = ["b/3", "c/4", "d/4", "e/4", "f/4", "g/4", "a/4", "b/4", "c/5", "d/5", "e/5"];
//var notes = ["D4", "C4", "E5", "F4", "H3"];
var notes = ["d/4", "c/4", "e/5", "f/4", "b/3"];
*/
var questionCounter = 0;
var correctNo = 0;
var appResponse;
/*
var aufg1 = {
note: [
{"a":"c/4", "l":["C","D","E","H"]},
{"a":"d/4", "l":["D","C","G","F"]},
{"a":"f/4", "l":["F","C","G","E"]},
{"a":"b/4", "l":["H","F","D","E"]},
]
};
*/
var aufg1 = {};
//setupVF(notes[questionCounter]);
function setClef(clef){
quizClef = clef;
if(document.querySelector("#setSelect").selectedIndex != 0){
if(document.querySelector("#setSelect").selectedIndex < 4){
console.log("Calling fetchQuestions for own tasks.");
fetchQuestions(false);
var note = JSON.parse(appResponse)["noten" + document.querySelector("#setSelect").selectedIndex];
} else {
console.log("Calling fetchQuestions for Beispielaufgaben.");
fetchQuestions(true);
var note = JSON.parse(appResponse)["note"];
}
console.log("note:");
console.dir(note);
for(var elm in note){
if(elm != "shuffle"){
if(clef == "treble")
note[elm]["a"] = note[elm]["a"][0].toLowerCase() + "/" + note[elm]["a"][1];
else
note[elm]["a"] = note[elm]["a"][0].toLowerCase() + "/" + (parseInt(note[elm]["a"][1])-2);
}
}
console.log("note neu:");
console.dir(note);
aufg1 = {note};
} else {
console.log("Did not call fetchQuestions");
aufg1 = {
note: [
{"a":"c/4", "l":["C","D","E","H"]},
{"a":"d/4", "l":["D","C","G","F"]},
{"a":"f/4", "l":["F","C","G","E"]},
{"a":"b/4", "l":["B","F","D","E"]},
]
};
for(var i = 0; i < aufg1.note.length; i++){
var notes = ["c", "d", "e", "f", "g", "a", "b"];
var randomIndex = parseInt(Math.round(Math.random()*notes.length))%notes.length;
aufg1.note[i].a = notes[randomIndex] + ((clef=="treble")?"/4":"/2");
aufg1.note[i].l[0] = aufg1.note[i].a[0].toUpperCase();
notes.splice(randomIndex, randomIndex+1);
for(var j = 1; j < aufg1.note[i].l.length; j++){
var randomIndex = parseInt(Math.round(Math.random()*notes.length))%notes.length;
console.log("random index: " + randomIndex);
console.log("random note:" + notes[randomIndex]);
aufg1.note[i].l[j] = notes[randomIndex].toUpperCase();
notes.splice(randomIndex, randomIndex+1);
}
}
}
console.log("Calling setupVF with " + aufg1.note[questionCounter]["a"]);
setupVF(aufg1.note[questionCounter]["a"]);
document.querySelector("#start").style.display = "none";
document.querySelector("#app").style.display = "block";
updateJSON(aufg1.note, true);
}
//Draws a pie chart showing the relation between correct and incorrect answers
function drawPieChart(){
var correctCount = correctNo;
if(questionCounter == 0)
correctCount = 1;
var questionCounterAlt = questionCounter;
if(questionCounter == 0)
questionCounterAlt = 1;
//green
pieChartContext.moveTo(100, 100);
pieChartContext.arc(100, 100, 90.5, 0, Math.PI*(correctCount/questionCounterAlt)*2);
pieChartContext.fillStyle = "#a7e3bc";
pieChartContext.fill();
//red
pieChartContext.beginPath();
pieChartContext.moveTo(100, 100);
pieChartContext.arc(100, 100, 90.5, Math.PI*(correctCount/questionCounterAlt)*2, Math.PI*2);
pieChartContext.fillStyle = "#e19898";
pieChartContext.fill();
}
//Updates question divs and VexFlow note display
function updateJSON(tasks, initialCall){
//console.log("updateJSON was called with initialCall: " + initialCall);
//console.log("Called updateJSON, questionCounter: " + questionCounter);
//var correctIndex = Math.floor(Math.random()*3);
//Re-set color after indicator color for answering
for(var i = 0; i < answers.length; i++)
answers[i].style.background = "#AFAFAF";
if(questionCounter < tasks.length && !initialCall)
questionCounter++;
if(questionCounter < tasks.length){
//Fills answer-divs
var possibleAnswers = tasks[questionCounter].l.slice(0);
possibleAnswers = possibleAnswers.shuffle().slice(0);
console.log(possibleAnswers);
for(var i = 0; i < 4; i++){
answerHandler[i].innerHTML = possibleAnswers[i];
}
setupVF(tasks[questionCounter].a);
console.log("Called setupVF with: " + tasks[questionCounter].a);
/*
if(questionCounter < aufg1["note"].length && !initialCall)
questionCounter++;
*/
//Drawing indicators for correct and incorrect answers, progress bar, and pie chart
}
drawPieChart();
document.querySelector("#correctNo").innerHTML = correctNo;
document.querySelector("#wrongNo").innerHTML = questionCounter - correctNo;
document.querySelector("#progressBar").style.width = questionCounter/tasks.length*100 + "%";
console.log("Progress percentage: " + questionCounter/tasks.length*100 + "%");
}
function resetApp(){
questionCounter = 0;
correctNo = 0;
window.onload();
drawPieChart();
}
//Is called when the user clicks on one of the answer divs, invokes update after 1 second, to show correct answer
function answer(note){
//Debug
/*
console.log("Correct answer would be:" + notes[questionCounter]);
console.log("Answer was:" + note);
*/
//Check if answer was correct
if(note == aufg1["note"][questionCounter].a[0]){
correctNo++;
}
if(questionCounter == aufg1["note"].length - 1){
setTimeout(()=>{
document.querySelector("#end").style.display = "block";
document.querySelector("#answers").style.display = "none";
}, 1000);
}
//Colors indicate the correct and incorrect answers
for(var i = 0; i < answerHandler.length; i++){
//if(answerHandler[i].innerHTML == notes[questionCounter])
try{
//if(answerHandler[i].innerHTML.toLowerCase().indexOf(aufg1.note[questionCounter].a) != -1)
if(answerHandler[i].innerHTML.toLowerCase()[0] == aufg1.note[questionCounter].a[0]){
answerHandler[i].style.background = "#a7e3bc"; //green
} else {
answerHandler[i].style.background = "#e19898"; //red
}
} catch(e){
console.error(e);
console.log("Question Counter: " + questionCounter);
}
}
//Hold back function to let user see what answers were correct or incorrect
setTimeout(()=>{updateJSON(aufg1.note, false)}, 1000);
}
//Function to let user choose preferred clef
var clefOptions = document.querySelectorAll(".clefOption");
for(var i = 0; i < clefOptions.length; i++){
clefOptions[i].addEventListener("click", function(){
if(this.hasAttribute("treble"))
setClef("treble");
else
setClef("bass");
});
}
//Fetch new questions from server as json
function fetchQuestions(Beispielaufgaben){
var xh = new XMLHttpRequest();
//xh.responseType = "json"; //synchronous request can't set reponseType per definition
xh.onreadystatechange = function(){
if(this.readyState == 4 && this.status == 200){
appResponse = this.response;
//console.log(this.response);
}
};
if(Beispielaufgaben)
xh.open("GET", "https://idefix.informatik.htw-dresden.de/it1/beleg/noten-aufgaben.js", false); //CAUTION, request is set to synchronous because app can't continue without result
else
xh.open("GET", "aufgaben.json", false);
xh.send();
}
//Setting up event-listeners for answering
var answers = document.querySelectorAll(".answer");
for(var i = 0; i < answers.length; i++){
answers[i].addEventListener("click", function(){
answer((this.innerHTML).toLowerCase());
});
}
//Initialize pie chart
drawPieChart();
//Hiding app, showing start div
window.onload = function(){
document.querySelector("#start").style.display = "block";
document.querySelector("#app").style.display = "none";
document.querySelector("#answers").style.display = "block";
document.querySelector("#end").style.display = "none";
//updateJSON(aufg1.note, true);
}