参考 mouseClicked()

mouseClicked()

在鼠标按钮被按下并释放后调用一次的函数。

声明函数 mouseClicked() 会设置一个代码块,当用户在按下鼠标按钮后释放时自动运行:

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

鼠标系统变量,如 mouseXmouseY,将在 p5.js 调用 mouseClicked() 时更新为它们最新的值:

function mouseClicked() { if (mouseX &lt; 50) { // Code to run if the mouse is on the left. } <p> if (mouseY &gt; 50) { // Code to run if the mouse is near the bottom. } } </p>

参数 event 是可选的。mouseClicked() 总是会传递一个 MouseEvent 对象,其属性描述了鼠标点击事件:

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

在触摸屏设备上,如果没有声明 touchEnded()mouseClicked() 会在用户触摸结束时运行。如果声明了 touchEnded(),那么当用户触摸结束时 touchEnded() 会运行,并且 mouseClicked() 不会运行。

浏览器可能会对各种鼠标事件附加默认行为。例如,一些浏览器在用户按下鼠标按钮时移动鼠标会高亮显示文本。为了防止此事件的任何默认行为,可以在函数的末尾添加 return false;

注意:mousePressed()mouseReleased(), 和 mouseClicked() 都是相关联的。mousePressed() 会在用户点击鼠标时立即运行。mouseReleased() 会在用户释放鼠标点击时立即运行。mouseClicked() 会在 mouseReleased() 之后立即运行。

示例

语法

mouseClicked([event])

参数

event

optional MouseEvent argument.

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

相关参考