参考 touchStarted()

touchStarted()

每当用户触摸屏幕时调用的函数。

每当用户开始触摸触屏设备时,声明一个名为 touchStarted() 的函数会设置一个代码块,自动运行:

function touchStarted() { // Code to run. }

当 p5.js 调用 touchStarted() 时,touches 数组将被更新为最新的触摸点:

function touchStarted() { // Paint over the background. background(200); <p> // Mark each touch point once with a circle. for (let touch of touches) { circle(touch.x, touch.y, 40); } } </p>

参数 event 是可选的。touchStarted() 会被传递为一个描述触摸事件属性的 TouchEvent 对象:

function touchStarted(event) { // Code to run that uses the event. console.log(event); }

在触屏设备上,如果未声明 touchStarted(),当用户触摸开始时,mousePressed() 会运行。如果声明了 touchStarted(),那么当用户触摸开始时将运行 touchStarted(),而 mousePressed() 不会运行。

注意:touchStarted()touchEnded()touchMoved() 都是相关的。touchStarted() 会在用户触摸触屏设备时立即运行。 touchEnded() 会在用户结束触摸时立即运行。touchMoved() 会在用户移动任何触摸点时重复运行。

示例

语法

touchStarted([event])

参数

event

可选的 TouchEvent 回调参数。

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

相关参考