xxxxxxxxxx
74
let hearts = [];
function setup() {
createCanvas(600, 600);
// Create 25 bouncing hearts around the screen
for (let i = 0; i < 25; i++) {
let x = random(width);
let y = random(height);
let heart = new Heart(x, y);
hearts.push(heart);
}
}
function draw() {
background(255);
// Update and display each heart
for (let heart of hearts) {
heart.update();
heart.display();
}
}
class Heart {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = 50; // Same size for all hearts
this.speedX = random(-3, 3);
this.speedY = random(-3, 3);
this.color = color(255, 0, 0);
this.strokeColor = color(0);
}
update() {
// Bounce off walls
if (this.x < 0 || this.x > width) {
this.speedX *= -1;
}
if (this.y < 0 || this.y > height) {
this.speedY *= -1;
}
// Update position
this.x += this.speedX;
this.y += this.speedY;
}
display() {
// Draw heart shape
noFill();
stroke(this.strokeColor);
strokeWeight(2);
fill(this.color);
beginShape();
for (let a = 0; a < TWO_PI; a += 0.1) {
const r = this.size / 10;
const heartX = this.x + r * 16 * pow(sin(a), 3);
const heartY = this.y - r * (13 * cos(a) - 5 * cos(2 * a) - 2 * cos(3 * a) - cos(4 * a));
vertex(heartX, heartY);
}
endShape(CLOSE);
// Write text in the middle
fill(255);
strokeWeight(2)
textSize(32);
textAlign(CENTER, CENTER);
text("will you be my valentine?", width / 2, height / 2);
}
}