-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpattern-input.js
More file actions
239 lines (222 loc) · 10.7 KB
/
Copy pathpattern-input.js
File metadata and controls
239 lines (222 loc) · 10.7 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
(function ($) {
function hitTest(state, x, y) {
var columnHit = -1;
var rowHit = -1;
if (((x - state.border) % state.dotHSpace) <= state.dotSize) {
columnHit = Math.floor((x - state.border) / state.dotHSpace);
if (columnHit > state.horizontalDots - 1) {
columnHit = -1;
}
}
if (((y - state.border) % state.dotVSpace) <= state.dotSize) {
rowHit = Math.floor((y - state.border) / state.dotVSpace);
if (rowHit > state.verticalDots - 1) {
rowHit = -1;
}
}
if (columnHit > -1 && rowHit > -1) {
return ((rowHit * state.horizontalDots) + columnHit);
} else {
return -1;
}
}
function getPosition(state, index) {
var row = Math.floor(index / state.horizontalDots);
var col = index - (row * state.horizontalDots);
var x = (col * state.dotHSpace) + state.border + (state.dotSize / 2);
var y = (row * state.dotVSpace) + state.border + (state.dotSize / 2);
return [x, y];
}
function clearSelection(state) {
var canvas = state.canvas[0];
canvas.width = canvas.width;
var ctx = canvas.getContext("2d");
ctx.strokeStyle = state.pathColor;
ctx.lineWidth = state.pathSize;
for (var i = 0; i < state.dots.length; i++) {
state.dots[i].removeClass("selected");
}
}
function renderPath(state) {
if (state.value && state.value.length > 1) {
var ctx = state.canvas[0].getContext("2d");
for (var i = 0; i < (state.value.length - 1); i++) {
var pos1 = getPosition(state, state.value[i]);
var pos2 = getPosition(state, state.value[i + 1]);
ctx.moveTo(pos1[0], pos1[1]);
ctx.lineTo(pos2[0], pos2[1]);
ctx.stroke();
}
}
}
function processTouchStart(state, x, y) {
state.value = [];
clearSelection(state);
var hitAt = hitTest(state, x, y);
if (hitAt > -1) {
state.value.push(hitAt);
state.lastHit = hitAt;
state.dots[hitAt].addClass("selected");
state.onChange(state.value);
state.logger("onChange: SEQ=" + state.value.join(","));
}
}
function processTouchMove(state, x, y) {
var hitAt = hitTest(state, x, y);
if (hitAt > -1) {
if (state.lastHit != hitAt) {
state.value.push(hitAt);
state.lastHit = hitAt;
state.dots[hitAt].addClass("selected");
state.logger("onChange: SEQ=" + state.value.join(","));
renderPath(state);
setTimeout(function() {
state.onChange(state.value);
},10);
}
}
}
function processTouchEnd(state) {
if (state.value && state.value.length > 0) {
state.lastSequence = state.value;
setTimeout(function() {
state.onFinish(state.value);
},10);
if (state.autoClear) {
clearSelection(state);
}
state.logger("onFinish: SEQ=" + state.value.join(","));
}
}
var methods = {
init: function (options) {
var newOptions = $.extend({
width: 300,
height: 300,
border: 20,
verticalDots: 3,
horizontalDots: 3,
dotSize: 60,
innerDotSize: 20,
pathSize: 20,
autoClear: true,
pathColor: "#AAAAAA",
onChange: function () { },
onFinish: function () { },
logger: function () { }
}, options);
return this.each(function () {
var $this = $(this);
if (this.tagName == "DIV") {
//Fix CSS stuffs to correct the display and dragging.
$this.attr('unselectable', 'on').css('user-select', 'none');
if ($this.css("position") == "static") {
$this.css("position", "relative");
}
var state = $this.data("patternInput");
if (!state) {
//Save all options as the state of this plug-in to each DOM element
state = newOptions;
$this.data("patternInput", state);
$this.empty();
$this.addClass("patternInput");
$this.width(state.width)
.height(state.height);
//Create a new canvas for Path drawing
state.canvas = $('<canvas width="' + state.width + '" height="' + state.height + '" style="position:absolute;left:0px;top:0px;"></canvas>');
$this.append(state.canvas);
var ctx = state.canvas[0].getContext("2d");
ctx.strokeStyle = state.pathColor;
ctx.lineWidth = state.pathSize;
//Create new DIVs for each dot and also hook the event
state.dots = [];
var divStart = state.border;
var runnerX = divStart;
var runnerY = divStart;
state.dotHSpace = ((state.width - state.dotSize) - (state.border * 2)) / (state.horizontalDots - 1);
state.dotVSpace = ((state.height - state.dotSize) - (state.border * 2)) / (state.verticalDots - 1);
for (var j = 0; j < state.verticalDots; j++) {
for (var i = 0; i < state.horizontalDots; i++) {
var newDiv = $('<div class="dot" style="width:' + state.dotSize + 'px;' +
'height:' + state.dotSize + 'px;' +
'position: absolute; ' +
'left:' + runnerX + 'px; ' +
'top:' + runnerY + 'px;">' +
'<div class="innerDot" style="position:absolute;' +
'width:' + state.innerDotSize + 'px;' +
'height:' + state.innerDotSize + 'px;' +
'left:' + (state.dotSize - state.innerDotSize) / 2 + 'px; ' +
'top:' + (state.dotSize - state.innerDotSize) / 2 + 'px;">' +
'</div>' +
'</div>');
$this.append(newDiv);
state.dots.push(newDiv);
runnerX += state.dotHSpace;
}
runnerX = divStart;
runnerY += state.dotVSpace;
}
$this.on("mousedown", function (event) {
var relativeX = event.pageX - this.offsetLeft;
var relativeY = event.pageY - this.offsetTop;
state.mouseDown = true;
processTouchStart(state, relativeX, relativeY);
});
$this.on("mousemove", function (event) {
if (state.mouseDown == true) {
var relativeX = event.pageX - this.offsetLeft;
var relativeY = event.pageY - this.offsetTop;
processTouchMove(state, relativeX, relativeY);
}
});
$this.on("mouseup", function (event) {
if (state.mouseDown == true) {
state.mouseDown = false;
processTouchEnd(state);
}
});
$(document).on("mouseup", function (event) {
if (state.mouseDown == true) {
state.mouseDown = false;
processTouchEnd(state);
}
});
$this.on("touchstart", function (event) {
var relativeX = event.originalEvent.targetTouches[0].pageX - this.offsetLeft;
var relativeY = event.originalEvent.targetTouches[0].pageY - this.offsetTop;
processTouchStart(state, relativeX, relativeY);
state.touchDown = true;
});
$this.on("touchmove", function (event) {
var relativeX = event.originalEvent.targetTouches[0].pageX - this.offsetLeft;
var relativeY = event.originalEvent.targetTouches[0].pageY - this.offsetTop;
processTouchMove(state, relativeX, relativeY);
});
$this.on("touchend", function (event) {
processTouchEnd(state);
});
}
}
});
},
clear: function() {
var state = this.data("patternInput");
clearSelection(state);
return this;
},
getLastSequence: function() {
var state = this.data("patternInput");
return state.lastSequence;
}
}
$.fn.patternInput = function (arg) {
if (methods[arg]) {
return methods[arg].apply(this, Array.prototype.slice.call(arguments, 1));
} else
if (typeof arg === 'object' || !arg) {
return methods.init.apply(this, arguments);
} else {
$.error("Invalid method call. Method '" + arg + "' does not exist on jQuery.patternInput");
}
};
})(jQuery);