xxxxxxxxxx
114
let font;
// Array to hold information about each worm
let worms = [];
function preload() {
font = loadFont('/EBGaramond-Regular.ttf');
}
function setup() {
pinkColor = color(248, 86, 114);
blueColor = color('#373CF7');
yellowColor = color(250,250,124);
redColor = color('#2B056F')
createCanvas(600, 600);
frameRate(20);
background('rgb(255,255,247)');
//textFont(font);
textSize(50);
textAlign(CENTER, CENTER);
// Initialize worms
for (let i = 0; i < 20; i++) {
worms.push(new Worm());
}
}
function draw() {
//background(220);
for (let worm of worms) {
worm.update(worms); // Pass all worms to allow for repulsion
worm.display();
}
}
// Worm class
class Worm {
constructor() {
this.x = random(50, 550); // Initial x position
this.y = random(550, 550); // Initial y position
this.speed = random(1, 3); // Speed of movement
this.text = random(["量子詩第1625番",
"2024年3月25日東京。毎日新聞朝刊より最高予想気温最低予想気温。本日13度1",
"0度。26日14度7度。27日16度8度",
"。28日16度7度。純粋詩2行を書く。H",
"P更新。2024年3月26日東京。毎日新",
"聞朝刊より最高予想気温最低予想気温。本日",
"10度9度。27日17度7度。28日16",
"度7度。29日22度11度。純粋詩1行を",
"書く。2024年3月27日東京。毎日新聞",
"朝刊より最高予想気温最低予想気温。本日1",
"5度6度。28日16度7度。29日21度",
"11度。30日24度11度。純粋詩6行を",
"書く。2024年3月28日東京。毎日新聞",
"朝刊より最高予想気温最低予想気温。本日1",
"5度6度。29日21度11度。30日23",
"度11度。31日24度11度。純粋詩6行",
"を書く。2024年3月29日東京。毎日新",
"聞朝刊より最高予想気温最低予想気温。本日",
"22度11度。30日23度13度。31日",
"23度11度。1日20度10度。純粋詩6"]); // Random text
this.angle = random(-PI, PI); // Initial angle of movement
this.verticalSpeed = random(-4, 4); // Random vertical speed
}
update(allWorms) {
// Move the worm
this.x += cos(this.angle) * this.speed;
this.y += this.verticalSpeed;
// Bounce off walls
if (this.x < 0 || this.x > width) {
this.angle = PI - this.angle;
}
if (this.y < 0 || this.y > height) {
this.verticalSpeed *= -1;
}
// Repulsion between worms
for (let otherWorm of allWorms) {
if (otherWorm !== this) {
let dx = otherWorm.x - this.x;
let dy = otherWorm.y - this.y;
let distance = sqrt(dx * dx + dy * dy);
if (distance < 10) { // If worms are too close
let angleBetween = atan2(dy, dx);
let pushForce = 1 / distance;
// Apply force to move away from other worm
this.x -= cos(angleBetween) * pushForce;
this.y -= sin(angleBetween) * pushForce;
}
}
}
}
display() {
let lerpedColor = lerpColor(pinkColor, blueColor, frameCount / 1000); // Adjust the divisor to control the speed of transition
let lerpedColor2 = lerpColor(yellowColor, redColor, frameCount / 1500); // Adjust the divisor to control the speed of transition
//fill('rgb(250,250,124)')
fill(lerpedColor2);
stroke(lerpedColor);
strokeWeight(2);
text(this.text, this.x, this.y);
}
}