1 | "use strict";
|
2 |
|
3 |
|
4 | document.ontouchmove = function(e) {
|
5 | e.preventDefault();
|
6 | };
|
7 |
|
8 |
|
9 | window.oncontextmenu = function() {
|
10 | return false;
|
11 | };
|
12 |
|
13 | var relMouseCoords = function(canvas, event) {
|
14 | var x = event.pageX - canvas.offsetLeft + document.body.scrollLeft;
|
15 | var y = event.pageY - canvas.offsetTop + document.body.scrollTop;
|
16 |
|
17 |
|
18 | if (canvas.style.width.length) {
|
19 | x *= canvas.width / canvas.style.width.substring(0, canvas.style.width.indexOf("p"));
|
20 | }
|
21 | if (canvas.style.height.length) {
|
22 | y *= canvas.height / canvas.style.height.substring(0, canvas.style.height.indexOf("p"));
|
23 | }
|
24 |
|
25 | return {x:x, y:y};
|
26 | };
|
27 |
|
28 | function relMouseCoordsEjecta() {
|
29 | var event = arguments[1];
|
30 | var x = event.pageX * window.devicePixelRatio;
|
31 | var y = event.pageY * window.devicePixelRatio;
|
32 | return {x:x, y:y};
|
33 | }
|
34 |
|
35 | if (window.ejecta) {
|
36 | relMouseCoords = relMouseCoordsEjecta;
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | function Mouse(canvas) {
|
53 | |
54 |
|
55 |
|
56 |
|
57 | this.x = 0;
|
58 | |
59 |
|
60 |
|
61 |
|
62 | this.y = 0;
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 | this.buttons = [0, 0, 0];
|
69 |
|
70 | |
71 |
|
72 |
|
73 |
|
74 | this.touches = [];
|
75 |
|
76 | var self = this;
|
77 | canvas.addEventListener("mousedown", function(event) {
|
78 | var m = relMouseCoords(canvas, event);
|
79 | self.x = m.x;
|
80 | self.y = m.y;
|
81 | self.buttons[event.button] = 2;
|
82 | updateTouchFromMouse();
|
83 | });
|
84 | canvas.addEventListener("mouseup", function(event) {
|
85 | var m = relMouseCoords(canvas, event);
|
86 | self.x = m.x;
|
87 | self.y = m.y;
|
88 | self.buttons[event.button] = 0;
|
89 | updateTouchFromMouse();
|
90 | });
|
91 | canvas.addEventListener("mousemove", function(event) {
|
92 | var m = relMouseCoords(canvas, event);
|
93 | self.x = m.x;
|
94 | self.y = m.y;
|
95 | updateTouchFromMouse();
|
96 | });
|
97 |
|
98 | function updateTouchFromMouse() {
|
99 | if (self.supportsTouch()) {
|
100 | return;
|
101 | }
|
102 | var idx = touchIndexById("mouse");
|
103 | if (self.isPressed(0)) {
|
104 | if (idx !== undefined) {
|
105 | var touch = self.touches[idx];
|
106 | touch.x = self.x;
|
107 | touch.y = self.y;
|
108 | } else {
|
109 | self.touches.push({
|
110 | id: "mouse",
|
111 | x: self.x,
|
112 | y: self.y
|
113 | });
|
114 | }
|
115 | } else if (idx !== undefined) {
|
116 | self.touches.splice(idx, 1);
|
117 | }
|
118 | }
|
119 | function updateMouseFromTouch(touch) {
|
120 | self.x = touch.x;
|
121 | self.y = touch.y;
|
122 | if (self.buttons[0] === 0) {
|
123 | self.buttons[0] = 2;
|
124 | }
|
125 | }
|
126 | function touchIndexById(id) {
|
127 | for (var i = 0; i < self.touches.length; i++) {
|
128 | if (self.touches[i].id === id) {
|
129 | return i;
|
130 | }
|
131 | }
|
132 | return undefined;
|
133 | }
|
134 | function eachChangedTouch(event, onChangeFunc) {
|
135 | var touches = event.changedTouches;
|
136 | for (var i = 0; i < touches.length; i++) {
|
137 | onChangeFunc(touches[i]);
|
138 | }
|
139 | }
|
140 | canvas.addEventListener("touchstart", function(event) {
|
141 | eachChangedTouch(event, function(touch) {
|
142 | var t = relMouseCoords(canvas, touch);
|
143 | t.id = touch.identifier;
|
144 | if (self.touches.length === 0) {
|
145 | t.isMouse = true;
|
146 | updateMouseFromTouch(t);
|
147 | }
|
148 | self.touches.push(t);
|
149 | });
|
150 | });
|
151 | canvas.addEventListener("touchmove", function(event) {
|
152 | eachChangedTouch(event, function(touch) {
|
153 | var idx = touchIndexById(touch.identifier);
|
154 | var t = self.touches[idx];
|
155 | var coords = relMouseCoords(canvas, touch);
|
156 | t.x = coords.x;
|
157 | t.y = coords.y;
|
158 | if (t.isMouse) {
|
159 | updateMouseFromTouch(t);
|
160 | }
|
161 | });
|
162 | });
|
163 | canvas.addEventListener("touchend", function(event) {
|
164 | eachChangedTouch(event, function(touch) {
|
165 | var idx = touchIndexById(touch.identifier);
|
166 | var t = self.touches.splice(idx, 1)[0];
|
167 | if (t.isMouse) {
|
168 | if (self.touches.length === 0) {
|
169 | self.buttons[0] = 0;
|
170 | } else {
|
171 | self.touches[0].isMouse = true;
|
172 | updateMouseFromTouch(self.touches[0]);
|
173 | }
|
174 | }
|
175 | });
|
176 | });
|
177 | }
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | Mouse.prototype.supportsTouch = function() {
|
183 | return "ontouchstart" in window || navigator.msMaxTouchPoints;
|
184 | };
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | Mouse.prototype.isPressed = function(button) {
|
191 | return this.buttons[button] >= 1;
|
192 | };
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | Mouse.prototype.consumePressed = function(button, x, y, width, height) {
|
204 | var b = this.buttons[button] === 2;
|
205 | if (arguments.length > 1 && (this.x < x || this.x > x + width || this.y < y || this.y > y + height)) {
|
206 | b = false;
|
207 | }
|
208 | if (b) {
|
209 | this.buttons[button] = 1;
|
210 | if (button === 0) {
|
211 | for (var i = 0; i < this.touches.length; i++) {
|
212 | this.touches[i].consumed = true;
|
213 | }
|
214 | }
|
215 | }
|
216 | return b;
|
217 | };
|
218 |
|
219 | module.exports = Mouse;
|