p5.Color setAlpha() 方法
p5.js 中的setAlpha() 方法用于设置 p5.Color 对象的 alpha 分量。 alpha 值的范围取决于当前的颜色模式。在默认的 RGB 颜色模式下,该值可以在 0 到 255 之间。
句法:
setAlpha( alpha )
参数:此方法接受如上所述和如下所述的单个参数:
- alpha:它是一个数字,表示新的 alpha 值。
下面的例子说明了 p5.js 中的setAlpha() 方法:
示例 1:
Javascript
function setup() {
createCanvas(600, 300);
alphaSlider = createSlider(0, 255, 128);
alphaSlider.position(30, 50);
}
function draw() {
clear();
textSize(18);
// Get the alpha value from the slider
let alphaValue = alphaSlider.value();
let newColor = color(128, 255, 128);
// Set the alpha value of the color
newColor.setAlpha(alphaValue);
background(newColor);
fill('black');
text("Move the slider to change the " +
"alpha component of the background color",
20, 20);
}
Javascript
function setup() {
createCanvas(600, 300);
alphaSlider = createSlider(0, 255, 128);
alphaSlider.position(30, 50);
}
function draw() {
clear();
textSize(18);
noFill();
stroke(0);
strokeWeight(50);
ellipse(width / 2, height / 2, 150);
strokeWeight(1);
// Get the alpha value from the slider
let alphaValue = alphaSlider.value();
let newColor = color(255, 128, 128);
// Set the alpha value of the color
newColor.setAlpha(alphaValue);
noStroke();
background(newColor);
fill('black');
text(
"Move the slider to change the " +
"alpha component of the background color",
20, 20);
}
输出:
示例 2:
Javascript
function setup() {
createCanvas(600, 300);
alphaSlider = createSlider(0, 255, 128);
alphaSlider.position(30, 50);
}
function draw() {
clear();
textSize(18);
noFill();
stroke(0);
strokeWeight(50);
ellipse(width / 2, height / 2, 150);
strokeWeight(1);
// Get the alpha value from the slider
let alphaValue = alphaSlider.value();
let newColor = color(255, 128, 128);
// Set the alpha value of the color
newColor.setAlpha(alphaValue);
noStroke();
background(newColor);
fill('black');
text(
"Move the slider to change the " +
"alpha component of the background color",
20, 20);
}
输出:
在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5.Color/setAlpha