forked from processing/p5.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
touch.js
293 lines (284 loc) · 7.57 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
/**
* @module Events
* @submodule Touch
* @for p5
* @requires core
*/
import p5 from '../core/main';
/**
* 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.
*
* The touches[] array is not supported on Safari and IE on touch-based
* desktops (laptops).
*
* @property {Object[]} touches
* @readOnly
*
* @example
* <div>
* <code>
* // On a touchscreen device, touch
* // the canvas using one or more fingers
* // at the same time
* function draw() {
* clear();
* let display = touches.length + ' touches';
* text(display, 5, 10);
* describe(`Number of touches currently registered are displayed
* on the canvas`);
* }
* </code>
* </div>
*/
p5.prototype.touches = [];
p5.prototype._updateTouchCoords = function(e) {
if (this._curElement !== null) {
const touches = [];
for (let i = 0; i < e.touches.length; i++) {
touches[i] = getTouchInfo(
this._curElement.elt,
this.width,
this.height,
e,
i
);
}
this._setProperty('touches', touches);
}
};
function getTouchInfo(canvas, w, h, e, i = 0) {
const rect = canvas.getBoundingClientRect();
const sx = canvas.scrollWidth / w || 1;
const sy = canvas.scrollHeight / h || 1;
const touch = e.touches[i] || e.changedTouches[i];
return {
x: (touch.clientX - rect.left) / sx,
y: (touch.clientY - rect.top) / sy,
winX: touch.clientX,
winY: touch.clientY,
id: touch.identifier
};
}
/**
* The touchStarted() function is called once after every time a touch is
* registered. If no <a href="#/p5/touchStarted">touchStarted()</a> function is defined, the <a href="#/p5/mousePressed">mousePressed()</a>
* 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
* @param {TouchEvent} [event] optional TouchEvent callback argument.
* @example
* <div>
* <code>
* // Touch within the image to change
* // the value of the rectangle
*
* let value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* describe('50-by-50 black rect turns white with touch event.');
* }
* function touchStarted() {
* if (value === 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
* </code>
* </div>
*
* <div class="norender">
* <code>
* function touchStarted() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
* describe('no image displayed');
* </code>
* </div>
*
* <div class="norender">
* <code>
* // returns a TouchEvent object
* // as a callback argument
* function touchStarted(event) {
* console.log(event);
* }
* describe('no image displayed');
* </code>
* </div>
*/
p5.prototype._ontouchstart = function(e) {
const context = this._isGlobal ? window : this;
let executeDefault;
this._setProperty('mouseIsPressed', true);
this._updateTouchCoords(e);
this._updateNextMouseCoords(e);
this._updateMouseCoords(); // reset pmouseXY at the start of each touch event
if (typeof context.touchStarted === 'function') {
executeDefault = context.touchStarted(e);
if (executeDefault === false) {
e.preventDefault();
}
// only safari needs this manual fallback for consistency
} else if (
navigator.userAgent.toLowerCase().includes('safari') &&
typeof context.mousePressed === 'function'
) {
executeDefault = context.mousePressed(e);
if (executeDefault === false) {
e.preventDefault();
}
}
};
/**
* The <a href="#/p5/touchMoved">touchMoved()</a> function is called every time a touch move is registered.
* If no <a href="#/p5/touchMoved">touchMoved()</a> function is defined, the <a href="#/p5/mouseDragged">mouseDragged()</a> 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
* @param {TouchEvent} [event] optional TouchEvent callback argument.
* @example
* <div>
* <code>
* // Move your finger across the page
* // to change its value
*
* let value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* describe('50-by-50 black rect turns lighter with touch until white. resets');
* }
* function touchMoved() {
* value = value + 5;
* if (value > 255) {
* value = 0;
* }
* }
* </code>
* </div>
*
* <div class="norender">
* <code>
* function touchMoved() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
* describe('no image displayed');
* </code>
* </div>
*
* <div class="norender">
* <code>
* // returns a TouchEvent object
* // as a callback argument
* function touchMoved(event) {
* console.log(event);
* }
* describe('no image displayed');
* </code>
* </div>
*/
p5.prototype._ontouchmove = function(e) {
const context = this._isGlobal ? window : this;
let executeDefault;
this._updateTouchCoords(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 <a href="#/p5/touchEnded">touchEnded()</a> function is called every time a touch ends. If no
* <a href="#/p5/touchEnded">touchEnded()</a> function is defined, the <a href="#/p5/mouseReleased">mouseReleased()</a> 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
* @param {TouchEvent} [event] optional TouchEvent callback argument.
* @example
* <div>
* <code>
* // Release touch within the image to
* // change the value of the rectangle
*
* let value = 0;
* function draw() {
* fill(value);
* rect(25, 25, 50, 50);
* describe('50-by-50 black rect turns white with touch.');
* }
* function touchEnded() {
* if (value === 0) {
* value = 255;
* } else {
* value = 0;
* }
* }
* </code>
* </div>
*
* <div class="norender">
* <code>
* function touchEnded() {
* ellipse(mouseX, mouseY, 5, 5);
* // prevent default
* return false;
* }
* describe('no image displayed');
* </code>
* </div>
*
* <div class="norender">
* <code>
* // returns a TouchEvent object
* // as a callback argument
* function touchEnded(event) {
* console.log(event);
* }
* describe('no image displayed');
* </code>
* </div>
*/
p5.prototype._ontouchend = function(e) {
this._setProperty('mouseIsPressed', false);
this._updateTouchCoords(e);
this._updateNextMouseCoords(e);
const context = this._isGlobal ? window : this;
let 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();
}
}
};
export default p5;