Skip to content

Commit b845f61

Browse files
author
Lauren McCarthy
committed
adding touch elt specific methods
1 parent 55c1113 commit b845f61

File tree

4 files changed

+155
-16
lines changed

4 files changed

+155
-16
lines changed

lib/p5.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! p5.js v0.3.13 December 06, 2014 */
1+
/*! p5.js v0.3.13 December 10, 2014 */
22
var shim = function (require) {
33
window.requestDraw = function () {
44
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback, element) {
@@ -561,6 +561,7 @@ var p5Element = function (require, core) {
561561
};
562562
p5.Element.prototype.mousePressed = function (fxn) {
563563
attachListener('mousedown', fxn, this);
564+
attachListener('touchstart', fxn, this);
564565
return this;
565566
};
566567
p5.Element.prototype.mouseWheel = function (fxn) {
@@ -569,6 +570,7 @@ var p5Element = function (require, core) {
569570
};
570571
p5.Element.prototype.mouseReleased = function (fxn) {
571572
attachListener('mouseup', fxn, this);
573+
attachListener('touchend', fxn, this);
572574
return this;
573575
};
574576
p5.Element.prototype.mouseClicked = function (fxn) {
@@ -577,6 +579,7 @@ var p5Element = function (require, core) {
577579
};
578580
p5.Element.prototype.mouseMoved = function (fxn) {
579581
attachListener('mousemove', fxn, this);
582+
attachListener('touchmove', fxn, this);
580583
return this;
581584
};
582585
p5.Element.prototype.mouseOver = function (fxn) {
@@ -587,6 +590,21 @@ var p5Element = function (require, core) {
587590
attachListener('mouseout', fxn, this);
588591
return this;
589592
};
593+
p5.Element.prototype.touchStarted = function (fxn) {
594+
attachListener('touchstart', fxn, this);
595+
attachListener('mousedown', fxn, this);
596+
return this;
597+
};
598+
p5.Element.prototype.touchMoved = function (fxn) {
599+
attachListener('touchmove', fxn, this);
600+
attachListener('mousemove', fxn, this);
601+
return this;
602+
};
603+
p5.Element.prototype.touchEnded = function (fxn) {
604+
attachListener('touchend', fxn, this);
605+
attachListener('mouseup', fxn, this);
606+
return this;
607+
};
590608
function attachListener(ev, fxn, ctx) {
591609
var f = fxn.bind(ctx);
592610
ctx.elt.addEventListener(ev, f, false);

lib/p5.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/environment/environment.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -325,26 +325,19 @@ define(function(require) {
325325
};
326326

327327
/**
328-
* If argument is given, sets the sketch to fullscreen or not based on the
329-
* value of the argument. If no argument is given, returns the current
330-
* fullscreen state. Note that due to browser restrictions this can only
331-
* be called on user input, for example, on mouse press like the example
332-
* below.
328+
* Toggles pixel scaling for high pixel density displays. By default
329+
* pixel scaling is on, call devicePixelScaling(false) to turn it off.
333330
*
334331
* @method devicePixelScaling
335332
* @example
336333
* <div>
337334
* <code>
338335
* function setup() {
336+
* devicePixelScaling(false);
339337
* createCanvas(100, 100);
340-
* }
341-
*
342-
* function draw() {
338+
* background(200);
343339
* ellipse(width/2, height/2, 50, 50);
344340
* }
345-
* function mousePressed() {
346-
* devicePixelScaling(false);
347-
* }
348341
* </code>
349342
* </div>
350343
*/

src/objects/p5.Element.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)