-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
324 lines (306 loc) · 11.9 KB
/
index.html
File metadata and controls
324 lines (306 loc) · 11.9 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BinaryNumberContentViewer</title>
<!-- Since GitHub Raw files use text/plain as MIME type the script cannot be executed when linking directly, instead routed over jsdelivr-->
<script src="https://cdn.jsdelivr.net/gh/MicroContent/BinaryNumberContentViewer@master/seamlessjs/seamless.child.min.js"></script>
<script type="application/javascript">
var connection = window.seamless.connect({
url: window.location.url,
allowStyleInjection: true,
});
connection.receive(function(data) {
switch(data.type) {
case 'setContent':
// parent for varying type of input sends data as special object; data.main[0] is the array of arrays with your message inside
window.data = {number: parseInt(data.main[0].number,10)}
binaryString = parseInt(data.main[0].number,10).toString(2);
solution = binaryString.split('').map(function (c) {
return c === "1"
});
init();
break;
case 'submit':
if (data.value === 'true'){
onSubmit();
}
break;
case 'getXapiStatements':
stmts = data.stmts;
break;
}
});
function init() {
if(window.data === undefined) {
window.data = {number: 255};
binaryString
}
document.getElementById('question').innerHTML = "What is the binary representation of " +
window.data.number + "?<br />Click on the switches to set the correct binary number.";
}
function updateLayout() {
connection.send({
type: 'updateLayout'
});
}
function sendStatement(stmt) {
connection.send({
type: 'sendXapiStatement',
stmt: stmt
});
}
function getStatements() {
connection.send({
type: 'getXapiStatements'
});
}
</script>
</head>
<body>
<!-- EXAMPLE CODE - EDIT BELOW TO VIEW DATA -->
<div style="text-align: center;">
<p id="question"></p>
<button id="showHintBtn" class="btn btn-primary" onclick="showHideHint()">Show Hint</button>
<canvas id="canvas" width="600" height="300" style="width: 100%"></canvas>
<p id="solution"></p>
</div>
<div id="statistics"></div>
<script type="application/javascript">
function showHideHint() {
showHint = !showHint;
draw();
document.getElementById("showHintBtn").innerHTML = showHint ? "Hide Hint" : "Show Hint";
}
function draw() {
ctx.clearRect(0, 0, 600, 300);
for (var i = 0; i < 8; i++) {
if (showHint) {
ctx.beginPath();
ctx.font = "24px Arial";
var text = (Math.pow(2, (7 - i))).toString();
var twh = Math.round(ctx.measureText(text).width / 2);
ctx.fillStyle = "#000000";
ctx.fillText(text, i * 60 + 60 + 20 - twh, 60);
ctx.closePath();
}
ctx.beginPath();
ctx.strokeStyle = isShowingSolution && active[i] && !solution[i] ? "#ff0000" : "#000000";
ctx.lineWidth = isShowingSolution && active[i] && !solution[i] ? 3 : 1;
ctx.arc(i * 60 + 60 + 20, 100, 20, 0, Math.PI * 2, false);
ctx.stroke();
if (active[i] || (isShowingSolution && solution[i])) {
ctx.fillStyle = active[i] ? ((isShowingSolution && !solution[i]) ? "#66ff66" : "#00aa00") : "#ff6666";
ctx.fill();
}
ctx.closePath();
ctx.lineWidth = 1;
ctx.beginPath();
ctx.font = "60px Arial";
var text = active[i] ? "1" : "0";
var twh = Math.round(ctx.measureText(text).width / 2);
ctx.fillStyle = !isShowingSolution || (solution[i] === active[i]) ? "#000000" : "#ff0000";
ctx.fillText(text, i * 60 + 60 + 20 - twh, 200);
ctx.closePath();
if (isShowingSolution) {
ctx.beginPath();
ctx.font = "60px Arial";
var text = solution[i] ? "1" : "0";
var twh = Math.round(ctx.measureText(text).width / 2);
ctx.fillStyle = "#00aa00";
ctx.fillText(text, i * 60 + 60 + 20 - twh, 260);
ctx.closePath();
}
}
}
function onCanvasClicked(event) {
if (!isShowingSolution) {
var outerRect = {left: 0, top: 0};
var rect = canvas.getBoundingClientRect();
var x = (event.clientX - rect.left - outerRect.left) * (600 / (rect.right - rect.left));
var y = (event.clientY - rect.top - outerRect.top) * (300 / (rect.bottom - rect.top));
for (var i = 0; i < 8; i++) {
if (Math.pow(x - (i * 60 + 60 + 20), 2) + Math.pow(y - 100, 2) <= 400) {
active[i] = !active[i];
onInteraction();
break;
}
}
}
draw();
}
function onInteraction() {
var stmt = {};
// actor is set by hosting application
stmt.actor = undefined;
stmt.verb = {
"id": "http://adlnet.gov/expapi/verbs/interacted",
"display": {
"de-DE": "interagierte",
"en-US": "interacted",
"fr-FR": "a interagi",
"es-ES": "interactuó"
}
};
stmt.object = getXapiObject();
var responses = [];
var allRight = true;
for (var i = 0; i < 8; i++) {
allRight = allRight && (active[i] === solution[i]);
if (active[i]) {
responses.push(Math.pow(2, (7 - i)).toString());
}
}
stmt.result = {
response: responses.reduce(function (acc, e) {
return (acc.length > 0 && e.length > 0) ? acc + "[,]" + e : acc.length > 0 ? acc : e;
}, ""),
success: allRight
};
sendStatement(stmt);
}
function getXapiObject() {
// may be called on init()
binaryString = window.data.number.toString(2);
binaryString = "00000000".substr(0,8 -binaryString.length).concat(binaryString);
solution = binaryString.split('').map(function (c) {
return c === "1"
});
return {
"objectType": "Activity",
// id is set by hosting application
"id": undefined,
"definition": {
"description": {
"de-AT": "What is the binary representation of " + window.data.number + "?"
},
"type": "http://adlnet.gov/expapi/activities/cmi.interaction",
"interactionType": "choice",
"choices": [128, 64, 32, 16, 8, 4, 2, 1].map(function (e, i) {
return {
id: i.toString(),
description: {
"de-AT": i.toString()
}
}
}),
"correctResponsesPattern": [solution.map(function (e, i) {
return e ? Math.pow(2, (7 - i)).toString() : ""
})
.reduce(function (acc, e) {
return (acc.length > 0 && e.length > 0) ? acc + "[,]" + e : acc.length > 0 ? acc : e;
}, "")]
}
};
}
function onSubmit() {
if (!isShowingSolution) {
var responses = [];
var allRight = true;
// assign classes and disable
for (var i = 0; i < 8; i++) {
allRight = allRight && (active[i] === solution[i]);
if (active[i]) {
responses.push(Math.pow(2, (7 - i)).toString());
}
}
var stmt = {};
// actor is set by hosting application
stmt.actor = undefined;
stmt.verb = {
"id": "http://adlnet.gov/expapi/verbs/answered",
"display": {
"de-DE": "beantwortete",
"en-US": "answered",
"fr-FR": "a répondu",
"es-ES": "contestó"
}
};
stmt.object = getXapiObject();
stmt.result = {
response: responses.reduce(function (acc, e) {
return (acc.length > 0 && e.length > 0) ? acc + "[,]" + e : acc.length > 0 ? acc : e;
}, ""),
success: allRight
};
sendStatement(stmt);
isShowingSolution = true;
document.getElementById('solution').innerHTML = "Your number is: "
+ active.map(function (e, i) {
return e ? Math.pow(2, 7 - i) : 0;
}).reduce(function (a, e) {
return a + e;
}, 0) + "<br />" + (allRight ? "Correct!" : "Try again!");
getStatements();
setTimeout(function() { showStatistics(stmt.result) }, 1000);
draw();
} else {
// remove classes and enable
isShowingSolution = false;
document.getElementById('solution').innerHTML = "";
hideStatistics();
draw();
}
updateLayout();
}
function showStatistics(result) {
var answered = [];
stmts.forEach(function(s) {
if (s.verb.id.indexOf('answered') !== -1) {
answered.push(s);
}
});
if(answered.length === 0) return;
var correct = [];
var wrongRespones = {};
var answeredWrong = 0;
answered.forEach(function(s) {
if (s.result.success) {
correct.push(s);
} else {
if (wrongRespones[s.result.response] === undefined) {
wrongRespones[s.result.response] = 1;
} else {
wrongRespones[s.result.response]++;
}
if (!result.success && result.response === s.result.response) {
answeredWrong++;
}
}
});
var max = {key: '', value: 0};
for (var response in wrongRespones) {
if (wrongRespones[response] > max.value) {
max.key = response;
max.value = wrongRespones[response];
}
};
var showLearningMsg = (max.key === result.response && max.value > 4) ? true : false;
document.getElementById('statistics').innerHTML =
'Card answered: ' + answered.length + ' time(s)' +
'<br>Ansered correct: ' + correct.length + ' time(s) (' + Math.round(correct.length/answered.length*10000)/100 + '%)' +
(answeredWrong > 1 ? ('<br>You\'ve made the same mistake before ' + (answeredWrong-1) +' time(s).') : ('')) +
(showLearningMsg ? ('<br>This seems to be your most common mistake, try to reflect why you keep answering that way!') : (''));
updateLayout();
}
function hideStatistics() {
document.getElementById('statistics').innerHTML = '';
}
init();
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext("2d");
var active = [false, false, false, false, false, false, false, false];
var stmts = [];
var binaryString = window.data.number.toString(2);
var solution = binaryString.split('').map(function (c) {
return c === "1"
});
var showHint = false;
var isShowingSolution = false;
draw();
canvas.addEventListener('click', onCanvasClicked);
updateLayout();
</script>
<!-- END OF EXAMPLE CODE -->
</body>
</html>