xxxxxxxxxx
184
let snowflakes = []; // Array for storing snowflake objects
let ground = [];
let groundColors = []; // Array to store the color of the snow on the ground
let snowmanFilled = false; // Flag to detect when the snowman is full
const minSpeed = 5; // Increased minimum speed for faster falling snow
const maxSpeed = 15; // Increased maximum speed for faster falling snow
// Snow-themed colors for the snowflakes: shades of white, light blue, and light gray
const snowThemeColors = [
[240, 240, 255], // Very light blue
[220, 220, 230], // Light gray
[255, 255, 255], // Pure white
];
class Snowflake {
constructor(x, y, speed, color) {
this.x = x;
this.y = y;
this.speed = speed;
this.color = color; // Store the color of the snowflake
}
// Method to update the position of the snowflake
fall() {
this.y += this.speed;
// Check if the snowman is full before falling on the ground
if (!snowmanFilled && snowman.contains(this.x, this.y)) {
snowman.addSnow(this.x, this.y, this.color); // Add snow to the snowman outline
this.reset();
} else if (this.y >= ground[floor(this.x)]) {
ground[floor(this.x)] -= 3; // Faster snow accumulation by lowering ground faster
groundColors[floor(this.x)] = this.color; // Store the snowflake's color on the ground
this.reset();
}
}
// Method to reset snowflake position after it lands
reset() {
this.x = random(width);
this.y = 0;
}
// Method to display the snowflake
display() {
fill(this.color);
noStroke();
rect(this.x, this.y, 3, 3); // Slightly larger snowflakes for better visibility
}
}
class Snowman {
constructor() {
this.baseSize = 100;
this.middleSize = 70;
this.headSize = 50;
this.x = width / 2;
this.y = height - 50;
this.snowPoints = { base: [], middle: [], head: [] }; // Store snowflakes on the outline
}
// Method to check if a snowflake is near the snowman outline
contains(x, y) {
let distBase = dist(x, y, this.x, this.y);
let distMiddle = dist(x, y, this.x, this.y - this.baseSize / 2 - this.middleSize / 2);
let distHead = dist(x, y, this.x, this.y - this.baseSize / 2 - this.middleSize - this.headSize / 2);
return distBase < this.baseSize / 2 || distMiddle < this.middleSize / 2 || distHead < this.headSize / 2;
}
// Method to add snow to the snowman outline
addSnow(x, y, color) {
let distBase = dist(x, y, this.x, this.y);
let distMiddle = dist(x, y, this.x, this.y - this.baseSize / 2 - this.middleSize / 2);
let distHead = dist(x, y, this.x, this.y - this.baseSize / 2 - this.middleSize - this.headSize / 2);
if (distBase < this.baseSize / 2) {
this.snowPoints.base.push({ x, y, color });
} else if (distMiddle < this.middleSize / 2) {
this.snowPoints.middle.push({ x, y, color });
} else if (distHead < this.headSize / 2) {
this.snowPoints.head.push({ x, y, color });
}
}
// Method to draw the snowman and the snow on the outline
display() {
stroke(255);
noFill();
ellipse(this.x, this.y, this.baseSize); // Base of snowman
ellipse(this.x, this.y - this.baseSize / 2 - this.middleSize / 2, this.middleSize); // Middle
ellipse(this.x, this.y - this.baseSize / 2 - this.middleSize - this.headSize / 2, this.headSize); // Head
// Draw the snow on the outline
noStroke();
for (let snow of this.snowPoints.base) {
fill(snow.color);
ellipse(snow.x, snow.y, 3);
}
for (let snow of this.snowPoints.middle) {
fill(snow.color);
ellipse(snow.x, snow.y, 3);
}
for (let snow of this.snowPoints.head) {
fill(snow.color);
ellipse(snow.x, snow.y, 3);
}
}
}
let snowman;
function setup() {
createCanvas(400, 400);
noSmooth();
snowman = new Snowman();
// Initialize the snowflakes array with Snowflake objects using snow-themed colors
for (let i = 0; i < 200; i++) {
const snowColorIndex = floor(random(snowThemeColors.length)); // Pick a random snow-themed color
const snowColor = color(
snowThemeColors[snowColorIndex][0],
snowThemeColors[snowColorIndex][1],
snowThemeColors[snowColorIndex][2],
150
); // Snowflakes are semi-transparent
snowflakes.push(new Snowflake(random(width), random(height), random(minSpeed, maxSpeed), snowColor));
}
// Initialize ground array to store snow level at each x position
for (let x = 0; x < width; x++) {
ground[x] = height;
groundColors[x] = color(255); // Initial ground color is white
}
}
function draw() {
background(30, 30, 100); // Darker background color
// Loop through snowflakes array and update each snowflake
for (let snowflake of snowflakes) {
snowflake.fall();
snowflake.display();
}
// Draw the ground level with the colors of the accumulated snow
for (let x = 0; x < width; x++) {
fill(groundColors[x]);
noStroke();
rect(x, ground[x], 1, height - ground[x]);
}
// Draw the snowman outline and the snow on the outline
snowman.display();
}
function mousePressed() {
// Add a burst of snowflakes at mouse position on click
for (let i = 0; i < 20; i++) { // Create 20 snowflakes on each click
const snowColorIndex = floor(random(snowThemeColors.length)); // Pick a random snow-themed color
const snowColor = color(
snowThemeColors[snowColorIndex][0],
snowThemeColors[snowColorIndex][1],
snowThemeColors[snowColorIndex][2],
150
); // Semi-transparent snowflakes
snowflakes.push(new Snowflake(mouseX + random(-20, 20), mouseY + random(-20, 20), random(minSpeed, maxSpeed), snowColor));
}
}
function mouseDragged() {
// Create a gradient trail effect with snowflakes while dragging the mouse
const snowColorIndex = floor(random(snowThemeColors.length)); // Pick a random snow-themed color
const snowColor = color(
snowThemeColors[snowColorIndex][0],
snowThemeColors[snowColorIndex][1],
snowThemeColors[snowColorIndex][2],
150
); // Semi-transparent snowflakes
for (let i = 0; i < 10; i++) {
snowflakes.push(new Snowflake(mouseX + random(-10, 10), mouseY + random(-10, 10), random(minSpeed, maxSpeed), snowColor));
}
}