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


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

在setup()函數之後調用draw()函數。 draw()函數用於在塊內執行代碼,直到程序停止或調用noLoop()。如果該程序在setup()函數中不包含noLoop()函數,則draw()函數在停止之前仍將執行一次。應始終通過noLoop(),redraw()和loop()函數進行控製。

用法:

draw()

以下示例說明了p5.js中的draw()函數:


示例1:

function setup() {  
      
    // Create Canvas of given size  
    createCanvas(400, 300);  
}  
  
function draw() {  
      
    background(220);  
      
    // Use color() function  
    let c = color('green');  
  
    // Use fill() function to fill color  
    fill(c);  
      
    // Draw a rectangle  
    rect(50, 50, 300, 200);  
      
} 

輸出:

示例2:

function setup() {   
     
    // Create Canvas of given size  
    var cvs = createCanvas(600, 250); 
} 
  
function draw() { 
    
    // Set the background color 
    background('green');  
    
    // Use createDiv() function to 
    // create a div element 
    var myDiv = createDiv('GeeksforGeeks'); 
    
    var myDiv1 = createDiv('A computer science portal for geeks'); 
    
    // Use child() function 
    myDiv.child(myDiv1); 
    
    // Set the position of div element 
    myDiv.position(150, 100);   
    
    myDiv.style('text-align', 'center'); 
    
    // Set the font-size of text 
    myDiv.style('font-size', '24px'); 
    
    // Set the font color 
    myDiv.style('color', 'white'); 
  
}

輸出:



相關用法


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