A function that's called once each time the user touches the screen.
Declaring a function called touchStarted() sets a code block to run automatically each time the user begins touching a touchscreen device:
function touchStarted() {
// Code to run.
}
The touches array will be updated with the most recent touch points when touchStarted() is called by p5.js:
function touchStarted() {
// Paint over the background.
background(200);
// Mark each touch point once with a circle.
for (let touch of touches) {
circle(touch.x, touch.y, 40);
}
}
The parameter, event, is optional. touchStarted() will be passed a TouchEvent object with properties that describe the touch event:
function touchStarted(event) {
// Code to run that uses the event.
console.log(event);
}
On touchscreen devices, mousePressed() will run when a user’s touch starts if touchStarted() isn’t declared. If touchStarted() is declared, then touchStarted() will run when a user’s touch starts and mousePressed() won’t.
Note: touchStarted(), touchEnded(), and touchMoved() are all related. touchStarted() runs as soon as the user touches a touchscreen device. touchEnded() runs as soon as the user ends a touch. touchMoved() runs repeatedly as the user moves any touch points.
Ejemplos
Sintaxis
touchStarted([event])
Parámetros
optional TouchEvent argument.
Referencias Relacionadas
touchEnded
Una función que se ejecuta una vez por cada toque en la pantalla que termina.
touches
Un Array de todos los puntos táctiles actuales en un dispositivo táctil.
touchMoved
Una función que se ejecuta cada vez que el usuario toca la pantalla y se mueve.
touchStarted
Una función que se ejecuta una vez por cada vez que el usuario toca la pantalla.