Reference touchStarted()

touchStarted()

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

event

optional TouchEvent argument.

Notice any errors or typos? Please let us know. Please feel free to edit src/events/touch.js and open a pull request!

Related References