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'); 
  
} 输出:

相关用法
- JQuery position()用法及代码示例
 - CSS background-position用法及代码示例
 - CSS object-position用法及代码示例
 - CSS list-style-position用法及代码示例
 - HTML Style position用法及代码示例
 - HTML Geolocation position用法及代码示例
 - jQuery position()和offset()的区别用法及代码示例
 - HTML Progress position用法及代码示例
 
注:本文由纯净天空筛选整理自jit_t大神的英文原创作品 p5.js | position() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
