forked from neacsugeorge/simple-signature-analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
472 lines (393 loc) · 12 KB
/
index.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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.strokeStyle = "#222222";
ctx.lineWith = 2;
// Set up mouse events for drawing
var drawing = false;
var validate = false;
var validateDirections = false;
var mousePos = {
x: 0,
y: 0
};
var lastPos = mousePos;
canvas.addEventListener("mousedown", function (e) {
drawing = true;
lastPos = getMousePos(canvas, e);
}, false);
canvas.addEventListener("mouseup", function (e) {
drawing = false;
}, false);
canvas.addEventListener("mousemove", function (e) {
mousePos = getMousePos(canvas, e);
}, false);
// Get the position of the mouse relative to the canvas
function getMousePos(canvasDom, mouseEvent) {
var rect = canvasDom.getBoundingClientRect();
return {
x: mouseEvent.clientX - rect.left,
y: mouseEvent.clientY - rect.top
};
}
// Get a regular interval for drawing to the screen
window.requestAnimFrame = (function (callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimaitonFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
function getDirection(lastPos, mousePos) {
var direction = 'C';
if (!lastPos) {
lastPos = window.lastPos;
}
if (!mousePos) {
mousePos = window.mousePos;
}
var deltaX = mousePos.x - lastPos.x;
var deltaY = mousePos.y - lastPos.y;
var angle = Math.atan2(deltaY, deltaX) * 180 / Math.PI;
if (-22.5 < angle && angle < 22.5) {
direction = 'E';
}
else if (22.5 < angle && angle < 67.5) {
direction = 'SE';
}
else if (67.5 < angle && angle < 112.5) {
direction = 'S';
}
else if (112.5 < angle && angle < 157.5) {
direction = 'SW';
}
else if (157.5 < angle && angle < 180 || angle > -180 && angle < -157.5) {
direction = 'W';
}
else if (-157.5 < angle && angle < -112.5) {
direction = 'NW';
}
else if (-112.5 < angle && angle < -67.5) {
direction = 'N';
}
else if (-67.5 < angle && angle < -22.5) {
direction = 'NE';
}
else {
direction = 'C';
}
return direction;
}
function levenshtein(str1, str2) {
var n = str2.length;
var v0 = new Array(n + 1).fill(0);
var v1 = new Array(n + 1).fill(0);
for (var i = 0; i < v0.length; i++) {
v0[i] = i;
}
for (var i = 0; i < str1.length; i++) {
v1[0] = i + 1;
for (var j = 0; j < str2.length; j++) {
var cost = 1;
if (str1[i] == str2[j]) {
cost = 0;
}
v1[j + 1] = Math.min(
v1[j] + 1, //Insertion
v0[j + 1] + 1, //Remove
v0[j] + cost //Substitution
);
}
var temp = v0;
v0 = v1;
v1 = temp;
}
return v0[n];
}
var signatures = [];
var directionsMap = [];
var idx = 0;
// Draw to the canvas
var lastSec = Date.now();
var lastSecPos = lastPos;
var lastDirection = 'C';
function getDistance(a, b, precision) {
if (!precision) {
precision = 0;
}
return Math.floor(Math.sqrt(Math.pow(mousePos.x - lastPos.x, 2) + Math.pow(mousePos.y - lastPos.y, 2)) * Math.pow(10, precision)) / Math.pow(10, precision);
}
function getDistances(a, b) {
return [(a.x - b.x), (a.y - b.y)];
}
function renderCanvas() {
var precision = 0;
var distance = getDistance(lastPos, mousePos);
if (!signatures[idx]) {
signatures[idx] = [];
}
if (!directionsMap[idx]) {
directionsMap[idx] = [];
}
if (drawing) {
ctx.moveTo(lastPos.x, lastPos.y);
ctx.lineTo(mousePos.x, mousePos.y);
ctx.stroke();
if (Date.now() - lastSec >= 50) {
var direction = getDirection(lastSecPos, mousePos);
if (validateDirections.constructor === Array) {
validateDirections.push(direction);
}
else {
directionsMap[idx].push(direction);
}
lastSecPos = mousePos;
lastSec = Date.now();
}
var distances = getDistances(lastPos, mousePos);
if (lastPos.x !== mousePos.x || lastPos.y !== mousePos.y) {
if (validate.constructor === Array) {
validate.push(getDistances(lastPos, mousePos));
}
else {
signatures[idx].push(getDistances(lastPos, mousePos));
}
}
lastPos = mousePos;
}
else {
var len = 0;
if (validate.constructor === Array) {
len = validate.length;
validate[len - 1] = getDistances(lastPos, mousePos);
}
else {
len = signatures[idx].length;
signatures[idx][len - 1] = getDistances(lastPos, mousePos);
}
}
}
function minWithUndefined() {
for (var i = 0; i < arguments.length; ++i) {
if (arguments[i] === undefined) {
arguments[i] = Infinity;
}
}
return Math.min.apply(this, arguments);
}
function maxWithUndefined() {
for (var i = 0; i < arguments.length; ++i) {
if (arguments[i] === undefined) {
arguments[i] = -Infinity;
}
}
return Math.max.apply(this, arguments);
}
function getMinMaxPath(signatures) {
var signature = [];
if (!signatures) {
signatures = window.signatures;
}
var maxLength = 0;
signatures.forEach(one => maxLength = maxLength < one.length ? one.length : maxLength);
for (var i = 0; i < maxLength; ++i) {
var xs = [], ys = [];
signatures.forEach(one => {
if (one[i]) {
xs.push(one[i][0]);
ys.push(one[i][1]);
}
});
signature[i] = [
minWithUndefined.apply(this, xs),
maxWithUndefined.apply(this, xs),
minWithUndefined.apply(this, ys),
maxWithUndefined.apply(this, ys)
];
}
return signature;
}
function lcs(intervals, signature) {
var n1 = intervals.length;
var n2 = signature.length;
var LCS = new Array(n1+1);
for (var i = 0; i < n1+1; i++) {
LCS[i] = new Array(n2+1).fill(0);
}
for (var i = 0; i <= n1; i++) {
for (var j = 0; j <= n2; j++) {
if (!i || !j) {
LCS[i][j] = 0;
}
else if (intervals[i - 1][0] < signature[j - 1][0]
&& signature [j - 1][0] < intervals[i - 1][1]
&& intervals[i - 1][2] < signature[j - 1][1]
&& signature[j - 1][1] < intervals[i - 1][3]) {
LCS[i][j] = LCS[i - 1][j - 1] + 1;
}
else {
LCS[i][j] = Math.max(LCS[i - 1][j], LCS[i][j - 1]);
}
}
}
return LCS[n1][n2];
}
function lcs_string(str1, str2) {
var n1 = str1.length;
var n2 = str2.length;
var LCS = new Array(n1+1);
for (var i = 0; i < n1+1; i++) {
LCS[i] = new Array(n2+1).fill(0);
}
for (var i = 0; i <= n1; i++) {
for (var j = 0; j <= n2; j++) {
if (!i || !j) {
LCS[i][j] = 0;
}
else if (str1[i - 1] == str2[j - 1]) {
LCS[i][j] = LCS[i - 1][j - 1] + 1;
}
else {
LCS[i][j] = Math.max(LCS[i - 1][j], LCS[i][j - 1]);
}
}
}
return LCS[n1][n2];
}
function checkValidate() {
if (validate.constructor === Array) {
var signatureIntervals = getMinMaxPath();
var signature = validate;
var LCS = lcs(signatureIntervals, signature);
var min = Math.min(LCS, signature.length);
var directions = validateDirections.join('');
var minMatch = Infinity;
directionsMap.forEach(one => {
var str = one.join('');
var len = str.length;
var LCS = lcs_string(directions, str);
var percent = LCS / Math.min(len, directions.length);
minMatch = minMatch > percent ? percent : minMatch;
});
console.log({
lcs: LCS,
lcs_interval: LCS / Math.max(signatureIntervals.length, signature.length),
lcs_signature: LCS / signature.length,
min_direction: minMatch
});
}
validate = [];
validateDirections = [];
canvas.width = canvas.width;
}
function findUser() {
if (validate.constructor === Array) {
var found = false;
for (var user_id in db.signatures) {
var interval = getMinMaxPath(db.signatures[user_id]);
var LCS = lcs(interval, validate);
var min = Math.min(LCS, validate.length);
var l1 = LCS / Math.max(interval.length, validate.length);
var l2 = LCS / validate.length;
var directions = validateDirections.join('');
var l3 = Infinity;
db.directionsMap[user_id].forEach(one => {
var str = one.join('');
var len = str.length;
var LCS = lcs_string(directions, str);
var percent = LCS / Math.min(len, directions.length);
l3 = l3 > percent ? percent : l3;
});
console.log(l1, l2, l3);
if ((l1 + l2 + l3) / 3 >= 0.75) {
var data = db.data[user_id];
alert(JSON.stringify(data));
found = true;
break;
}
}
if (!found) {
alert('Not found');
}
}
}
function composeSignature(str) {
}
// Allow for animation
(function drawLoop() {
requestAnimFrame(drawLoop);
renderCanvas();
})();
// Set up touch events for mobile, etc
canvas.addEventListener("touchstart", function (e) {
mousePos = getTouchPos(canvas, e);
var touch = e.touches[0];
var mouseEvent = new MouseEvent("mousedown", {
clientX: touch.clientX,
clientY: touch.clientY
});
canvas.dispatchEvent(mouseEvent);
}, false);
canvas.addEventListener("touchend", function (e) {
var mouseEvent = new MouseEvent("mouseup", {});
canvas.dispatchEvent(mouseEvent);
}, false);
canvas.addEventListener("touchmove", function (e) {
var touch = e.touches[0];
var mouseEvent = new MouseEvent("mousemove", {
clientX: touch.clientX,
clientY: touch.clientY
});
canvas.dispatchEvent(mouseEvent);
}, false);
// Get the position of a touch relative to the canvas
function getTouchPos(canvasDom, touchEvent) {
var rect = canvasDom.getBoundingClientRect();
return {
x: touchEvent.touches[0].clientX - rect.left,
y: touchEvent.touches[0].clientY - rect.top
};
}
function clearCanvas() {
canvas.width = canvas.width;
if (validate.constructor === Array) {
validate = [];
validateDirections = [];
}
else {
signatures[idx] = [];
directionsMap[idx] = [];
}
}
function newCanvas() {
canvas.width = canvas.width;
idx++;
signatures[idx] = [];
directionsMap[idx] = [];
validate = false;
validateDirections = false;
}
function setCanvasDimensions() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx = canvas.getContext('2d');
}
function goFullscreen() {
var el = document.documentElement,
rfs = el.requestFullScreen
|| el.webkitRequestFullScreen
|| el.mozRequestFullScreen
|| el.msRequestFullscreen;
rfs.call(el);
}
$(function() {
setCanvasDimensions();
$(window).on('resize', setCanvasDimensions);
var canvas = $('#canvas');
canvas.on('touchstart', (e) => e.preventDefault());
canvas.on('touchend', (e) => e.preventDefault());
canvas.on('touchmove', (e) => e.preventDefault());
$('#clear').click(clearCanvas);
$('#fullscreen').click(goFullscreen);
$('#add').click(newCanvas);
$('#validate').click(checkValidate);
$('#search').click(findUser);
goFullscreen();
});