p5.js | alpha()函数
p5.js 中的alpha()函数用于从颜色或像素数组中提取 alpha 值。
句法:
alpha(c)
参数:该函数接受上面提到的单个参数,如下所述:
下面的程序说明了 p5.js 中的alpha()函数:
示例 1:
function setup() {
//create Canvas of size 300*80
createCanvas(300, 80);
}
function draw() {
background(220);
//initialize the parameter
let c = color(0, 126, 255, 102);
//extract the alpha value
let y = alpha(c);
textSize(16);
fill(color('red'));
text("Alpha Value is : " + y, 50, 30);
}
输出:
示例 2:
function setup() {
//create Canvas of size 300*80
createCanvas(160, 180);
}
function draw() {
background(220);
//initialize the parameter
let c = color(0, 126, 255, 34);
// Sets 'value' to 34
let value = alpha(c);
fill(value);
rect(50, 15, 35, 70);
text("Value of alpha is : " + value, 22, 110);
}
输出:
参考: https://p5js.org/reference/#/p5/alpha