Создание анимации падающей звезды: примеры кода и методы

Вот пример кода анимации падающей звезды с использованием JavaScript и CSS:

HTML:

<div class="falling-star"></div>

CSS:

.falling-star {
  position: absolute;
  top: 0;
  left: 50%;
  width: 20px;
  height: 20px;
  background-image: url('star.png'); /* Replace 'star.png' with your star image */
  background-size: cover;
  animation: falling-star-animation 3s linear infinite;
}
@keyframes falling-star-animation {
  0% {
    transform: translate(-50%, -100%);
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    transform: translate(-50%, 100vh);
    opacity: 0;
  }
}

Этот код создаст анимацию падающей звезды, при которой изображение звезды падает с верхней части страницы вниз. Вам нужно будет заменить 'star.png'фактическим путем к изображению звезды.

Вот несколько дополнительных методов создания анимации падающей звезды:

  1. Анимация SVG. Вы можете создать анимацию падающей звезды, используя анимацию SVG и CSS. Вот пример:
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
  <path d="M10 10 L50 90 L90 10" stroke="yellow" fill="none">
    <animate attributeName="d" dur="2s" repeatCount="indefinite"
      values="M10 10 L50 90 L90 10; M20 20 L50 90 L80 20; M30 30 L50 90 L70 30; M40 40 L50 90 L60 40; M50 50 L50 90 L50 50; M60 60 L50 90 L40 60; M70 70 L50 90 L30 70; M80 80 L50 90 L20 80; M90 90 L50 90 L10 90;"/>
  </path>
</svg>
  1. Библиотеки анимации CSS. Вы можете использовать библиотеки анимации CSS, такие как Animate.css или Magic Animations, для создания анимации падающей звезды с помощью предварительно определенных классов CSS и анимаций. Вот пример использования Animate.css:
<div class="falling-star animate__animated animate__fadeInDown"></div>
  1. Анимация на холсте. Вы можете создать анимацию падающей звезды, используя элемент холста HTML5 и JavaScript. Вот простой пример:
<canvas id="falling-star-canvas"></canvas>
<script>
  const canvas = document.getElementById('falling-star-canvas');
  const ctx = canvas.getContext('2d');
  const starImage = new Image();
  starImage.src = 'star.png'; // Replace 'star.png' with your star image
  const star = {
    x: canvas.width / 2,
    y: 0,
    speed: 2,
    draw: function() {
      ctx.drawImage(starImage, this.x, this.y);
    },
    update: function() {
      this.y += this.speed;
    }
  };
  function animate() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    star.update();
    star.draw();
    requestAnimationFrame(animate);
  }
  starImage.onload = function() {
    animate();
  };
</script>