xxxxxxxxxx
75
let t = 0;
let t2 = 0;
let t3 = 0;
let x = 0;
let y =0;
let k = 0;
let stars = [];
let numStars = 100;
function setup() {
createCanvas(400, 300);
strokeWeight(2);
// Draw sky gradient
for(let y = 0; y < height; y++){
let r = map(y, 0, height/2, 200, 255);
let g = map(y, 0, height/2, 100, 150);
let b = map(y, 0, height/2, 150, 200);
stroke(r, g, b);
line(0, y, width, y);
}
generateStars();
}
function generateStars(){
stars = [];
for (let i = 0; i < numStars; i++){
let s = {
x: random(width),
y: random(height/2), // Place stars in the upper half (sky area)
size: random(1, 3),
};
stars.push(s);
}
}
function draw() {
// Draw stars
noStroke();
for (let i = 0; i < stars.length; i++) {
let s = stars[i];
let alpha = map(noise(k+100), 0, 1, 0, 255); // Twinkling effect
fill(255, 255, 255, alpha);
ellipse(s.x, s.y, s.size);
}
// Mountains
strokeWeight(2);
// First mountain
let mountainColor1 = map(noise(t), 0, 1, 50, 150);
stroke(150, mountainColor1, 200);
line(x, noise(t) * height / 3.5 + height / 2 - 30, x, height);
// Second mountain
let mountainColor2 = map(noise(t2), 0, 1, 70, 170);
stroke(120, mountainColor2, 180);
line(x, noise(t2) * height / 2 + height / 2 - 10, x, height);
// Third mountain
let mountainColor3 = map(noise(t3), 0, 1, 90, 190);
stroke(90, mountainColor3, 160);
line(x, noise(t3) * height/3 + height / 2 + 10, x, height);
x++;
y=y+2;
k = k + 0.01;
t = t + 0.02;
t2 = t2 + 0.015;
t3 = t3 + 0.3;
}