switched to canvas
This commit is contained in:
19
snowfall.css
19
snowfall.css
@ -1,8 +1,17 @@
|
|||||||
.snowflake {
|
.snowfall-container {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
background-color: white;
|
width: 100%;
|
||||||
/* background-color: rgba(255, 255, 255, 0.8); */
|
height: 100vh;
|
||||||
border-radius: 50%;
|
background: transparent;
|
||||||
|
overflow: hidden;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
#snowfallCanvas {
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
pointer-events: none;
|
pointer-events: none;
|
||||||
/* opacity: 0.7; */
|
|
||||||
}
|
}
|
153
snowfall.js
153
snowfall.js
@ -4,9 +4,11 @@ const snowFallSpeed = 3; // speed of snowfall (recommended values: 0-5)
|
|||||||
|
|
||||||
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 snowfall
|
// function to check and control the snowfall
|
||||||
function toggleSnowfall() {
|
function toggleSnowfall() {
|
||||||
const snowfallContainer = document.querySelector('.snowfall');
|
const snowfallContainer = document.querySelector('.snowfall-container');
|
||||||
if (!snowfallContainer) return;
|
if (!snowfallContainer) return;
|
||||||
|
|
||||||
const videoPlayer = document.querySelector('.videoPlayerContainer');
|
const videoPlayer = document.querySelector('.videoPlayerContainer');
|
||||||
@ -41,66 +43,109 @@ observer.observe(document.body, {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
function createSnowflakes() {
|
function initializeCanvas() {
|
||||||
const container = document.querySelector('.snowfall') || document.createElement("div");
|
const container = document.querySelector('.snowfall-container');
|
||||||
|
if (!container) {
|
||||||
if (!document.querySelector('.snowfall')) {
|
console.error('Error: No element with class "snowfall-container" found.');
|
||||||
container.className = "snowfall";
|
return;
|
||||||
container.setAttribute("aria-hidden", "true");
|
|
||||||
document.body.appendChild(container);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
canvas = document.createElement('canvas');
|
||||||
|
canvas.id = 'snowfallCanvas';
|
||||||
|
container.appendChild(canvas);
|
||||||
|
ctx = canvas.getContext('2d');
|
||||||
|
|
||||||
const windowWidth = window.innerWidth;
|
resizeCanvas(container);
|
||||||
const windowHeight = window.innerHeight;
|
window.addEventListener('resize', () => resizeCanvas(container));
|
||||||
|
|
||||||
for (let i = 0; i < snowflakesCount; i++) {
|
|
||||||
const snowflake = document.createElement('div');
|
|
||||||
snowflake.classList.add('snowflake');
|
|
||||||
|
|
||||||
// random size between 1 and 3 pixels
|
|
||||||
const size = Math.random() * 3 + 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 Parameter
|
const rect = container.getBoundingClientRect();
|
||||||
const speed = Math.random() * snowFallSpeed + 1;
|
canvas.width = rect.width;
|
||||||
// const speed = Math.random() * 3 + 1;
|
canvas.height = rect.height;
|
||||||
const sidewaysMovement = Math.random() * 2 - 1;
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createSnowflakes(container) {
|
||||||
|
return Array.from({ length: snowflakesCount }, () => ({
|
||||||
|
x: Math.random() * canvas.width,
|
||||||
|
y: Math.random() * canvas.height,
|
||||||
|
radius: Math.random() * 1.2 + 1,
|
||||||
|
speed: Math.random() * snowFallSpeed + 1,
|
||||||
|
swing: Math.random() * 2 - 1,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.speed; // downwards movement
|
||||||
|
flake.x += flake.swing; // 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 after the DOM is loaded
|
// initialize snowfall after the DOM is loaded
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
if (!snowfall) return; // exit if snowfall is disabled
|
if (!snowfall){
|
||||||
createSnowflakes();
|
console.warn('Snowfall is disabled.');
|
||||||
|
return; // exit if snowfall is disabled
|
||||||
|
}
|
||||||
|
const container = document.querySelector('.snowfall-container');
|
||||||
|
if (container) {
|
||||||
|
initializeCanvas();
|
||||||
|
snowflakes = createSnowflakes(container);
|
||||||
|
animateSnowfall();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
|
|
||||||
|
// initialize snowfall
|
||||||
|
function initializeSnowfall() {
|
||||||
|
if (!snowfall){
|
||||||
|
console.warn('Snowfall is disabled.');
|
||||||
|
return; // exit if snowfall is disabled
|
||||||
|
}
|
||||||
|
const container = document.querySelector('.snowfall-container');
|
||||||
|
if (container) {
|
||||||
|
console.log('Snowfall enabled.');
|
||||||
|
initializeCanvas();
|
||||||
|
snowflakes = createSnowflakes(container);
|
||||||
|
animateSnowfall();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
initializeSnowfall();
|
@ -15,7 +15,7 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class="snowfall"></div>
|
<div class="snowfall-container"></div>
|
||||||
<script src="snowfall.js"></script>
|
<script src="snowfall.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user