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


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

p5.j​​s中的shorten()函數用於將給定數組的元素數量減少一。

用法:

shorten(Array)

參數:此函數接受參數Array,其元素將縮短一。


返回值:它返回縮短的數組。

以下示例程序旨在說明p5.js中的shorten()函數。
示例1:

function setup() { 
  
    // Creating Canvas size 
    createCanvas(500, 90); 
} 
  
function draw() { 
  
    // Set the background color  
    background(220); 
  
    // Initializing the arrays 
    let Array1 = ['IT', 'CSE', 'ECE']; 
  
    // Calling to shorten() function. 
    let Array2 = shorten(Array1); 
  
    // Set the size of text  
    textSize(16); 
  
    // Set the text color  
    fill(color('red')); 
  
    // Getting new shortened array 
    text("Shortened array is : " + Array2, 50, 30); 
  
}

輸出:

示例2:

function setup() { 
  
    // Creating Canvas size 
    createCanvas(500, 90); 
} 
  
function draw() { 
  
    // Set the background color  
    background(220); 
  
    // Calling to shorten() function on an array 
    // Taken as the parameter 
    let Array = shorten(['Ram', 'Shayam', 'Geeta', 'Anita']); 
  
    // Set the size of text  
    textSize(16); 
  
    // Set the text color  
    fill(color('red')); 
  
    // Getting new shortened array 
    text("Shortened array is : " + Array, 50, 30); 
  
}

輸出:

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



相關用法


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