p5.js |鼠标 |鼠标按下
p5.js 中的mouseIsPressed系统变量用于存储布尔值。如果按下鼠标,则存储 True,否则存储 False。
句法:
mouseIsPressed
下面的程序说明了 p5.js 中的 mouseIsPressed 变量:
示例 1:此示例使用 mouseIsPressed 变量来检查鼠标是否按下。
function setup() {
// Create canvas of given size
createCanvas(500, 250);
// Set the text size
textSize(30);
}
function draw() {
// Set the background color
background('green');
fill('white');
// If mouse is pressed then if part will
// execute otherwise else part will execute
if (mouseIsPressed) {
text("Mouse is Pressed", 120, 100);
}
else {
text("Mouse is Released", 120, 100);
}
}
输出:
示例 2:
function setup() {
// Create Canvas of given size
createCanvas(300, 150);
}
function draw() {
// Set the background color
background('green');
fill('white');
// Use mouseIsPressed variable
if (mouseIsPressed) {
ellipse(50, 50, 50, 50);
}
else {
rect(25, 25, 50, 50);
}
}
输出:
参考: https://p5js.org/reference/#/p5/mouseIsPressed