Skip to content

Commit 14681f4

Browse files
authored
Add p5.Element touch events
1 parent 9586bcf commit 14681f4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/data.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,46 @@ function addData(p5, fn){
88
fn.touchMoved = function (...args) {
99
return this.mouseDragged(...args);
1010
};
11+
p5.Element.prototype.touchStarted = function (cb) {
12+
if (cb === false) {
13+
return this.mousePressed(false);
14+
}
15+
return this.mousePressed(function (event) {
16+
if (this._hasTouchMoveHandler) {
17+
this._pointers = this._pointers || {};
18+
this._pointers[event.pointerId] = true;
19+
}
20+
event.target.setPointerCapture(event.pointerId);
21+
return cb(event);
22+
});
23+
};
24+
p5.Element.prototype.touchEnded = function (cb) {
25+
if (cb === false) {
26+
return this.mouseReleased(false);
27+
}
28+
return this.mouseReleased(function (event) {
29+
if (this._hasTouchMoveHandler) {
30+
this._pointers = this._pointers || {};
31+
if (this._pointers[event.pointerId]) {
32+
event.target.releasePointerCapture(event.pointerId);
33+
delete this._pointers[event.pointerId];
34+
}
35+
}
36+
return cb(event);
37+
});
38+
};
39+
p5.Element.prototype.touchMoved = function (cb) {
40+
if (cb === false) {
41+
this._hasTouchMoveHandler = false;
42+
return this.mouseMoved(false);
43+
}
44+
this._hasTouchMoveHandler = true;
45+
return this.mouseMoved(function (event) {
46+
if (Object.keys(this._pointers || {}).length > 0) {
47+
return cb(event);
48+
}
49+
});
50+
};
1151

1252
fn.append = function (array, value) {
1353
array.push(value);

0 commit comments

Comments
 (0)