A function that's called when the user touches the screen and moves.
Declaring the function touchMoved()
sets a code block to run automatically when the user touches a touchscreen device and moves:
function touchMoved() {
// Code to run.
}
The touches array will be updated with the most recent touch points when touchMoved()
is called by p5.js:
function touchMoved() {
// Paint over the background.
background(200);
// Mark each touch point while the user moves.
for (let touch of touches) {
circle(touch.x, touch.y, 40);
}
}
The parameter, event, is optional. touchMoved()
will be passed a TouchEvent object with properties that describe the touch event:
function touchMoved(event) {
// Code to run that uses the event.
console.log(event);
}
On touchscreen devices, mouseDragged() will run when the user’s touch points move if touchMoved()
isn’t declared. If touchMoved()
is declared, then touchMoved()
will run when a user’s touch points move and mouseDragged() 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
touchMoved([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.