forked from kishanrajput23/Java-Projects-Collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
64 lines (61 loc) · 1.61 KB
/
utils.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
var log = console.log.bind(console);
var random = function (start, end) {
start = start === void 0 ? 0 : start;
end = end === void 0 ? 1 : end;
end = end + 1;
var rand = Math.random() * (end - start) + start;
return Math.floor(rand);
};
var $ = function (elem) {
return document.querySelectorAll(elem);
}
var on = function (elem, type, callback) {
elem.addEventListener(type, function (e) {
callback(e);
});
}
var indexToPos = function (index) {
return {
x: index % 4,
y: Math.floor(index / 4),
}
}
var getLocalStorage = function (key) {
return localStorage[key] ?
JSON.parse(localStorage[key]) : null;
}
var touchMoveDir = function (elem, min, callback) {
var touchPos = {
beforeX: 0,
beforeY: 0,
afterX: 0,
afterY: 0,
}
var move = false;
var dir;
on(elem, 'touchstart', function (e) {
touchPos.beforeX = e.touches[0].clientX;
touchPos.beforeY = e.touches[0].clientY;
});
on(elem, 'touchmove', function (e) {
move = true;
touchPos.afterX = e.touches[0].clientX;
touchPos.afterY = e.touches[0].clientY;
});
on(elem, 'touchend', function (e) {
if (!move) return;
var x = touchPos.beforeX - touchPos.afterX;
var y = touchPos.beforeY - touchPos.afterY;
log(x, y);
if (Math.abs(x) < min && Math.abs(y) < min) {
return;
}
if (Math.abs(x) > Math.abs(y)) {
dir = x > 0 ? 0 : 2;
} else {
dir = y > 0 ? 1 : 3;
}
move = false;
callback(dir);
});
};