forked from piqnt/stage.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcut-mouse.js
More file actions
205 lines (178 loc) · 4.93 KB
/
cut-mouse.js
File metadata and controls
205 lines (178 loc) · 4.93 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
/*
* CutJS
* Copyright (c) 2013-2014 Ali Shakiba, Piqnt LLC and other contributors
* Available under the MIT license
* @license
*/
DEBUG = true || (typeof DEBUG === 'undefined' || DEBUG) && console;
Cut.prototype.spy = function(spy) {
if (!arguments.length) {
return this._spy;
}
this._spy = spy ? true : false;
return this;
};
Cut.Mouse = function() {
Cut.Mouse.subscribe.apply(Cut.Mouse, arguments);
};
Cut.Mouse.CLICK = "click";
Cut.Mouse.START = "touchstart mousedown";
Cut.Mouse.MOVE = "touchmove mousemove";
Cut.Mouse.END = "touchend mouseup";
Cut.Mouse.subscribe = function(listener, elem, move) {
elem = elem || document;
var isTouchSupported = "ontouchstart" in window;
var CLICK = "click";
var START = isTouchSupported ? "touchstart" : "mousedown";
var MOVE = isTouchSupported ? "touchmove" : "mousemove";
var END = isTouchSupported ? "touchend" : "mouseup";
elem.addEventListener(CLICK, mouseClick);
elem.addEventListener(START, mouseStart);
elem.addEventListener(END, mouseEnd);
move && elem.addEventListener(MOVE, mouseMove);
var visitor = null;
var abs = {
x : 0,
y : 0,
toString : function() {
return (this.x | 0) + "x" + (this.y | 0);
}
};
var rel = {
x : 0,
y : 0,
toString : function() {
return abs + " / " + (this.x | 0) + "x" + (this.y | 0);
}
};
var clicked = {
x : 0,
y : 0,
state : 0
};
function mouseStart(event) {
update(event, elem);
DEBUG && console.log("Mouse Start: " + event.type + " " + abs);
!move && elem.addEventListener(MOVE, mouseMove);
event.preventDefault();
publish(event.type, event);
clicked.x = abs.x;
clicked.y = abs.y;
clicked.state = 1;
}
function mouseEnd(event) {
try {
// New xy is not valid/available, last xy is used instead.
DEBUG && console.log("Mouse End: " + event.type + " " + abs);
!move && elem.removeEventListener(MOVE, mouseMove);
event.preventDefault();
publish(event.type, event);
if (clicked.state == 1 && clicked.x == abs.x && clicked.y == abs.y) {
DEBUG && console.log("Mouse Click [+]");
publish("click", event);
clicked.state = 2;
} else {
clicked.state = 0;
}
} catch (e) {
console && console.log(e);
}
}
function mouseMove(event) {
try {
update(event, elem);
// DEBUG && console.log("Mouse Move: " + event.type + " " +
// abs);
event.preventDefault();
publish(event.type, event);
} catch (e) {
console && console.log(e);
}
}
function mouseClick(event) {
try {
update(event, elem);
DEBUG && console.log("Mouse Click: " + event.type + " " + abs);
event.preventDefault();
if (clicked.state != 2) {
publish(event.type, event);
} else {
DEBUG && console.log("Mouse Click [-]");
}
} catch (e) {
console && console.log(e);
}
}
function publish(type, event) {
abs.type = type;
abs.event = event;
rel.x = abs.x;
rel.y = abs.y;
listener.visit(visitor);
}
visitor = {
reverse : true,
visible : true,
start : function(cut) {
if (!(cut.spy() || listener === cut)) {
cut.matrix().reverse().map(abs, rel);
if (rel.x < 0 || rel.x > cut._pin._width || rel.y < 0
|| rel.y > cut._pin._height) {
return true;
}
}
},
end : function(cut) {
var listeners = cut.listeners(abs.type);
if (listeners) {
cut.matrix().reverse().map(abs, rel);
for (var l = 0; l < listeners.length; l++)
if (listeners[l].call(cut, abs.event, rel)) {
return true;
}
}
}
};
function update(event, elem) {
var isTouch = false;
// touch screen events
if (event.touches) {
if (event.touches.length) {
isTouch = true;
abs.x = event.touches[0].pageX;
abs.y = event.touches[0].pageY;
} else {
return;
}
} else {
// mouse events
abs.x = event.clientX;
abs.y = event.clientY;
// http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
if (document.body.scrollLeft || document.body.scrollTop) {
// body is added as offsetParent
} else if (document.documentElement) {
abs.x += document.documentElement.scrollLeft;
abs.y += document.documentElement.scrollTop;
}
}
// accounts for border
abs.x -= elem.clientLeft || 0;
abs.y -= elem.clientTop || 0;
var par = elem;
while (par) {
abs.x -= par.offsetLeft || 0;
abs.y -= par.offsetTop || 0;
if (!isTouch) {
// touch events offset scrolling with pageX/Y
// so scroll offset not needed for them
abs.x += par.scrollLeft || 0;
abs.y += par.scrollTop || 0;
}
par = par.offsetParent;
}
// see loader
abs.x *= listener._ratio || 1;
abs.y *= listener._ratio || 1;
}
};