xxxxxxxxxx
let circleSun;
let velocity;
let tempX;
let tempY;
let img;
let img2;
let img3;
let snowflakes = [];
function preload() {
img = loadImage("Autumn.png");
img2 = loadImage("Spring.png");
img3 = loadImage("Summer.png");
}
function setup() {
createCanvas(400, 400);
background(204, 206, 236);
noStroke();
circleSunX = 240;
circleSunY = 170;
circleX = 240;
circleY = 470;
circleSunR = 70;
circleR = circleY - circleSunY;
position = createVector(250, 30);
velocity = createVector(3, 7);
image(img, 0, 0);
slider = createSlider(1, 4, 0, 1);
slider.position(width / 2, height + 30);
}
function draw() {
if (circleSunY < height) {
background(204, 206, 236);
} else {
background(0);
}
fill(255, 67, 67);
circle(circleSunX, circleSunY, circleSunR);
tempX = circleSunX - circleX;
tempY = circleSunY - circleY;
circleSunX = circleR * cos(atan2(tempY, tempX) + PI / 180) + circleX;
circleSunY = circleR * sin(atan2(tempY, tempX) + PI / 180) + circleY;
fill(63, 109, 195);
quad(180, 180, 220, 180, 400, 400, 0, 400);
fill(255, 255, 255);
quad(180, 180, 220, 180, 260, 230, 140, 230);
let t = frameCount / 60; // update time
// create a random number of snowflakes each frame
for (var i = 0; i < random(5); i++) {
snowflakes.push(new snowflake()); // append snowflake object
}
if (mouseIsPressed){
snowflakes = [];}
// loop through snowflakes with a for..of loop
if (slider.value() == 1) {
for (let flake of snowflakes) {
flake.update(t); // update snowflake position
flake.display(); // draw snowflake
}
} else if (slider.value() == 2) {
for (let flake of snowflakes) {
flake.update(t); // update snowflake position
flake.display2(); // draw snowflake
}
} else if (slider.value() == 3) {
for (let flake of snowflakes) {
flake.update(t); // update snowflake position
flake.display3(); // draw snowflake
}
} else if (slider.value() == 4) {
for (let flake of snowflakes) {
flake.update(t); // update snowflake position
flake.display4(); // draw snowflake
}
}
}
class snowflake {
constructor() {
this.posX = 0;
this.posY = random(-50, 0);
this.initialangle = random(0, 2 * PI);
this.size = random(5, 15);
// radius of snowflake spiral
// chosen so the snowflakes are uniformly spread out in area
this.radius = sqrt(random(pow(width / 2, 2)));
}
update(time) {
// x position follows a circle
let w = 0.6; // angular speed
let angle = w * time + this.initialangle;
this.posX = width / 2 + this.radius * sin(angle);
// different size snowflakes fall at slightly different y speeds
this.posY += pow(this.size, 0.5);
// delete snowflake if past end of screen
if (this.posY > height) {
let index = snowflakes.indexOf(this);
snowflakes.splice(index, 1);
}
}
display() {
image(img2, this.posX, this.posY, this.size, this.size);
}
display3() {
image(img, this.posX, this.posY, this.size, this.size);
}
display2() {
image(img3, this.posX, this.posY, this.size*1.5, this.size*1.5);
}
display4() {
ellipse(this.posX, this.posY, this.size/2);
}
}