forked from tympanix/pattern-lock-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patternlock.js
276 lines (248 loc) · 8.55 KB
/
patternlock.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
(function (factory) {
let global = Function('return this')() || (0, eval)('this');
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], function($) {
return factory($, global)
});
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require('jquery'), global);
} else {
// Browser globals (global is window)
global.PatternLock = factory(global.jQuery, global);
}
}(function ($, window) {
let svgns = 'http://www.w3.org/2000/svg',
moveEvent = 'touchmove mousemove',
scrollKeys = {
37: true, // left
38: true, // up
39: true, // right
40: true, // down
32: true, // spacebar
38: true, // pageup
34: true, // pagedown
35: true, // end
36: true, // home
};
function vibrate() {
navigator.vibrate = navigator.vibrate || navigator.webkitVibrate || navigator.mozVibrate || navigator.msVibrate;
if (navigator.vibrate) {
window.navigator.vibrate(25)
}
}
function PatternLock(element, options) {
let svg = $(element),
self = this,
root = svg[0],
dots = svg.find('.lock-dots circle'),
lines = svg.find('.lock-lines'),
actives = svg.find('.lock-actives'),
pt = root.createSVGPoint(),
code = [],
currentline,
currenthandler,
i,
dotsMap = []
for(i=0; i<dots.length; i++){
dotsMap[i+1] = {x: dots[i].cx.baseVal.value, y: dots[i].cy.baseVal.value};
}
options = Object.assign(PatternLock.defaults, options || {})
svg.on('touchstart mousedown', (e) => {
clear()
e.preventDefault()
disableScroll()
svg.on(moveEvent, discoverDot)
let endEvent = e.type == 'touchstart' ? 'touchend' : 'mouseup';
$(document).one(endEvent, (e) => {
end()
})
})
// Exported methods
Object.assign(this, {
clear,
success,
error,
getPattern,
setPattern,
})
function success() {
svg.removeClass('error')
svg.addClass('success')
}
function error() {
svg.removeClass('success')
svg.addClass('error')
}
function getPattern() {
return parseInt(code.map((i) => dots.index(i)+1).join(''))
}
function setPattern(arr){
clear()
if (arr === undefined) return
let marker,line, dotActual, dotBefore = null
for(i = 0; i < arr.length ; i++ ){
if(dotsMap[arr[i]] !== undefined){
dotActual = dotsMap[arr[i]]
marker = createNewMarker(dotActual.x,dotActual.y)
actives.append(marker)
if(dotBefore != null){
line = createNewLine(dotBefore.x, dotBefore.y, dotActual.x, dotActual.y)
lines.append(line)
}
if (options.vibrate) vibrate()
dotBefore = dotActual
}
}
}
function end() {
enableScroll()
stopTrack(currentline)
currentline && currentline.remove()
svg.off(moveEvent, discoverDot)
let val = options.onPattern.call(self, getPattern())
if (val === true) {
success()
} else if (val === false) {
error()
}
}
function clear() {
code = []
currentline = undefined
currenthandler = undefined
svg.removeClass('success error')
lines.empty()
actives.empty()
}
function preventDefault(e) {
e = e || window.event;
if (e.preventDefault)
e.preventDefault();
e.returnValue = false;
}
function preventDefaultForScrollKeys(e) {
if (scrollKeys[e.keyCode]) {
preventDefault(e);
return false;
}
}
function disableScroll() {
if (window.addEventListener) // older FF
window.addEventListener('DOMMouseScroll', preventDefault, false);
window.onwheel = preventDefault; // modern standard
window.onmousewheel = document.onmousewheel = preventDefault; // older browsers, IE
window.ontouchmove = preventDefault; // mobile
document.onkeydown = preventDefaultForScrollKeys;
}
function enableScroll() {
if (window.removeEventListener)
window.removeEventListener('DOMMouseScroll', preventDefault, false);
window.onmousewheel = document.onmousewheel = null;
window.onwheel = null;
window.ontouchmove = null;
document.onkeydown = null;
}
function isUsed(target) {
for (i = 0; i < code.length; i++) {
if (code[i] === target) {
return true
}
}
return false
}
function isAvailable(target) {
for (i = 0; i < dots.length; i++) {
if (dots[i] === target) {
return true
}
}
return false
}
function updateLine(line) {
return function(e) {
e.preventDefault()
if (currentline !== line) return
let pos = svgPosition(e.target, e)
line.setAttribute('x2', pos.x)
line.setAttribute('y2', pos.y)
return false
}
}
function discoverDot(e, target) {
if (!target) {
let {x, y} = getMousePos(e)
target = document.elementFromPoint(x, y);
}
let cx = target.getAttribute('cx'),
cy = target.getAttribute('cy')
if (isAvailable(target) && !isUsed(target)) {
stopTrack(currentline, target)
currentline = beginTrack(target)
}
}
function stopTrack(line, target) {
if (line === undefined) return
if (currenthandler) {
svg.off('touchmove mousemove', currenthandler)
}
if (target === undefined) return
let x = target.getAttribute('cx'),
y = target.getAttribute('cy')
line.setAttribute('x2', x)
line.setAttribute('y2', y)
}
function beginTrack(target) {
code.push(target)
let x = target.getAttribute('cx'),
y = target.getAttribute('cy'),
line = createNewLine(x, y),
marker = createNewMarker(x, y)
actives.append(marker)
currenthandler = updateLine(line)
svg.on('touchmove mousemove', currenthandler)
lines.append(line);
if(options.vibrate) vibrate()
return line
}
function createNewMarker(x, y) {
let marker = document.createElementNS(svgns, "circle")
marker.setAttribute('cx', x)
marker.setAttribute('cy', y)
marker.setAttribute('r', 6)
return marker
}
function createNewLine(x1, y1, x2, y2) {
let line = document.createElementNS(svgns, "line")
line.setAttribute('x1', x1)
line.setAttribute('y1', y1)
if (x2 === undefined || y2 == undefined) {
line.setAttribute('x2', x1)
line.setAttribute('y2', y1)
} else {
line.setAttribute('x2', x2)
line.setAttribute('y2', y2)
}
return line
}
function getMousePos(e) {
return {
x: e.clientX || e.originalEvent.touches[0].clientX,
y :e.clientY || e.originalEvent.touches[0].clientY
}
}
function svgPosition(element, e) {
let {x, y} = getMousePos(e)
pt.x = x; pt.y = y;
return pt.matrixTransform(element.getScreenCTM().inverse());
}
}
PatternLock.defaults = {
onPattern: () => {},
vibrate: true,
}
return PatternLock
}));