当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


p5.js mouseClicked()用法及代码示例


p5.j​​s 中的 mouseClicked() 函数在按下和释放鼠标按钮时起作用。浏览器可能包含附加到各种鼠标事件的不同默认行为。要防止此事件的默认行为,请将 “return false” 添加到方法的末尾。

用法:

mouseClicked(Event)

以下示例程序旨在说明 p5.js 中的 mouseClicked() 函数:

范例1:此示例说明了 mouseClicked() 函数。


let valueX;
let valueY;
  
function setup() {
      
    // Create Canvas
    createCanvas(500, 500);
}
   
function draw() {
      
    // Set the background color
    background(200); 
      
    // SEt the filled color
    fill('green');
      
    // Set the font size
    textSize(25);
      
    text('Click mouse to change color', 30, 30);
      
    // Fill color according to mouseClicked() 
    fill(valueX, 255-valueY, 255-valueX);
      
    // Draw ellipse  
    ellipse(mouseX, mouseY, 115, 115);
}
  
function mouseClicked() {
    valueX = mouseX%255;
    valueY = mouseY%255;
}

输出:



范例2:


let valueX;
let valueY;
  
function setup() {
      
    // Create Canvas
    createCanvas(500, 500);
}
   
function draw() {
      
    // Set background color
    background(200); 
      
    fill('green');
      
    // Set font size
    textSize(25);
      
    text('Click mouse to change color', 30, 30);
      
    // Fill color according to mouseMoved() 
    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 mouseReleased() {
    valueX = mouseX%255;
    valueY = mouseY%255;
}

输出:

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




相关用法


注:本文由纯净天空筛选整理自sarthak_ishu11大神的英文原创作品 p5.js mouseClicked() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。