Skip to content

Commit c69e4c5

Browse files
committed
Replace p5.Element mouse/touch events with pointer events
1 parent e37bfa6 commit c69e4c5

File tree

2 files changed

+32
-312
lines changed

2 files changed

+32
-312
lines changed

src/dom/p5.Element.js

Lines changed: 8 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,12 +1571,14 @@ class Element {
15711571
// For details, see https://github.com/processing/p5.js/issues/3087.
15721572
const eventPrependedFxn = function (event) {
15731573
this._pInst.mouseIsPressed = true;
1574+
this._pInst._activePointers.set(e.pointerId, e);
15741575
this._pInst._setMouseButton(event);
1576+
this._pInst._updatePointerCoords(e);
15751577
// Pass along the return-value of the callback:
15761578
return fxn.call(this, event);
15771579
};
15781580
// Pass along the event-prepended form of the callback.
1579-
Element._adjustListener('mousedown', eventPrependedFxn, this);
1581+
Element._adjustListener('pointerdown', eventPrependedFxn, this);
15801582
return this;
15811583
}
15821584

@@ -1746,7 +1748,7 @@ class Element {
17461748
* </div>
17471749
*/
17481750
mouseReleased(fxn) {
1749-
Element._adjustListener('mouseup', fxn, this);
1751+
Element._adjustListener('pointerup', fxn, this);
17501752
return this;
17511753
}
17521754

@@ -1831,7 +1833,7 @@ class Element {
18311833
* </div>
18321834
*/
18331835
mouseMoved(fxn) {
1834-
Element._adjustListener('mousemove', fxn, this);
1836+
Element._adjustListener('pointermove', fxn, this);
18351837
return this;
18361838
}
18371839

@@ -1872,7 +1874,7 @@ class Element {
18721874
* </div>
18731875
*/
18741876
mouseOver(fxn) {
1875-
Element._adjustListener('mouseover', fxn, this);
1877+
Element._adjustListener('pointerover', fxn, this);
18761878
return this;
18771879
}
18781880

@@ -1913,140 +1915,11 @@ class Element {
19131915
* </div>
19141916
*/
19151917
mouseOut(fxn) {
1916-
Element._adjustListener('mouseout', fxn, this);
1918+
Element._adjustListener('pointerout', fxn, this);
19171919
return this;
19181920
}
19191921

1920-
/**
1921-
* Calls a function when the element is touched.
1922-
*
1923-
* Calling `myElement.touchStarted(false)` disables the function.
1924-
*
1925-
* Note: Touch functions only work on mobile devices.
1926-
*
1927-
* @param {Function|Boolean} fxn function to call when the touch
1928-
* starts.
1929-
* `false` disables the function.
1930-
* @chainable
1931-
*
1932-
* @example
1933-
* <div>
1934-
* <code>
1935-
* function setup() {
1936-
* // Create a canvas element and
1937-
* // assign it to cnv.
1938-
* let cnv = createCanvas(100, 100);
1939-
*
1940-
* background(200);
1941-
*
1942-
* // Call randomColor() when the
1943-
* // user touches the canvas.
1944-
* cnv.touchStarted(randomColor);
1945-
*
1946-
* describe('A gray square changes color when the user touches the canvas.');
1947-
* }
1948-
*
1949-
* // Paint the background either
1950-
* // red, yellow, blue, or green.
1951-
* function randomColor() {
1952-
* let c = random(['red', 'yellow', 'blue', 'green']);
1953-
* background(c);
1954-
* }
1955-
* </code>
1956-
* </div>
1957-
*/
1958-
touchStarted(fxn) {
1959-
Element._adjustListener('touchstart', fxn, this);
1960-
return this;
1961-
}
1962-
1963-
/**
1964-
* Calls a function when the user touches the element and moves.
1965-
*
1966-
* Calling `myElement.touchMoved(false)` disables the function.
1967-
*
1968-
* Note: Touch functions only work on mobile devices.
1969-
*
1970-
* @param {Function|Boolean} fxn function to call when the touch
1971-
* moves over the element.
1972-
* `false` disables the function.
1973-
* @chainable
1974-
* @example
1975-
* <div>
1976-
* <code>
1977-
* function setup() {
1978-
* // Create a canvas element and
1979-
* // assign it to cnv.
1980-
* let cnv = createCanvas(100, 100);
1981-
*
1982-
* background(200);
1983-
*
1984-
* // Call randomColor() when the
1985-
* // user touches the canvas
1986-
* // and moves.
1987-
* cnv.touchMoved(randomColor);
1988-
*
1989-
* describe('A gray square changes color when the user touches the canvas and moves.');
1990-
* }
1991-
*
1992-
* // Paint the background either
1993-
* // red, yellow, blue, or green.
1994-
* function randomColor() {
1995-
* let c = random(['red', 'yellow', 'blue', 'green']);
1996-
* background(c);
1997-
* }
1998-
* </code>
1999-
* </div>
2000-
*/
2001-
touchMoved(fxn) {
2002-
Element._adjustListener('touchmove', fxn, this);
2003-
return this;
2004-
}
2005-
2006-
/**
2007-
* Calls a function when the user stops touching the element.
2008-
*
2009-
* Calling `myElement.touchMoved(false)` disables the function.
2010-
*
2011-
* Note: Touch functions only work on mobile devices.
2012-
*
2013-
* @param {Function|Boolean} fxn function to call when the touch
2014-
* ends.
2015-
* `false` disables the function.
2016-
* @chainable
2017-
* @example
2018-
* <div>
2019-
* <code>
2020-
* function setup() {
2021-
* // Create a canvas element and
2022-
* // assign it to cnv.
2023-
* let cnv = createCanvas(100, 100);
2024-
*
2025-
* background(200);
2026-
*
2027-
* // Call randomColor() when the
2028-
* // user touches the canvas,
2029-
* // then lifts their finger.
2030-
* cnv.touchEnded(randomColor);
2031-
*
2032-
* describe('A gray square changes color when the user touches the canvas, then lifts their finger.');
2033-
* }
2034-
*
2035-
* // Paint the background either
2036-
* // red, yellow, blue, or green.
2037-
* function randomColor() {
2038-
* let c = random(['red', 'yellow', 'blue', 'green']);
2039-
* background(c);
2040-
* }
2041-
* </code>
2042-
* </div>
2043-
*/
2044-
touchEnded(fxn) {
2045-
Element._adjustListener('touchend', fxn, this);
2046-
return this;
2047-
}
2048-
2049-
/**
1922+
/**
20501923
* Calls a function when a file is dragged over the element.
20511924
*
20521925
* Calling `myElement.dragOver(false)` disables the function.

0 commit comments

Comments
 (0)