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.
Examples
Syntax
touchStarted([event])
Parameters
optional TouchEvent
argument.
Related References
touchEnded
A function that's called once each time a screen touch ends.
touchMoved
A function that's called when the user touches the screen and moves.
touchStarted
A function that's called once each time the user touches the screen.
touches
An Array of all the current touch points on a touchscreen device.