fix canvas hide and show

This commit is contained in:
MLH
2025-01-24 00:16:11 +01:00
parent 821e3d1636
commit 8376b0e140
4 changed files with 104 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ 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 let canvas, ctx; // canvas and context for drawing snowflakes
let animationFrameId; // ID of the animation frame
// function to check and control the snowfall // function to check and control the snowfall
function toggleSnowfall() { function toggleSnowfall() {
@@ -20,12 +21,21 @@ function toggleSnowfall() {
// hide snowfall 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) { if (videoPlayer || trailerPlayer || isDashboard || hasUserMenu) {
snowfallContainer.style.display = 'none'; // hide snowfall snowfallContainer.style.display = 'none'; // hide snowfall
removeCanvas();
if (!msgPrinted) { if (!msgPrinted) {
console.log('Snowfall hidden'); console.log('Snowfall hidden');
msgPrinted = true; msgPrinted = true;
} }
} else { } else {
snowfallContainer.style.display = 'block'; // show snowfall snowfallContainer.style.display = 'block'; // show snowfall
if (!animationFrameId) {
initializeCanvas();
snowflakes = createSnowflakes(snowfallContainer);
animateSnowfall();
} else {
console.warn('could not initialize snowfall: animation frame is already running');
}
if (msgPrinted) { if (msgPrinted) {
console.log('Snowfall visible'); console.log('Snowfall visible');
msgPrinted = false; msgPrinted = false;
@@ -60,7 +70,21 @@ function initializeCanvas() {
window.addEventListener('resize', () => resizeCanvas(container)); window.addEventListener('resize', () => resizeCanvas(container));
} }
function removeCanvas() {
const canvas = document.getElementById('snowfallCanvas');
if (canvas) {
canvas.remove();
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
console.log('Animation frame canceled');
}
console.log('Canvas removed');
}
}
function resizeCanvas(container) { function resizeCanvas(container) {
if (!canvas) return;
const rect = container.getBoundingClientRect(); const rect = container.getBoundingClientRect();
canvas.width = rect.width; canvas.width = rect.width;
canvas.height = rect.height; canvas.height = rect.height;
@@ -114,7 +138,7 @@ function updateSnowflakes() {
function animateSnowfall() { function animateSnowfall() {
drawSnowflakes(); drawSnowflakes();
updateSnowflakes(); updateSnowflakes();
requestAnimationFrame(animateSnowfall); animationFrameId = requestAnimationFrame(animateSnowfall);
} }
// initialize snowfall // initialize snowfall

View File

@@ -8,6 +8,7 @@ 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 let canvas, ctx; // canvas and context for drawing snowflakes
let animationFrameId; // ID of the animation frame
// function to check and control the snowstorm // function to check and control the snowstorm
function toggleSnowstorm() { function toggleSnowstorm() {
@@ -22,12 +23,21 @@ function toggleSnowstorm() {
// hide snowstorm if video/trailer player is active or dashboard is visible // hide snowstorm if video/trailer player is active or dashboard is visible
if (videoPlayer || trailerPlayer || isDashboard || hasUserMenu) { if (videoPlayer || trailerPlayer || isDashboard || hasUserMenu) {
snowstormContainer.style.display = 'none'; // hide snowstorm snowstormContainer.style.display = 'none'; // hide snowstorm
removeCanvas();
if (!msgPrinted) { if (!msgPrinted) {
console.log('Snowstorm hidden'); console.log('Snowstorm hidden');
msgPrinted = true; msgPrinted = true;
} }
} else { } else {
snowstormContainer.style.display = 'block'; // show snowstorm snowstormContainer.style.display = 'block'; // show snowstorm
if (!animationFrameId) {
initializeCanvas();
snowflakes = createSnowflakes(snowstormContainer);
animateSnowstorm();
} else {
console.warn('could not initialize snowfall: animation frame is already running');
}
if (msgPrinted) { if (msgPrinted) {
console.log('Snowstorm visible'); console.log('Snowstorm visible');
msgPrinted = false; msgPrinted = false;
@@ -62,7 +72,21 @@ function initializeCanvas() {
window.addEventListener('resize', () => resizeCanvas(container)); window.addEventListener('resize', () => resizeCanvas(container));
} }
function removeCanvas() {
const canvas = document.getElementById('snowstormCanvas');
if (canvas) {
canvas.remove();
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
console.log('Animation frame canceled');
}
console.log('Canvas removed');
}
}
function resizeCanvas(container) { function resizeCanvas(container) {
if (!canvas) return;
const rect = container.getBoundingClientRect(); const rect = container.getBoundingClientRect();
canvas.width = rect.width; canvas.width = rect.width;
canvas.height = rect.height; canvas.height = rect.height;
@@ -114,10 +138,10 @@ function updateSnowflakes() {
}); });
} }
function animateSnowfall() { function animateSnowstorm() {
drawSnowflakes(); drawSnowflakes();
updateSnowflakes(); updateSnowflakes();
requestAnimationFrame(animateSnowfall); animationFrameId = requestAnimationFrame(animateSnowstorm);
} }
// initialize snowfall // initialize snowfall
@@ -137,7 +161,7 @@ function initializeSnowstorm() {
console.log('Snowstorm enabled.'); console.log('Snowstorm enabled.');
initializeCanvas(); initializeCanvas();
snowflakes = createSnowflakes(container); snowflakes = createSnowflakes(container);
animateSnowfall(); animateSnowstorm();
} }
} }

View File

@@ -6,6 +6,7 @@ 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 let canvas, ctx; // canvas and context for drawing snowflakes
let animationFrameId; // ID of the animation frame
// function to check and control the snowfall // function to check and control the snowfall
function toggleSnowfall() { function toggleSnowfall() {
@@ -20,12 +21,21 @@ function toggleSnowfall() {
// hide snowfall 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) { if (videoPlayer || trailerPlayer || isDashboard || hasUserMenu) {
snowfallContainer.style.display = 'none'; // hide snowfall snowfallContainer.style.display = 'none'; // hide snowfall
removeCanvas();
if (!msgPrinted) { if (!msgPrinted) {
console.log('Snowfall hidden'); console.log('Snowfall hidden');
msgPrinted = true; msgPrinted = true;
} }
} else { } else {
snowfallContainer.style.display = 'block'; // show snowfall snowfallContainer.style.display = 'block'; // show snowfall
if (!animationFrameId) {
initializeCanvas();
snowflakes = createSnowflakes(snowfallContainer);
animateSnowfall();
} else {
console.warn('could not initialize snowfall: animation frame is already running');
}
if (msgPrinted) { if (msgPrinted) {
console.log('Snowfall visible'); console.log('Snowfall visible');
msgPrinted = false; msgPrinted = false;
@@ -60,7 +70,21 @@ function initializeCanvas() {
window.addEventListener('resize', () => resizeCanvas(container)); window.addEventListener('resize', () => resizeCanvas(container));
} }
function removeCanvas() {
const canvas = document.getElementById('snowfallCanvas');
if (canvas) {
canvas.remove();
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
console.log('Animation frame canceled');
}
console.log('Canvas removed');
}
}
function resizeCanvas(container) { function resizeCanvas(container) {
if (!canvas) return;
const rect = container.getBoundingClientRect(); const rect = container.getBoundingClientRect();
canvas.width = rect.width; canvas.width = rect.width;
canvas.height = rect.height; canvas.height = rect.height;
@@ -114,7 +138,7 @@ function updateSnowflakes() {
function animateSnowfall() { function animateSnowfall() {
drawSnowflakes(); drawSnowflakes();
updateSnowflakes(); updateSnowflakes();
requestAnimationFrame(animateSnowfall); animationFrameId = requestAnimationFrame(animateSnowfall);
} }
// initialize snowfall // initialize snowfall

View File

@@ -8,6 +8,7 @@ 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 let canvas, ctx; // canvas and context for drawing snowflakes
let animationFrameId; // ID of the animation frame
// function to check and control the snowstorm // function to check and control the snowstorm
function toggleSnowstorm() { function toggleSnowstorm() {
@@ -22,12 +23,21 @@ function toggleSnowstorm() {
// hide snowstorm if video/trailer player is active or dashboard is visible // hide snowstorm if video/trailer player is active or dashboard is visible
if (videoPlayer || trailerPlayer || isDashboard || hasUserMenu) { if (videoPlayer || trailerPlayer || isDashboard || hasUserMenu) {
snowstormContainer.style.display = 'none'; // hide snowstorm snowstormContainer.style.display = 'none'; // hide snowstorm
removeCanvas();
if (!msgPrinted) { if (!msgPrinted) {
console.log('Snowstorm hidden'); console.log('Snowstorm hidden');
msgPrinted = true; msgPrinted = true;
} }
} else { } else {
snowstormContainer.style.display = 'block'; // show snowstorm snowstormContainer.style.display = 'block'; // show snowstorm
if (!animationFrameId) {
initializeCanvas();
snowflakes = createSnowflakes(snowstormContainer);
animateSnowstorm();
} else {
console.warn('could not initialize snowfall: animation frame is already running');
}
if (msgPrinted) { if (msgPrinted) {
console.log('Snowstorm visible'); console.log('Snowstorm visible');
msgPrinted = false; msgPrinted = false;
@@ -62,7 +72,21 @@ function initializeCanvas() {
window.addEventListener('resize', () => resizeCanvas(container)); window.addEventListener('resize', () => resizeCanvas(container));
} }
function removeCanvas() {
const canvas = document.getElementById('snowstormCanvas');
if (canvas) {
canvas.remove();
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
console.log('Animation frame canceled');
}
console.log('Canvas removed');
}
}
function resizeCanvas(container) { function resizeCanvas(container) {
if (!canvas) return;
const rect = container.getBoundingClientRect(); const rect = container.getBoundingClientRect();
canvas.width = rect.width; canvas.width = rect.width;
canvas.height = rect.height; canvas.height = rect.height;
@@ -114,10 +138,10 @@ function updateSnowflakes() {
}); });
} }
function animateSnowfall() { function animateSnowstorm() {
drawSnowflakes(); drawSnowflakes();
updateSnowflakes(); updateSnowflakes();
requestAnimationFrame(animateSnowfall); animationFrameId = requestAnimationFrame(animateSnowstorm);
} }
// initialize snowfall // initialize snowfall
@@ -137,7 +161,7 @@ function initializeSnowstorm() {
console.log('Snowstorm enabled.'); console.log('Snowstorm enabled.');
initializeCanvas(); initializeCanvas();
snowflakes = createSnowflakes(container); snowflakes = createSnowflakes(container);
animateSnowfall(); animateSnowstorm();
} }
} }