當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js createCheckbox()用法及代碼示例

p5.j​​s中的createCheckbox()函數用於在DOM(文檔對象模型)中創建複選框元素。此函數包括p5.dom庫。在頭部添加以下語法。

<script src= 
"https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.11/addons/p5.dom.min.js">  
</script>

用法:

createCheckbox(label, value)

參數:


  • label:此參數保留複選框旁邊顯示的標簽。
  • value:此參數保存複選框的狀態(是/否)。

例:本示例使用複選框將背景顏色從淺更改為暗,反之亦然。

// Create a variable for checkbox object 
var checkbox; 
  
// Create a function to change the background-color 
function change_bg() { 
    // Set dark color if box is checked 
    if (this.checked()) { 
        background("darkgreen"); 
    } 
    // Set light color if box is unckecked 
    else { 
        background("lightgreen"); 
    } 
} 
  
function setup() { 
    // Create a canvas 
    createCanvas(400, 400); 
    // Set the background-color 
    background("lightgreen"); 
    // Create a checkbox object  
    // Initially unchecked 
    checkbox = createCheckbox('Dark Background', false); 
    // Posotion the checkbox object 
    checkbox.position(160, 200); 
    // Call the change_bg() function when the box 
    // is cheched or unchecked 
    checkbox.changed(change_bg); 
}

輸出:
選中複選框之前:

咀嚼盒子之後:

參考:https://p5js.org/reference/#/p5/createCheckbox



相關用法


注:本文由純淨天空篩選整理自SujanDutta大神的英文原創作品 p5.js | createCheckbox() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。