xxxxxxxxxx
84
let noiseY;
let noiseSpeed = 0.01; // Initial speed of the noise
let noiseHeight = 150;
let acceleration = 0; // Acceleration starts at 0
let maxSpeed = 0.05; // Limit the maximum speed to prevent too fast waves
let moonX; // Horizontal position of the moon
let moonY; // Vertical position of the moon
function setup() {
createCanvas(windowWidth, windowHeight);
noiseY = height * 3 / 4;
moonX = width / 2; // Start moon in the center horizontally
moonY = height / 5; // Moon in the upper part of the canvas
}
function draw() {
background(0, 15);
// Draw sand at the bottom
let sand = 0;
stroke(194, 178, 128); // Sand color
strokeWeight(80); // Make it a thin line
while (sand < width) {
line(sand, height * 0.9 + 20 * noise(sand / 100, 0), sand, height);
sand += 10;
}
// Draw random stars for the background
noStroke();
fill(255);
for (let i = 0; i < 10; i++) {
let xrandom = random(width);
let yrandom = random(height / 1.8);
ellipse(xrandom, yrandom, 3, 3);
}
// Draw the moon in the sky
drawMoon();
// Update the speed by adding the acceleration
noiseSpeed += acceleration;
// Constrain the noiseSpeed to a minimum of 0 and a maximum of maxSpeed
noiseSpeed = constrain(noiseSpeed, 0, maxSpeed);
// Waves using noise
for (let j = 0; j < 10; j++) {
let offsetY = j * 100;
noFill();
stroke(0, 0, 255, 10);
strokeWeight(height / 2);
beginShape();
curveVertex(0, height / 2);
for (let i = 0; i < width; i += 50) {
// Slightly adjust wave height based on moon's horizontal position (gravity effect)
let gravitationalEffect = map(moonX, 0, width, -20, 20); // Subtle effect
let y = noise(frameCount * noiseSpeed + i + j) * noiseHeight + noiseY + offsetY + gravitationalEffect;
curveVertex(i, y);
}
curveVertex(width, height / 2);
endShape(LINES);
}
// Slow down the moon's movement, simulating gravitational influence
moonX = map(noise(frameCount * 0.00005), 0, 1, 0, width);
}
// Function to draw the moon
function drawMoon() {
fill(255, 255, 200); // Moon color
noStroke();
ellipse(moonX, moonY, 100, 100); // Draw the moon
}
// Function to increase/decrease acceleration with arrow keys
function keyPressed() {
if (keyCode === RIGHT_ARROW) {
acceleration += 0.001; // Increase acceleration
} else if (keyCode === LEFT_ARROW) {
acceleration -= 0.001; // Decrease acceleration
}
}