xxxxxxxxxx
32
let angle = 0; // Initial angle for harmonic motion
let amplitude = 100; // Amplitude of the motion
let frequency = 1; // Frequency of the sine wave
let phaseOffset = 0; // Phase offset of the sine wave
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
// Calculate the vertical position based on harmonic motion
let y = height / 2 + amplitude * sin(TWO_PI * frequency * frameCount / 60 + phaseOffset);
// Draw a line from the center to the circle's position
stroke(0);
line(width / 2, height / 2, width / 2, y);
// Draw an oscillating object
drawOscillatingObject(y);
// Increment the angle to simulate motion
angle += 0.02;
}
function drawOscillatingObject(y) {
// draw a circle at y-coordinate
fill(0);
ellipse(width / 2, y, 20, 20); // map the y-coordinate
}