diff --git a/add_to_index_html.html b/add_to_index_html.html new file mode 100644 index 0000000..b71bf10 --- /dev/null +++ b/add_to_index_html.html @@ -0,0 +1,3 @@ +
+ + \ No newline at end of file diff --git a/snowfall.css b/snowfall.css new file mode 100644 index 0000000..8e8f25c --- /dev/null +++ b/snowfall.css @@ -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; */ +} \ No newline at end of file diff --git a/snowfall.js b/snowfall.js index 8f9f42f..12b0c09 100644 --- a/snowfall.js +++ b/snowfall.js @@ -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'); - const snowflakeSymbols = ['❅', '❆']; // some snowflake symbols - const snowflakeSymbolsMobile = ['❅', '❆', '❄']; // some snowflake symbols mobile version +function createSnowflakes() { + const container = document.getElementById('snowfall'); + const windowWidth = window.innerWidth; + const windowHeight = window.innerHeight; - 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); } -}); \ No newline at end of file + + fall(); +} + +// initialize snowfall after the DOM is loaded +document.addEventListener('DOMContentLoaded', () => { + if (!snowfall) return; // exit if snowfall is disabled + createSnowflakes(); +}); diff --git a/test-site.html b/test-site.html new file mode 100644 index 0000000..bf7984b --- /dev/null +++ b/test-site.html @@ -0,0 +1,22 @@ + + + + + +