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


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

position()函數用於設置元素相對於原​​點(0,0)坐標的位置。如果此函數不包含任何參數,則它將返回元素的x和y位置。

注意:此函數需要p5.dom庫。因此,在index.html文件的開頭部分添加以下行。

<script language="javascript" 
    type="text/javascript" src="path/to/p5.dom.js"> 
</script>

用法:


position()

或者

position( x, y )

參數:

  • x:此參數相對於窗口左上角保留x-position。
  • y:此參數相對於窗口左上角保留y-position。

返回值:此函數返回一個包含元素的x和y位置的對象。

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

範例1:

function setup() { 
      
    // Create canvas of given size 
    createCanvas(400, 200); 
      
    // Set background color 
    background('green'); 
      
    // Create an input element 
    var div_cont = createDiv('Welcome to GeeksforGeeks'); 
      
    // Set the position of div element 
    div_cont.position(60, 80);  
    
    // Set font color 
    div_cont.style('color', '#ffffff');   
    
    // Set width of input field 
    div_cont.style('width', '250px'); 
    
    // Set font-size of input text 
    div_cont.style('font-size', '20px'); 
  
} 

輸出:

範例2:

function setup() { 
      
    // Create canvas of given size 
    createCanvas(400, 200); 
      
    // Set background color 
    background('green'); 
      
    // Create an input element 
    var input_val = createInput('');     
      
    // Set the attribute and its value     
    input_val.attribute('value', 'Welcome to GeeksforGeeks');   
    
    // Set the position of div element 
    input_val.position(60, 80);  
    
    // Set width of input field 
    input_val.style('width', '250px'); 
    
    // Set font-size of input text 
    input_val.style('font-size', '20px'); 
  
} 

輸出:



相關用法


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