xxxxxxxxxx
81
let poemLines = []; // Array to store poem lines
let currentLineIndex = 0; // Index of the currently displayed line
let customFont; // Variable to store the loaded font
let butterflyImage; // Variable to store the butterfly image
let butterflyX, butterflyY; // Butterfly position
let butterflySize = 100; // Butterfly size
let butterflyVelocityX, butterflyVelocityY; // Butterfly velocity
let butterflyAngle = 0; // Angle for butterfly rotation
let wingFlapSpeed = 0.05; // Speed of wing flapping
let grassImage; // Variable to store the grass image
function preload() {
// Load the CSV file with the poem lines
poemLines = loadStrings('poem.csv');
customFont = loadFont('font.ttf');
// Load the butterfly image
butterflyImage = loadImage('butterfly.png');
// Load the grass image
grassImage = loadImage('grassimage.png');
}
function setup() {
createCanvas(900, 400);
textAlign(CENTER, CENTER);
textSize(50);
textFont(customFont);
// Initialize butterfly position and velocity
butterflyX = random(width);
butterflyY = random(height);
butterflyVelocityX = random(-1, 1); // Adjust horizontal velocity
butterflyVelocityY = random(-1, 1); // Adjust vertical velocity
}
function draw() {
// Display the grass image as the background
image(grassImage, 0, 0, width, height);
// Display the current poem line
text(poemLines[currentLineIndex], width / 2, height / 2);
// Calculate the angle for butterfly rotation based on velocity
butterflyAngle = atan2(butterflyVelocityY, butterflyVelocityX) + PI / 2;
// Apply the angle and display the butterfly
push();
translate(butterflyX, butterflyY);
rotate(butterflyAngle);
imageMode(CENTER);
image(butterflyImage, 0, 0, butterflySize, butterflySize);
pop();
// Update butterfly position
butterflyX += butterflyVelocityX;
butterflyY += butterflyVelocityY;
// Wrap the butterfly around the canvas edges
if (butterflyX > width) {
butterflyX = 0;
} else if (butterflyX < 0) {
butterflyX = width;
}
if (butterflyY > height) {
butterflyY = 0;
} else if (butterflyY < 0) {
butterflyY = height;
}
// Add a flapping wing animation
butterflySize = 100 + sin(frameCount * wingFlapSpeed) * 10; // Adjust wing flap speed
}
function mousePressed() {
// Display the next poem line when the mouse is pressed
currentLineIndex = (currentLineIndex + 1) % poemLines.length;
clear();
// Display the current poem line
text(poemLines[currentLineIndex], width / 2, height / 2);
}