p5.js Touch | touchStarted()
The touchStarted() function in p5.js is called once after every time a touch is registered. If touchStarted() function is not defined, the mousePressed() function will be called instead if it is defined.
Syntax:
touchStarted([Event])
Below program illustrates the touchStarted() function in p5.js:
Example-1:
let valueX; let valueY; function setup() { // Create Canvas of size 500*500 createCanvas(500, 500); } function draw() { // set background color background(200); fill('green'); // set text and text size textSize(25); text('Touch to change color', 30, 30); // fill color according to touchStarted() fill(valueX, 255 - valueY, 255 - valueX); // draw ellipse ellipse(mouseX, mouseY, 115, 115); } function touchStarted() { valueX = mouseX % 255; valueY = mouseY % 255; } |
chevron_right
filter_none
Output:

Example-2:
let valueX; let valueY; function setup() { // Create Canvas of size 500*500 createCanvas(500, 500); } function draw() { // set background color background(200); fill('green'); // set text and text size textSize(25); text('Touch to change color', 30, 30); // fill color according to mouseMoved() fill(valueX, 255 - valueY, 255 - valueX); // draw rectangle rect(mouseX, mouseY, 115, 115); fill(valueY, 255 - valueX, 255 - valueX); rect(mouseX, mouseY + 115, 115, 115); fill(255 - valueY, 255 - valueX, 255 - valueY); rect(mouseX - 115, mouseY, 115, 115); fill(255 - valueY, 255 - valueY, 255 - valueY); rect(mouseX - 115, mouseY + 115, 115, 115); } function touchStarted() { valueX = mouseX % 255; valueY = mouseY % 255; } |
chevron_right
filter_none
Output:

Reference: https://p5js.org/reference/#/p5/touchStarted
Recommended Posts:
- PHP | touch( ) function
- p5.js Touch | touchMoved()
- p5.js Touch | touchEnded()
- How to detect touch screen device using JavaScript?
- JavaScript Interview Questions and Answers
- ReactJS | forms
- How to remove Objects from Associative Array in JavaScript ?
- How to change the Content of a <textarea> using JavaScript ?
- How to check input file is empty or not using JavaScript/jQuery ?
- How to print JavaScript console with PHP ?
- How to set PHP development environment in windows ?
- How to set font size based on window size using JavaScript ?
- How to check two elements are same using jQuery/JavaScript ?
- How to globally replace a forward slash in a JavaScript string ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



