Commit eb1a009f authored by Mattia Monga's avatar Mattia Monga

Cifre cancellabili indipendentemente

parent c521c365
Pipeline #130619535 passed with stage
in 53 seconds
......@@ -2,21 +2,34 @@
// Basic idea from https://gist.github.com/peshoicov/ab3286875d980948ad3f5f434fec37a9
// and http://bencentra.com/code/2014/12/05/html5-canvas-touch-events.html
Conticini.reset_canvases = function () {
Conticini.reset_canvases = function (just_this = null) {
var drawing = null;
var mousePos = {x:0, y:0};
var lastPos = mousePos;
var isMobile = ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch;
const canvasDim = 0.9*(Conticini.CONTICINO.clientWidth / 2);
Conticini.CIFRA_D.has_listeners = false;
Conticini.CIFRA_U.has_listeners = false;
if (just_this === null) {
setup_canvas(Conticini.CIFRA_D);
setup_canvas(Conticini.CIFRA_U);
} else {
setup_canvas(just_this);
if (just_this === Conticini.CIFRA_D)
Conticini.CIFRA_U.is_readonly = true;
else
Conticini.CIFRA_D.is_readonly = true;
}
setup_canvas(Conticini.CIFRA_D);
setup_canvas(Conticini.CIFRA_U);
Conticini.check_msg();
function setup_canvas(canvas){
// Setup canvas ..
const ctx = canvas.getContext('2d');
canvas.is_blank = true; // blank is ok in cifra_d
canvas.is_readonly = false; // don't draw on a readonly canvas!
canvas.width = canvasDim;
canvas.height = canvas.width;
......@@ -26,17 +39,26 @@ Conticini.reset_canvases = function () {
ctx.lineCap = 'round';
// mouse/touch events ..
canvas.addEventListener((isMobile ? 'touchstart' : 'mousedown'), function(e) {
drawing = ctx.canvas.id;
lastPos = get_mouse_pos(canvas, e);
mousePos = lastPos;
});
canvas.addEventListener((isMobile ? 'touchmove' : 'mousemove'), function(e) {
mousePos = get_mouse_pos(canvas, e);
});
canvas.addEventListener((isMobile ? 'touchend' : 'mouseup'), function(e) {
drawing = null;
});
if (!canvas.has_listeners) {
canvas.addEventListener((isMobile ? 'touchstart' : 'mousedown'), function(e) {
if (!canvas.is_readonly){
drawing = ctx.canvas.id;
lastPos = get_mouse_pos(canvas, e);
mousePos = lastPos;
}
});
canvas.addEventListener((isMobile ? 'touchmove' : 'mousemove'), function(e) {
if (!canvas.is_readonly){
mousePos = get_mouse_pos(canvas, e);
}
});
canvas.addEventListener((isMobile ? 'touchend' : 'mouseup'), function(e) {
if (!canvas.is_readonly){
drawing = null;
}
});
canvas.has_listeners = true;
}
// drawing ..
window.requestAnimFrame = (function(callback) {
......@@ -69,6 +91,7 @@ Conticini.reset_canvases = function () {
function render_canvas(ctx) {
if (drawing === ctx.canvas.id) {
ctx.canvas.is_blank = false;
ctx.moveTo(lastPos.x, lastPos.y);
ctx.lineTo(mousePos.x, mousePos.y);
ctx.stroke();
......
......@@ -30,9 +30,5 @@ var Conticini = {
MSG_KO: localStorage.getItem('MSG_KO') || "HAI SCRITTO MALE, RIPROVA!",
},
// expected result
expected: null,
};
......@@ -15,7 +15,7 @@ Conticini.nn = {
const outputMap = await myOnnxSession.run([inputTensor]);
const outputData = outputMap.values().next().value.data;
const probabilities = this.softmax(outputData);
console.log(probabilities);
console.log(canvas.id, Array.from(probabilities.map((x) => 100*x).entries()).toString());
return probabilities.findIndex((x) => x > .5); // -1 is not found
},
......
......@@ -9,6 +9,8 @@ Conticini.randomize = function() {
+ Math.random()*(Conticini.cfg.MAX_OPERANDO - Conticini.cfg.MIN_OPERANDO + 1));
op = Conticini.cfg.OPS[Math.floor(Math.random()*Conticini.cfg.OPS.length)];
Conticini.expected = Math.floor(eval(op_sx + op + op_dx));
Conticini.expected_d = Math.floor(Conticini.expected / 10);
Conticini.expected_u = Conticini.expected % 10;
} while (Conticini.expected < Conticini.cfg.MIN_EXPECTED || Conticini.expected > Conticini.cfg.MAX_EXPECTED);
Conticini.OPERANDO_SX.innerHTML = "" + op_sx;
......@@ -28,10 +30,10 @@ Conticini.check_msg = function() {
Conticini.PULSANTE.onclick = Conticini.check_result;
};
Conticini.ko_msg = function(timeout) {
Conticini.ko_msg = function(timeout, canvas = null) {
Conticini.PULSANTE.value = Conticini.cfg.MSG_KO;
Conticini.PULSANTE.className = 'ko';
window.setTimeout(Conticini.reset_canvases, timeout);
window.setTimeout(() => Conticini.reset_canvases(canvas), timeout);
};
Conticini.ok_msg = function(timeout) {
......@@ -43,14 +45,22 @@ Conticini.ok_msg = function(timeout) {
Conticini.check_result = async function() {
const u = await Conticini.nn.getNumber(Conticini.CIFRA_U, Conticini.CANVAS_TMP);
if (u === -1) {
Conticini.ko_msg(Conticini.cfg.KO_TIMEOUT);
return;
}
const d = await Conticini.nn.getNumber(Conticini.CIFRA_D, Conticini.CANVAS_TMP);
if ((d === -1 && u === Conticini.expected) || (d > -1 && 10*d + u === Conticini.expected)) {
if ((d === Conticini.expected_d
|| (Conticini.CIFRA_D.is_blank && Conticini.expected_d === 0))
&& u === Conticini.expected_u) {
Conticini.ok_msg(Conticini.cfg.OK_TIMEOUT);
return;
}
if (d === Conticini.expected_d && u !== Conticini.expected_u) {
Conticini.ko_msg(Conticini.cfg.KO_TIMEOUT, Conticini.CIFRA_U);
return;
}
if (d !== Conticini.expected_d && u === Conticini.expected_u) {
Conticini.ko_msg(Conticini.cfg.KO_TIMEOUT, Conticini.CIFRA_D);
return;
}
if ((d === -1 && u === Conticini.expected_u) || (d > -1 && d === Conticini.expected_d)) {
}
Conticini.ko_msg(Conticini.cfg.KO_TIMEOUT);
};
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment