Skip to content

Commit

Permalink
Merge pull request #1 from Martin-Pitt/cat-rain-web-animation-api-patch
Browse files Browse the repository at this point in the history
Cat Rain w/ Web Animation API
  • Loading branch information
Martin-Pitt authored Dec 23, 2024
2 parents 00638b5 + 4b7cc7b commit b345f75
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,19 @@ function createCat() {
cat.style.top = '-50px';

document.body.appendChild(cat);

// Animate falling
let posY = -50;
let rotation = Math.random() * 360;
let speedY = 1 + Math.random() * 2;
let speedRotation = -2 + Math.random() * 4;

function animate() {
posY += speedY;
rotation += speedRotation;
cat.style.top = posY + 'px';
cat.style.transform = `rotate(${rotation}deg)`;

// Remove when off screen
if (posY > window.innerHeight) {
cat.remove();
} else {
requestAnimationFrame(animate);
}
}

animate();
let distance = window.innerHeight;

cat.animate([
{ top: '-50px', rotate: `${rotation}deg` },
{ top: `${distance}px`, rotate: `${rotation * speedRotation * 4}deg` },
], {
duration: (distance / speedY * 8)
}).finished.then(() => cat.remove());
}

// Create new cats periodically
Expand Down

0 comments on commit b345f75

Please sign in to comment.