From 8f962cc6e9d9714b6f70e9b6eb1090f0cb9d5d57 Mon Sep 17 00:00:00 2001 From: CodeDevMLH Date: Sat, 1 Feb 2025 03:31:47 +0100 Subject: [PATCH] fix santa height --- seasonals/santa.js | 102 ++++++++++++++++++++++++++------------------- 1 file changed, 60 insertions(+), 42 deletions(-) diff --git a/seasonals/santa.js b/seasonals/santa.js index 488ea36..8be9a39 100644 --- a/seasonals/santa.js +++ b/seasonals/santa.js @@ -39,8 +39,6 @@ function toggleSnowfall() { initializeCanvas(); snowflakes = createSnowflakes(santaContainer); animateAll(); - } else { - console.warn('could not initialize santa: animation frame is already running'); } if (msgPrinted) { @@ -201,54 +199,74 @@ function dropPresent(santa, fromLeft) { }); } +function reloadSantaGif() { + const santa = document.querySelector('.santa'); + const src = santa.src; + santa.src = ''; + santa.src = src; +} + function animateSanta() { const santa = document.querySelector('.santa'); - const screenWidth = window.innerWidth; - const screenHeight = window.innerHeight; - const fromLeft = Math.random() < 0.5; - const startX = fromLeft ? -220 : screenWidth + 220; - const endX = fromLeft ? screenWidth + 220 : -220; - const santaHeight = santa.offsetHeight; - const startY = Math.random() * (screenHeight / 5 - santaHeight - 50) + 50; // Restrict to upper screen - const endY = Math.random() * (screenHeight / 5 - santaHeight - 50) + 50; // Restrict to upper screen - const angle = Math.random() * 20 - 10; // -10 to 10 degrees - - santa.style.left = `${startX}px`; - santa.style.top = `${startY}px`; - santa.style.transform = `rotate(${angle}deg) ${fromLeft ? 'scaleX(-1)' : 'scaleX(1)'}`; // Mirror if not from left - - let duration; - if (isMobile) { - duration = santaSpeedMobile * 1000; - } else { - duration = santaSpeed * 1000; - } - const deltaX = endX - startX; - const deltaY = endY - startY; - const startTime = performance.now(); - - function move() { - const currentTime = performance.now(); - const elapsed = currentTime - startTime; - const progress = Math.min(elapsed / duration, 1); - - const currentY = startY + deltaY * progress - 50 * Math.sin(progress * Math.PI); - santa.style.left = `${startX + deltaX * progress}px`; - santa.style.top = `${currentY}px`; - - if (Math.random() < 0.05) { // 5% chance to drop a present - dropPresent(santa, fromLeft); + + function startAnimation() { + const santaHeight = santa.offsetHeight; + if (santaHeight === 0) { + setTimeout(startAnimation, 100); + return; } + // console.log('Santa height: ', santaHeight); - if (progress < 1) { - animationFrameIdSanta = requestAnimationFrame(move); + const screenWidth = window.innerWidth; + const screenHeight = window.innerHeight; + const fromLeft = Math.random() < 0.5; + const startX = fromLeft ? -220 : screenWidth + 220; + const endX = fromLeft ? screenWidth + 220 : -220; + const startY = Math.random() * (screenHeight / 5) + santaHeight; // Restrict to upper screen + const endY = Math.random() * (screenHeight / 5) + santaHeight; // Restrict to upper screen + const angle = Math.random() * 16 - 8; // -8 to 8 degrees + + santa.style.left = `${startX}px`; + santa.style.top = `${startY}px`; + santa.style.transform = `rotate(${angle}deg) ${fromLeft ? 'scaleX(-1)' : 'scaleX(1)'}`; // Mirror if not from left + + let duration; + if (isMobile) { + duration = santaSpeedMobile * 1000; } else { - const pause = Math.random() * ((maxSantaRestTime - minSantaRestTime) * 1000) + minSantaRestTime * 1000; - setTimeout(animateSanta, pause); + duration = santaSpeed * 1000; } + const deltaX = endX - startX; + const deltaY = endY - startY; + const startTime = performance.now(); + + function move() { + const currentTime = performance.now(); + const elapsed = currentTime - startTime; + const progress = Math.min(elapsed / duration, 1); + + const currentY = startY + deltaY * progress - 50 * Math.sin(progress * Math.PI); + santa.style.left = `${startX + deltaX * progress}px`; + santa.style.top = `${currentY}px`; + + if (Math.random() < 0.05) { // 5% chance to drop a present + dropPresent(santa, fromLeft); + } + + if (progress < 1) { + animationFrameIdSanta = requestAnimationFrame(move); + } else { + const pause = Math.random() * ((maxSantaRestTime - minSantaRestTime) * 1000) + minSantaRestTime * 1000; + setTimeout(animateSanta, pause); + } + } + + animationFrameIdSanta = requestAnimationFrame(move); } - animationFrameIdSanta = requestAnimationFrame(move); + reloadSantaGif(); + + startAnimation(); }