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


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

p5.j​​s中的setRed()函數用於在RGB顏色模式下設置紅色值。它設置RGB格式的第一個值。

用法:

setRed(red)

參數:該函數接受上述和以下描述的單個參數:


  • red:此參數存儲新的紅色值。

以下程序說明了p5.js中的setRed()函數:
示例-1:這個例子使用setRed()函數用於設置RGB顏色格式的紅色值。

/* declare a variable to  
 store the value of color*/
let backgroundColor; 
  
function setup() { 
    /* initialise the variable 
     with RGB color format*/
    backgroundColor = color(0, 50, 150); 
} 
  
function draw() { 
    // Create Canvas of a given size 
  
    createCanvas(500, 500); 
  
    // Use of setRed function 
    backgroundColor.setRed( 
      0 + 128 * cos(millis() / 1000)); 
  
    /* Pass the initialized  
      variable to background function*/
    background(backgroundColor); 
  
    // Set text size 
    textSize(30); 
  
    // Set text color 
    fill("white"); 
  
    // set text 
    text("GeeksForGeeks", 125, 125); 
  
}

輸出:

示例2:本示例使用setRed()函數設置RGB顏色格式的紅色值。

/* declare a variable to 
store the value of color*/
let backgroundColor; 
  
function setup() { 
    /* initialise the variable 
    with RGB color format*/
    backgroundColor = color(0, 0, 0); 
} 
  
function draw() { 
    // Create Canvas of a given size 
  
    createCanvas(540, 500); 
  
    /* Use of setRed function  
    and initialise it to maximum*/
    backgroundColor.setRed(255); 
  
    /* Pass the initialised 
    variable to background function*/
    background(backgroundColor); 
  
    // Set text size 
    textSize(30); 
  
    // Set text color 
    fill("white"); 
  
    // set text 
    text("GeeksForGeeks\n ", 135, 125); 
    text( 
      "A Computer Science Portal For Geeks", 
      10, 155); 
  
}

輸出:

參考:https://p5js.org/reference/#/p5.Color/setRed



相關用法


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