改变光标样式。
第一个参数,type
,设置要显示的光标类型。内置选项有: ARROW
、CROSS
、 HAND
、MOVE
、TEXT
和 WAIT
。 "cursor()
也可以识别以字符串形式传递的标准 CSS 光标属性: 'help'
,'wait'
,'crosshair'
," "'not-allowed'
,'zoom-in'
和 'grab'
。" 如果传递的是图像路径,例如: "cursor('/assets/target.png')
,那么光标将以图像的形式展现出来。" 图像格式必须是 .cur、.gif、.jpg、.jpeg 或 .png,要以这样的形式传递: ,最高像素为 32x32。
x
和 y
都是可选项。如果用图像作光标,x
和 y
用来设置图像中指向的位置。 它们初始值都是 0,此时光标指向图像左上角。并且 x
和 y
的值必须分别小于图像的宽高。
示例
function setup() {
createCanvas(100, 100);
describe('A gray square. The cursor appears as crosshairs.');
}
function draw() {
background(200);
// Set the cursor to crosshairs: +
cursor(CROSS);
}
function setup() {
createCanvas(100, 100);
describe('A gray square divided into quadrants. The cursor image changes when the mouse moves to each quadrant.');
}
function draw() {
background(200);
// Divide the canvas into quadrants.
line(50, 0, 50, 100);
line(0, 50, 100, 50);
// Change cursor based on mouse position.
if (mouseX < 50 && mouseY < 50) {
cursor(CROSS);
} else if (mouseX > 50 && mouseY < 50) {
cursor('progress');
} else if (mouseX > 50 && mouseY > 50) {
cursor('https://avatars0.githubusercontent.com/u/1617169?s=16');
} else {
cursor('grab');
}
}
function setup() {
createCanvas(100, 100);
describe('An image of three purple curves follows the mouse. The image shifts when the mouse is pressed.');
}
function draw() {
background(200);
// Change the cursor's active spot
// when the mouse is pressed.
if (mouseIsPressed === true) {
cursor('https://avatars0.githubusercontent.com/u/1617169?s=16', 8, 8);
} else {
cursor('https://avatars0.githubusercontent.com/u/1617169?s=16');
}
}
语法
cursor(type, [x], [y])
参数
type
字符串|常量:
内置:ARROW、CROSS、HAND、MOVE、TEXT、 WAIT 原生 CSS 属性:'grab'、'progress'等 光标图片路径
x
数字:
光标在水平方向的位置
y
数字:
光标在垂直方向的位置
Notice any errors or typos? Please let us know. Please feel free to edit src/core/environment.js and open a pull request!