xxxxxxxxxx
66
function preload(){
BGimg=loadImage('bg.png')
}
// Define a Swing class
class Swing {
constructor(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.angle = 0; // Start angle for swing motion
this.angleVelocity = 0.02; // Speed of swing motion
this.amplitude = 0.5; // Scale amplitude for depth effect
}
update() {
// Oscillate the scale between 0.8 and 1.2 to simulate forward and backward motion
this.swingScale = lerp(0.8, 1.2, (sin(this.angle) + 1) / 2);
this.angle += this.angleVelocity;
}
display() {
// Swing seat - scale for depth effect
push(); // Start a new drawing state
translate(this.x, this.y-40); // Translate to the position of the swing seat
scale(this.swingScale); // Apply scale transformation
fill(160, 82, 45); // Brown color for the seat
rect(0, 0, this.width, this.height); // Seat
pop(); // Restore original state
// Swing ropes - adjust stroke weight for depth effect
stroke(0); // White color for the ropes
strokeWeight(2 * this.swingScale); // Scale the stroke weight to simulate depth
line(this.x-8 - this.width / 2, 100, this.x - this.width / 2 * this.swingScale, this.y - 40 * this.swingScale); // Left rope
line(this.x+8 + this.width / 2, 100, this.x + this.width / 2 * this.swingScale, this.y - 40 * this.swingScale); // Right rope
// Draw the swing frame
stroke(139, 69, 19); // Dark brown color for the wood frame
strokeWeight(10); // Thick lines for the frame
line(this.x-100, 100, this.x-100, 300); // Left side of the frame
line(this.x+100, 100, this.x+100, 300); // Right side of the frame
line(this.x-100, 100, this.x+100, 100); // Top of the frame
}
}
// Instantiate the Swing object
let mySwing;
function setup() {
createCanvas(800, 600);
rectMode(CENTER);
mySwing = new Swing(300, 300, 100, 20);
Swing1 = new Swing(520, 300, 100, 20);// Create a new Swing object
}
function draw() {
background(135, 206, 235); // Sky color
// Update and display the swing
mySwing.update();
mySwing.display();
Swing1.update();
Swing1.display();
// No need to call noLoop() as we want the animation to keep running
}