This commit is contained in:
MLH
2024-12-16 19:34:29 +01:00
parent 6e1a54daec
commit 44ba61a199
4 changed files with 89 additions and 50 deletions

3
add_to_index_html.html Normal file
View File

@ -0,0 +1,3 @@
<div class="snowfall"></div>
<script src="seasonals/snowfall.js"></script>
<link rel="stylesheet" href="seasonals/snowfall.css">

8
snowfall.css Normal file
View File

@ -0,0 +1,8 @@
.snowflake {
position: absolute;
background-color: white;
/* background-color: rgba(255, 255, 255, 0.8); */
border-radius: 50%;
pointer-events: none;
/* opacity: 0.7; */
}

View File

@ -1,39 +1,37 @@
const snowflakes = true; // enable/disable snowflakes
const randomSnowflakes = true; // enable random Snowflakes
const enableColoredSnowflakes = true; // enable colored snowflakes on mobile devices
const snowflakeCount = 25; // count of random extra snowflakes
const snowfall = true; // enable/disable snowfall
const snowflakesCount = 500; // count of snowflakes (recommended values: 300-600)
const snowFallSpeed = 3; // speed of snowfall (recommended values: 0-5)
let msgPrinted = false; // flag to prevent multiple console messages
// function to check and control the snowflakes
function toggleSnowflakes() {
const snowflakeContainer = document.querySelector('.snowflakes');
if (!snowflakeContainer) return;
// function to check and control the snowfall
function toggleSnowfall() {
const snowfallContainer = document.querySelector('.snowfall');
if (!snowfallContainer) return;
const videoPlayer = document.querySelector('.videoPlayerContainer');
const trailerPlayer = document.querySelector('.youtubePlayerContainer');
const isDashboard = document.body.classList.contains('dashboardDocument');
const hasUserMenu = document.querySelector('#app-user-menu');
// hide snowflakes if video/trailer player is active or dashboard is visible
// hide snowfall if video/trailer player is active or dashboard is visible
if (videoPlayer || trailerPlayer || isDashboard || hasUserMenu) {
snowflakeContainer.style.display = 'none'; // hide snowflakes
snowfallContainer.style.display = 'none'; // hide snowfall
if (!msgPrinted) {
console.log('Snowflakes hidden');
console.log('Snowfall hidden');
msgPrinted = true;
}
} else {
snowflakeContainer.style.display = 'block'; // show snowflakes
snowfallContainer.style.display = 'block'; // show snowfall
if (msgPrinted) {
console.log('Snowflakes visible');
console.log('Snowfall visible');
msgPrinted = false;
}
}
}
// observe changes in the DOM
const observer = new MutationObserver(toggleSnowflakes);
const observer = new MutationObserver(toggleSnowfall);
// start observation
observer.observe(document.body, {
@ -42,51 +40,59 @@ observer.observe(document.body, {
attributes: true // observe changes to attributes (e.g. class changes)
});
function addRandomSnowflakes(count) {
const snowflakeContainer = document.querySelector('.snowflakes'); // get the snowflake container
if (!snowflakeContainer) return; // exit if snowflake container is not found
console.log('Adding random snowflakes');
function createSnowflakes() {
const container = document.getElementById('snowfall');
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
const snowflakeSymbols = ['❅', '❆']; // some snowflake symbols
const snowflakeSymbolsMobile = ['❅', '❆', '❄']; // some snowflake symbols mobile version
for (let i = 0; i < count; i++) {
// create a new snowflake element
for (let i = 0; i < snowflakesCount; i++) {
const snowflake = document.createElement('div');
snowflake.classList.add('snowflake');
// pick a random snowflake symbol
if (enableColoredSnowflakes) {
snowflake.textContent = snowflakeSymbolsMobile[Math.floor(Math.random() * snowflakeSymbolsMobile.length)];
} else {
snowflake.textContent = snowflakeSymbols[Math.floor(Math.random() * snowflakeSymbols.length)];
}
// random size between 1 and 3 pixels
const size = Math.random() * 3 + 1;
snowflake.style.width = `${size}px`;
snowflake.style.height = `${size}px`;
// set random horizontal position, animation delay and size(uncomment lines to enable)
const randomLeft = Math.random() * 100; // position (0% to 100%)
//const randomSize = Math.random() * 1.5 + 0.5; // size (0.5em to 2em) //uncomment to enable random size
const randomAnimationDelay = Math.random() * 8; // delay (0s to 8s)
const randomAnimationDelay2 = Math.random() * 5; // delay (0s to 5s)
// random starting position
snowflake.style.left = `${Math.random() * windowWidth}px`;
snowflake.style.top = `${Math.random() * windowHeight}px`;
// apply styles
snowflake.style.left = `${randomLeft}%`;
//snowflake.style.fontSize = `${randomSize}em`; //uncomment to enable random size
snowflake.style.animationDelay = `${randomAnimationDelay}s, ${randomAnimationDelay2}s`;
container.appendChild(snowflake);
// add the snowflake to the container
snowflakeContainer.appendChild(snowflake);
animateSnowflake(snowflake);
}
console.log('Random snowflakes added');
}
// initialize snowflakes and add random snowflakes after the DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
if (!snowflakes) return; // exit if snowflakes are disabled
toggleSnowflakes();
function animateSnowflake(snowflake) {
// Animation Parameter
const speed = Math.random() * snowFallSpeed + 1;
// const speed = Math.random() * 3 + 1;
const sidewaysMovement = Math.random() * 2 - 1;
const screenWidth = window.innerWidth; // get the screen width to detect mobile devices
if (randomSnowflakes && screenWidth > 768) { // add random snowflakes only on larger screens
addRandomSnowflakes(snowflakeCount);
function fall() {
const currentTop = parseFloat(snowflake.style.top || 0);
const currentLeft = parseFloat(snowflake.style.left || 0);
// fall and sideways movement
snowflake.style.top = `${currentTop + speed}px`;
snowflake.style.left = `${currentLeft + sidewaysMovement}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);
}
fall();
}
// initialize snowfall after the DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
if (!snowfall) return; // exit if snowfall is disabled
createSnowflakes();
});

22
test-site.html Normal file
View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snowfall Display</title>
<link rel="stylesheet" href="snowfall.css">
<style>
body {
background-color: black;
overflow: hidden;
}
</style>
</head>
<body>
<div id="snowfall"></div>
<script src="snowfall.js"></script>
</body>
</html>