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


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

p5.j​​s中的noCanvas()函數用於刪除由p5.js創建的默認畫布。可用於不需要畫布的草圖。它不接受任何參數。

用法:

noCanvas()

參數:此函數不接受任何參數。

以下程序說明了p5.js中的noCanvas()函數:

例:



function setup() { 
  createCanvas(400, 300); 
  
  // Create buttons for creating 
  // and removing the canvas 
  createBtn = createButton("Create Canvas"); 
  createBtn.position(30, 20); 
  createBtn.mouseClicked(createDrawArea); 
  
  removeBtn = createButton("Remove Canvas"); 
  removeBtn.position(30, 50); 
  removeBtn.mouseClicked(removeDrawArea); 
} 
  
function removeDrawArea() { 
    
  // Wrap noCanvas() in a try-catch 
  // to prevent error in case there 
  // exists no canvas to remove 
  try { 
    noCanvas(); 
  } catch (e) { 
    print("No canvas found to remove"); 
    print(e); 
  } 
} 
  
function createDrawArea() { 
  
  // Create a canvas with the 
  // given dimensions 
  createCanvas(400, 300); 
} 
  
function draw() { 
  clear(); 
  background("green"); 
  textSize(20); 
  
  text("This is the canvas area", 50, 130); 
  text("Canvas height:" + height, 50, 150); 
  text("Canvas width:" + width, 50, 170); 
}

輸出:

noCanvas-btns

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

相關用法


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