p5js/呼吸球.js
2022-11-27 15:35:53 +08:00

47 lines
924 B
JavaScript

const ball = {
x: 200,
y: 200,
size: 0,
speed: 1,
};
function setup() {
createCanvas(400, 400);
}
function draw() {
background(255);
strokeWeight(4);
stroke("yellow");
fill(ball.size, 0, 0);
star(ball.x, ball.y, 100, ball.size, 6);
ball.size += ball.speed;
ball.speed *= 1.1;
if (ball.size >= 200) {
ball.size = 200;
ball.speed = -1;
}
if (ball.size <= 0) {
ball.size = 0;
ball.speed = 1;
}
}
function star(x, y, radius1, radius2, npoints) {
let angle = TWO_PI / npoints;
let halfAngle = angle / 2.0;
beginShape();
for (let a = 0; a < TWO_PI; a += angle) {
let sx = x + cos(a) * radius2;
let sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a + halfAngle) * radius1;
sy = y + sin(a + halfAngle) * radius1;
vertex(sx, sy);
}
endShape(CLOSE);
}