📜  p5.js 触摸 |触摸结束()

📅  最后修改于: 2022-05-13 01:56:44.742000             🧑  作者: Mango

p5.js 触摸 |触摸结束()

p5.js 中的touchEnded()函数在触摸结束时被调用。如果未定义touchEnded()函数,则将调用mouseReleased()函数(如果已定义)。

句法:

touchEnded([Event])

下面的程序说明了 p5.js 中的 touchEnded()函数:
示例 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 touchEnded() 
    fill(valueX, 255 - valueY, 255 - valueX);
    // draw ellipse  
    ellipse(mouseX, mouseY, 115, 115);
  
}
  
function touchEnded() {
    valueX = mouseX % 255;
    valueY = mouseY % 255;
}

输出:

示例 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 and Move to change color', 30, 30);
    // fill color according to touchEnded() 
    
    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 touchEnded() {
    valueX = mouseX % 255;
    valueY = mouseY % 255;
}

输出:

参考: https://p5js.org/reference/#/p5/touchEnded