xxxxxxxxxx
// let ball
let balls = []
let colors = "fff400-ffdb00-ff7a00-ff5000-ff0000".split("-").map(a=>"#"+a)
let clr
class Ball{
constructor(p,v,a,r){
this.p=p
this.v=v
this.a=a
this.r=r
this.color = color(random(colors))
this.time = 10
}
update(){
this.p.add(this.v)
this.v.add(this.a)
// this.r -=0.05
this.time-=0.1
colorMode(HSB)
if (this.time<=0){
// this.r = 0
// this.color.setAlpha(0); // 將顏色的透明度設置為 0,讓物件消失
} else {
this.r -=0.05
let h = hue(this.color); // 获取当前颜色的色相
let s = saturation(this.color); // 获取当前颜色的饱和度
let b = map(this.time, 0,10 , 0, 100); // 根据时间调整亮度,这里使用了 0 到 100 的范围
this.color = color(h, s, b); // 使用调整后的 HSB 值创建新的颜色
}
// if (this.r<=0){
// this.r = 0
// }
}
draw(){
drawingContext.shadowColor = color(0)
noStroke()
fill(this.color)
circle(this.p.x,this.p.y,this.r)
}
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
// for(let i = 0;i<50;i++){
// let newBall = new Ball(
// createVector(width/2,height/2),
// createVector(random(-5,5),random(-2,3)),
// createVector(0,0.02),
// random(10)
// )
// balls.push(newBall)
// }
}
function draw() {
background(100);
// circle(ball.p.x, ball.p.y, ball.r);
for(let obj of balls){
obj.update()
obj.draw()
}
}
function mousePressed() {
for(let i = 0;i<200;i++){
let newBall = new Ball(
createVector(mouseX+random(-10,10)*sin(random(PI)),mouseY+random(-2,2)),
createVector(random(-5,5),random(-2,3)),
createVector(0,0.02),
random(10)
)
balls.push(newBall)
}
}