fix wrong variable declaration

This commit is contained in:
MLH
2025-01-14 02:01:15 +01:00
parent c286e7a529
commit b95c0bc78a
5 changed files with 111 additions and 55 deletions

View File

@@ -1,5 +1,5 @@
const snowfall = true; // enable/disable snowfall const snowfall = true; // enable/disable snowfall
const snowflakesCount = 500; // count of snowflakes (recommended values: 300-600) let snowflakesCount = 500; // count of snowflakes (recommended values: 300-600)
const snowflakesCountMobile = 250; // count of snowflakes on mobile devices const snowflakesCountMobile = 250; // count of snowflakes on mobile devices
const snowFallSpeed = 3; // speed of snowfall (recommended values: 0-5) const snowFallSpeed = 3; // speed of snowfall (recommended values: 0-5)
@@ -127,6 +127,7 @@ function initializeSnowfall() {
if (container) { if (container) {
const screenWidth = window.innerWidth; // get the screen width to detect mobile devices const screenWidth = window.innerWidth; // get the screen width to detect mobile devices
if (screenWidth < 768) { // lower count of snowflakes on mobile devices if (screenWidth < 768) { // lower count of snowflakes on mobile devices
console.log('Mobile device detected. Reducing snowflakes count.');
snowflakesCount = snowflakesCountMobile; snowflakesCount = snowflakesCountMobile;
} }

View File

@@ -1,5 +1,5 @@
const snowstorm = true; // enable/disable snowstorm const snowstorm = true; // enable/disable snowstorm
const snowflakesCount = 500; // count of snowflakes (recommended values: 300-600) let snowflakesCount = 500; // count of snowflakes (recommended values: 300-600)
const snowflakesCountMobile = 250; // count of snowflakes on mobile devices const snowflakesCountMobile = 250; // count of snowflakes on mobile devices
const snowFallSpeed = 6; // speed of snowfall (recommended values: 4-8) const snowFallSpeed = 6; // speed of snowfall (recommended values: 4-8)
const horizontalWind = 4; // horizontal wind speed (recommended value: 4) const horizontalWind = 4; // horizontal wind speed (recommended value: 4)
@@ -130,6 +130,7 @@ function initializeSnowstorm() {
if (container) { if (container) {
const screenWidth = window.innerWidth; // get the screen width to detect mobile devices const screenWidth = window.innerWidth; // get the screen width to detect mobile devices
if (screenWidth < 768) { // lower count of snowflakes on mobile devices if (screenWidth < 768) { // lower count of snowflakes on mobile devices
console.log('Mobile device detected. Reducing snowflakes count.');
snowflakesCount = snowflakesCountMobile; snowflakesCount = snowflakesCountMobile;
} }

View File

@@ -1,5 +1,5 @@
const snowfall = true; // enable/disable snowfall const snowfall = true; // enable/disable snowfall
const snowflakesCount = 500; // count of snowflakes (recommended values: 300-600) let snowflakesCount = 500; // count of snowflakes (recommended values: 300-600)
const snowflakesCountMobile = 250; // count of snowflakes on mobile devices const snowflakesCountMobile = 250; // count of snowflakes on mobile devices
const snowFallSpeed = 3; // speed of snowfall (recommended values: 0-5) const snowFallSpeed = 3; // speed of snowfall (recommended values: 0-5)
@@ -127,6 +127,7 @@ document.addEventListener('DOMContentLoaded', () => {
if (container) { if (container) {
const screenWidth = window.innerWidth; // get the screen width to detect mobile devices const screenWidth = window.innerWidth; // get the screen width to detect mobile devices
if (screenWidth < 768) { // lower count of snowflakes on mobile devices if (screenWidth < 768) { // lower count of snowflakes on mobile devices
console.log('Mobile device detected. Reducing snowflakes count.');
snowflakesCount = snowflakesCountMobile; snowflakesCount = snowflakesCountMobile;
} }

View File

@@ -1,7 +1,17 @@
.snowflake { .snowstorm-container {
position: fixed; position: fixed;
background-color: rgba(255, 255, 255, 0.8); width: 100%;
border-radius: 50%; height: 100vh;
background: transparent;
overflow: hidden;
pointer-events: none;
}
#snowstormCanvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none; pointer-events: none;
opacity: 0.7;
} }

View File

@@ -1,12 +1,17 @@
const snowstorm = true; // enable/disable snowstorm const snowstorm = true; // enable/disable snowstorm
const snowflakesCount = 500; // count of snowflakes (recommended values: 300-600) let snowflakesCount = 500; // count of snowflakes (recommended values: 300-600)
const snowFallSpeed = 4; // speed of snowfall (recommended values: 0-8) const snowflakesCountMobile = 250; // count of snowflakes on mobile devices
const snowFallSpeed = 6; // speed of snowfall (recommended values: 4-8)
const horizontalWind = 4; // horizontal wind speed (recommended value: 4)
const verticalVariation = 2; // vertical variation (recommended value: 2)
let msgPrinted = false; // flag to prevent multiple console messages let msgPrinted = false; // flag to prevent multiple console messages
let canvas, ctx; // canvas and context for drawing snowflakes
// function to check and control the snowstorm // function to check and control the snowstorm
function toggleSnowstorm() { function toggleSnowstorm() {
const snowstormContainer = document.querySelector('.snowstorm'); const snowstormContainer = document.querySelector('.snowstorm-container');
if (!snowstormContainer) return; if (!snowstormContainer) return;
const videoPlayer = document.querySelector('.videoPlayerContainer'); const videoPlayer = document.querySelector('.videoPlayerContainer');
@@ -41,64 +46,102 @@ observer.observe(document.body, {
}); });
function createSnowstorm() { function initializeCanvas() {
const container = document.querySelector('.snowstorm') || document.createElement("div"); const container = document.querySelector('.snowstorm-container');
if (!container) {
if (!document.querySelector('.snowstorm')) { console.error('Error: No element with class "snowfall-container" found.');
container.className = "snowstorm"; return;
container.setAttribute("aria-hidden", "true");
document.body.appendChild(container);
} }
const windowWidth = window.innerWidth; canvas = document.createElement('canvas');
const windowHeight = window.innerHeight; canvas.id = 'snowstormCanvas';
container.appendChild(canvas);
ctx = canvas.getContext('2d');
for (let i = 0; i < snowflakesCount; i++) { resizeCanvas(container);
const snowflake = document.createElement('div'); window.addEventListener('resize', () => resizeCanvas(container));
snowflake.classList.add('snowflake');
// random size
const size = Math.random() * 4 + 1;
snowflake.style.width = `${size}px`;
snowflake.style.height = `${size}px`;
// random starting position
snowflake.style.left = `${Math.random() * windowWidth}px`;
snowflake.style.top = `${Math.random() * windowHeight}px`;
container.appendChild(snowflake);
animateSnowflake(snowflake);
}
} }
function animateSnowflake(snowflake) { function resizeCanvas(container) {
// animation parameters const rect = container.getBoundingClientRect();
const fallSpeed = Math.random() * snowFallSpeed + 2; canvas.width = rect.width;
const horizontalWind = Math.random() * 4 - 2; canvas.height = rect.height;
const verticalVariation = Math.random() * 4 - 1;
function fall() {
const currentTop = parseFloat(snowflake.style.top || 0);
const currentLeft = parseFloat(snowflake.style.left || 0);
snowflake.style.top = `${currentTop + fallSpeed + verticalVariation}px`;
snowflake.style.left = `${currentLeft + horizontalWind}px`;
// if snowflake is out of the window, reset its position
if (currentTop > window.innerHeight) {
snowflake.style.top = '0px';
snowflake.style.left = `${Math.random() * window.innerWidth}px`;
} }
requestAnimationFrame(fall); function createSnowflakes(container) {
return Array.from({ length: snowflakesCount }, () => ({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
radius: Math.random() * 1.2 + 1,
fallspeed: Math.random() * snowFallSpeed + 2,
horizontal: Math.random() * horizontalWind * 2 - horizontalWind,
vertical: Math.random() * verticalVariation * 2 - verticalVariation,
}));
} }
fall(); // Initialize snowflakes
let snowflakes = [];
function drawSnowflakes() {
if (!ctx || !canvas) {
console.error('Error: Canvas or context not found.');
return;
}
ctx.clearRect(0, 0, canvas.width, canvas.height); // empty canvas
snowflakes.forEach(flake => {
ctx.beginPath();
ctx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);
ctx.fillStyle = 'white'; // color of snowflakes
ctx.fill();
});
}
function updateSnowflakes() {
snowflakes.forEach(flake => {
flake.y += flake.fallspeed + flake.vertical; // downwards movement
flake.x += flake.horizontal; // sideways movement
// reset snowflake if it reaches the bottom
if (flake.y > canvas.height) {
flake.y = 0;
flake.x = Math.random() * canvas.width; // with new random X position
}
// wrap snowflakes around the screen edges
if (flake.x > canvas.width) flake.x = 0;
if (flake.x < 0) flake.x = canvas.width;
});
}
function animateSnowfall() {
drawSnowflakes();
updateSnowflakes();
requestAnimationFrame(animateSnowfall);
}
// initialize snowfall
function initializeSnowstorm() {
if (!snowstorm) {
console.warn('Snowstorm is disabled.');
return; // exit if snowfall is disabled
}
const container = document.querySelector('.snowstorm-container');
if (container) {
const screenWidth = window.innerWidth; // get the screen width to detect mobile devices
if (screenWidth < 768) { // lower count of snowflakes on mobile devices
console.log('Mobile device detected. Reducing snowflakes count.');
snowflakesCount = snowflakesCountMobile;
}
console.log('Snowstorm enabled.');
initializeCanvas();
snowflakes = createSnowflakes(container);
animateSnowfall();
}
} }
// initialize snowstorm after the DOM is loaded // initialize snowstorm after the DOM is loaded
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
if (!snowstorm) return; // exit if snowstorm is disabled initializeSnowstorm();
createSnowstorm();
}); });