forked from processing/p5.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
touch.js
300 lines (283 loc) · 7.45 KB
/
touch.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* @module Events
* @submodule Touch
* @for p5
* @requires core
*/
'use strict';
var p5 = require('../core/core');
/*
* This is a flag which is false until the first time
* we receive a touch event. The ptouchX and ptouchY
* values will match the touchX and touchY values until
* this interaction takes place.
*/
p5.prototype._hasTouchInteracted = false;
/**
* The system variable touchX always contains the horizontal position of
* one finger, relative to (0, 0) of the canvas. This is best used for
* single touch interactions. For multi-touch interactions, use the
* touches[] array.
*
* @property touchX
*/
p5.prototype.touchX = 0;
/**
* The system variable touchY always contains the vertical position of
* one finger, relative to (0, 0) of the canvas. This is best used for
* single touch interactions. For multi-touch interactions, use the
* touches[] array.
*
* @property touchY
*/
p5.prototype.touchY = 0;
/**
* The system variable ptouchX always contains the horizontal position of
* one finger, relative to (0, 0) of the canvas, in the frame previous to the
* current frame.
*
* @property ptouchX
*/
p5.prototype.ptouchX = 0;
/**
* The system variable ptouchY always contains the vertical position of
* one finger, relative to (0, 0) of the canvas, in the frame previous to the
* current frame.
*
* @property ptouchY
*/
p5.prototype.ptouchY = 0;
/**
* The system variable touches[] contains an array of the positions of all
* current touch points, relative to (0, 0) of the canvas, and IDs identifying a
* unique touch as it moves. Each element in the array is an object with x, y,
* and id properties.
*
* @property touches[]
*/
p5.prototype.touches = [];
/**
* The boolean system variable touchIsDown is true if the screen is
* touched and false if not.
*
* @property touchIsDown
*/
p5.prototype.touchIsDown = false;
p5.prototype._updateNextTouchCoords = function(e) {
var x = this.touchX;
var y = this.touchY;
if(e.type === 'mousedown' ||
e.type === 'mousemove' ||
e.type === 'mouseup' || !e.touches) {
x = this.mouseX;
y = this.mouseY;
} else {
if(this._curElement !== null) {
var touchInfo = getTouchInfo(this._curElement.elt, e, 0);
x = touchInfo.x;
y = touchInfo.y;
var touches = [];
for(var i = 0; i < e.touches.length; i++){
touches[i] = getTouchInfo(this._curElement.elt, e, i);
}
this._setProperty('touches', touches);
}
}
this._setProperty('touchX', x);
this._setProperty('touchY', y);
if (!this._hasTouchInteracted) {
// For first draw, make previous and next equal
this._updateTouchCoords();
this._setProperty('_hasTouchInteracted', true);
}
};
p5.prototype._updateTouchCoords = function() {
this._setProperty('ptouchX', this.touchX);
this._setProperty('ptouchY', this.touchY);
};
function getTouchInfo(canvas, e, i) {
i = i || 0;
var rect = canvas.getBoundingClientRect();
var touch = e.touches[i] || e.changedTouches[i];
return {
x: touch.clientX - rect.left,
y: touch.clientY - rect.top,
id: touch.identifier
};
}
/**
* The touchStarted() function is called once after every time a touch is
* registered. If no touchStarted() function is defined, the mousePressed()
* function will be called instead if it is defined.<br><br>
* Browsers may have different default behaviors attached to various touch
* events. To prevent any default behavior for this event, add "return false"
* to the end of the method.
*
* @method touchStarted
* @example
* <div>
* <code>
* // Touch within the image to change
* // the value of the rectangle
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function touchStarted() {
* if (value == 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
* </code>
* </div>
*
* <div class="norender">
* <code>
* function touchStarted() {
* ellipse(touchX, touchY, 5, 5);
* // prevent default
* return false;
* }
* </code>
* </div>
*/
p5.prototype._ontouchstart = function(e) {
var context = this._isGlobal ? window : this;
var executeDefault;
this._updateNextTouchCoords(e);
this._updateNextMouseCoords(e);
this._setProperty('touchIsDown', true);
if(typeof context.touchStarted === 'function') {
executeDefault = context.touchStarted(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.mousePressed === 'function') {
executeDefault = context.mousePressed(e);
if(executeDefault === false) {
e.preventDefault();
}
//this._setMouseButton(e);
}
};
/**
* The touchMoved() function is called every time a touch move is registered.
* If no touchMoved() function is defined, the mouseDragged() function will
* be called instead if it is defined.<br><br>
* Browsers may have different default behaviors attached to various touch
* events. To prevent any default behavior for this event, add "return false"
* to the end of the method.
*
* @method touchMoved
* @example
* <div>
* <code>
* // Move your finger across the page
* // to change its value
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function touchMoved() {
* value = value + 5;
* if (value > 255) {
* value = 0;
* }
* }
* </code>
* </div>
*
* <div class="norender">
* <code>
* function touchMoved() {
* ellipse(touchX, touchY, 5, 5);
* // prevent default
* return false;
* }
* </code>
* </div>
*/
p5.prototype._ontouchmove = function(e) {
var context = this._isGlobal ? window : this;
var executeDefault;
this._updateNextTouchCoords(e);
this._updateNextMouseCoords(e);
if (typeof context.touchMoved === 'function') {
executeDefault = context.touchMoved(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.mouseDragged === 'function') {
executeDefault = context.mouseDragged(e);
if(executeDefault === false) {
e.preventDefault();
}
}
};
/**
* The touchEnded() function is called every time a touch ends. If no
* touchEnded() function is defined, the mouseReleased() function will be
* called instead if it is defined.<br><br>
* Browsers may have different default behaviors attached to various touch
* events. To prevent any default behavior for this event, add "return false"
* to the end of the method.
*
* @method touchEnded
* @example
* <div>
* <code>
* // Release touch within the image to
* // change the value of the rectangle
*
* var value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* }
* function touchEnded() {
* if (value == 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
* </code>
* </div>
*
* <div class="norender">
* <code>
* function touchEnded() {
* ellipse(touchX, touchY, 5, 5);
* // prevent default
* return false;
* }
* </code>
* </div>
*/
p5.prototype._ontouchend = function(e) {
this._updateNextTouchCoords(e);
this._updateNextMouseCoords(e);
if (this.touches.length === 0) {
this._setProperty('touchIsDown', false);
}
var context = this._isGlobal ? window : this;
var executeDefault;
if (typeof context.touchEnded === 'function') {
executeDefault = context.touchEnded(e);
if(executeDefault === false) {
e.preventDefault();
}
} else if (typeof context.mouseReleased === 'function') {
executeDefault = context.mouseReleased(e);
if(executeDefault === false) {
e.preventDefault();
}
}
};
module.exports = p5;