@@ -146,6 +146,7 @@ define(function(require) {
146146 */
147147 p5 . Element . prototype . mousePressed = function ( fxn ) {
148148 attachListener ( 'mousedown' , fxn , this ) ;
149+ attachListener ( 'touchstart' , fxn , this ) ;
149150 return this ;
150151 } ;
151152
@@ -180,6 +181,7 @@ define(function(require) {
180181 */
181182 p5 . Element . prototype . mouseReleased = function ( fxn ) {
182183 attachListener ( 'mouseup' , fxn , this ) ;
184+ attachListener ( 'touchend' , fxn , this ) ;
183185 return this ;
184186 } ;
185187
@@ -211,6 +213,7 @@ define(function(require) {
211213 */
212214 p5 . Element . prototype . mouseMoved = function ( fxn ) {
213215 attachListener ( 'mousemove' , fxn , this ) ;
216+ attachListener ( 'touchmove' , fxn , this ) ;
214217 return this ;
215218 } ;
216219
@@ -244,6 +247,131 @@ define(function(require) {
244247 return this ;
245248 } ;
246249
250+ /**
251+ * The .touchStarted() function is called once after every time a touch is
252+ * registered. This can be used to attach element specific event listeners.
253+ *
254+ * @method touchStarted
255+ * @param {Function } fxn function to be fired when touch is
256+ * started over the element.
257+ * @return {p5.Element }
258+ * @example
259+ * <div class='norender'><code>
260+ * var cnv;
261+ * var d;
262+ * var g;
263+ * function setup() {
264+ * cnv = createCanvas(100, 100);
265+ * cnv.touchStarted(changeGray); // attach listener for
266+ * // canvas click only
267+ * d = 10;
268+ * g = 100;
269+ * }
270+ *
271+ * function draw() {
272+ * background(g);
273+ * ellipse(width/2, height/2, d, d);
274+ * }
275+ *
276+ * // this function fires with any touch anywhere
277+ * function touchStarted() {
278+ * d = d + 10;
279+ * }
280+ *
281+ * // this function fires only when cnv is clicked
282+ * function changeGray() {
283+ * g = random(0, 255);
284+ * }
285+ * </code></div>
286+ *
287+ */
288+ p5 . Element . prototype . touchStarted = function ( fxn ) {
289+ attachListener ( 'touchstart' , fxn , this ) ;
290+ attachListener ( 'mousedown' , fxn , this ) ;
291+ return this ;
292+ } ;
293+
294+ /**
295+ * The .touchMoved() function is called once after every time a touch move is
296+ * registered. This can be used to attach element specific event listeners.
297+ *
298+ * @method touchMoved
299+ * @param {Function } fxn function to be fired when touch is moved
300+ * over the element.
301+ * @return {p5.Element }
302+ * @example
303+ * <div class='norender'><code>
304+ * var cnv;
305+ * var g;
306+ * function setup() {
307+ * cnv = createCanvas(100, 100);
308+ * cnv.touchMoved(changeGray); // attach listener for
309+ * // canvas click only
310+ * g = 100;
311+ * }
312+ *
313+ * function draw() {
314+ * background(g);
315+ * }
316+ *
317+ * // this function fires only when cnv is clicked
318+ * function changeGray() {
319+ * g = random(0, 255);
320+ * }
321+ * </code></div>
322+ *
323+ */
324+ p5 . Element . prototype . touchMoved = function ( fxn ) {
325+ attachListener ( 'touchmove' , fxn , this ) ;
326+ attachListener ( 'mousemove' , fxn , this ) ;
327+ return this ;
328+ } ;
329+
330+ /**
331+ * The .touchEnded() function is called once after every time a touch is
332+ * registered. This can be used to attach element specific event listeners.
333+ *
334+ * @method touchEnded
335+ * @param {Function } fxn function to be fired when touch is
336+ * ended over the element.
337+ * @return {p5.Element }
338+ * @example
339+ * <div class='norender'><code>
340+ * var cnv;
341+ * var d;
342+ * var g;
343+ * function setup() {
344+ * cnv = createCanvas(100, 100);
345+ * cnv.touchEnded(changeGray); // attach listener for
346+ * // canvas click only
347+ * d = 10;
348+ * g = 100;
349+ * }
350+ *
351+ * function draw() {
352+ * background(g);
353+ * ellipse(width/2, height/2, d, d);
354+ * }
355+ *
356+ * // this function fires with any touch anywhere
357+ * function touchEnded() {
358+ * d = d + 10;
359+ * }
360+ *
361+ * // this function fires only when cnv is clicked
362+ * function changeGray() {
363+ * g = random(0, 255);
364+ * }
365+ * </code></div>
366+ *
367+ */
368+ p5 . Element . prototype . touchEnded = function ( fxn ) {
369+ attachListener ( 'touchend' , fxn , this ) ;
370+ attachListener ( 'mouseup' , fxn , this ) ;
371+ return this ;
372+ } ;
373+
374+
247375 function attachListener ( ev , fxn , ctx ) {
248376 // LM removing, not sure why we had this?
249377 // var _this = ctx;
0 commit comments