-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathtouch.js
More file actions
198 lines (179 loc) · 6.08 KB
/
touch.js
File metadata and controls
198 lines (179 loc) · 6.08 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
suite('Touch Events', function() {
let myp5;
let canvas;
let touchObj1;
let touchObj2;
let touchEvent1;
let touchEvent2;
setup(function(done) {
new p5(function(p) {
p.setup = function() {
myp5 = p;
canvas = myp5._curElement.elt;
touchObj1 = new Touch({
target: canvas,
clientX: 100,
clientY: 100,
identifier: 36
});
touchObj2 = new Touch({
target: canvas,
clientX: 200,
clientY: 200,
identifier: 35
});
touchEvent1 = new TouchEvent('touchmove', {
touches: [touchObj1, touchObj2]
});
touchEvent2 = new TouchEvent('touchmove', {
touches: [touchObj2]
});
done();
};
});
});
teardown(function() {
myp5.remove();
});
suite('p5.prototype.touches', function() {
test('should be an empty array', function() {
assert.deepEqual(myp5.touches, []);
});
test('should be an array of multiple touches', function() {
window.dispatchEvent(touchEvent1);
assert.strictEqual(myp5.touches.length, 2);
});
test('should contain the touch registered', function() {
window.dispatchEvent(touchEvent2);
assert.strictEqual(myp5.touches[0].id, 35);
});
});
suite('touchStarted', function() {
test('touchStarted should be fired when a touch is registered', function() {
let count = 0;
myp5.touchStarted = function() {
count += 1;
};
window.dispatchEvent(new TouchEvent('touchstart'));
assert.strictEqual(count, 1);
});
test('should be fired when a touch starts over the element', function() {
let count = 0;
let div = myp5.createDiv();
let divTouchStarted = function() {
count += 1;
};
div.touchStarted(divTouchStarted);
div.elt.dispatchEvent(new TouchEvent('touchstart'));
assert.strictEqual(count, 1);
});
test('touchStarted functions on multiple instances must run once', async function() {
let sketchFn = function(sketch, resolve, reject) {
let count = 0;
sketch.touchStarted = function() {
count += 1;
};
sketch.finish = function() {
resolve(count);
};
};
let sketches = parallelSketches([sketchFn, sketchFn]); //create two sketches
await sketches.setup; //wait for all sketches to setup
window.dispatchEvent(new TouchEvent('touchstart'));
sketches.end(); //resolve all sketches by calling their finish functions
let counts = await sketches.result;
assert.deepEqual(counts, [1, 1]);
});
});
suite('touchMoved', function() {
test('touchMoved should be fired when a touchmove is registered', function() {
let count = 0;
myp5.touchMoved = function() {
count += 1;
};
window.dispatchEvent(touchEvent2);
assert.strictEqual(count, 1);
});
test('should be fired when a touchmove is registered over the element', function() {
let count = 0;
let div = myp5.createDiv();
let divTouchMoved = function() {
count += 1;
};
div.touchMoved(divTouchMoved);
div.elt.dispatchEvent(touchEvent2);
assert.strictEqual(count, 1);
});
test('touchMoved functions on multiple instances must run once', async function() {
let sketchFn = function(sketch, resolve, reject) {
let count = 0;
sketch.touchMoved = function() {
count += 1;
};
sketch.finish = function() {
resolve(count);
};
};
let sketches = parallelSketches([sketchFn, sketchFn]); //create two sketches
await sketches.setup; //wait for all sketches to setup
window.dispatchEvent(touchEvent2);
sketches.end(); //resolve all sketches by calling their finish functions
let counts = await sketches.result;
assert.deepEqual(counts, [1, 1]);
});
});
suite('touchEnded', function() {
test('touches[] should contain changedTouches data inside touchEnded', function() {
// During touchend, the W3C spec removes the lifted finger from e.touches
// and places it in e.changedTouches only. Verify that p5 surfaces those
// positions in the touches[] array so user code can read them.
let touchesInsideCallback = null;
myp5.touchEnded = function() {
touchesInsideCallback = [...myp5.touches];
};
const endEvent = new TouchEvent('touchend', {
touches: [], // spec: lifted touch is gone from touches
changedTouches: [touchObj1] // spec: lifted touch lives here
});
window.dispatchEvent(endEvent);
assert.isNotNull(touchesInsideCallback);
assert.strictEqual(touchesInsideCallback.length, 1);
assert.strictEqual(touchesInsideCallback[0].id, 36);
});
test('touchEnded must run when a touch is registered', function() {
let count = 0;
myp5.touchEnded = function() {
count += 1;
};
window.dispatchEvent(new TouchEvent('touchend'));
assert.strictEqual(count, 1);
});
test('should be fired when a touch starts over the element', function() {
let count = 0;
let div = myp5.createDiv();
let divTouchEnded = function() {
count += 1;
};
div.touchEnded(divTouchEnded);
div.elt.dispatchEvent(new TouchEvent('touchend'));
assert.strictEqual(count, 1);
});
test('touchEnded functions on multiple instances must run once', async function() {
let sketchFn = function(sketch, resolve, reject) {
let count = 0;
sketch.touchEnded = function() {
count += 1;
};
sketch.finish = function() {
resolve(count);
};
};
let sketches = parallelSketches([sketchFn, sketchFn]); //create two sketches
await sketches.setup; //wait for all sketches to setup
window.dispatchEvent(new TouchEvent('touchend'));
sketches.end(); //resolve all sketches by calling their finish functions
let counts = await sketches.result;
assert.deepEqual(counts, [1, 1]);
});
});
});