宇宙天体运转

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Solar System Animation</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
      background-color: #000000;
    }

    canvas {
      display: block;
    }

    .sun,
    .planet {
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
    }

    .orbit {
      border: 1px dashed rgba(255, 255, 255, 0.2);
    }

    .particle {
      position: absolute;
      background-color: #ffffff;
      border-radius: 50%;
    }

    .earth {
     
    }

    .saturn {

    }
  </style>
</head>

<body>
  <canvas id="canvas"></canvas>

  <script>
    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');

    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    const sun = {
      x: canvas.width / 2,
      y: canvas.height / 2,
      radius: 50,
      color: '#FFCA28',
      speed: 0.02,
      angle: 0
    };

    const planets = [
      {
        name: 'Mercury',
        radius: 10,
        distance: 100,
        speed: 0.05,
        angle: 0,
        color: '#616161'
      },
      {
        name: 'Venus',
        radius: 15,
        distance: 150,
        speed: 0.03,
        angle: 0,
        color: '#FF6F00'
      },
      {
        name: 'Earth',
        radius: 20,
        distance: 200,
        speed: 0.02,
        angle: 0,
        class: 'earth'
      },
      {
        name: 'Mars',
        radius: 18,
        distance: 250,
        speed: 0.015,
        angle: 0,
        color: '#E65100'
      },
      {
        name: 'Jupiter',
        radius: 30,
        distance: 300,
        speed: 0.01,
        angle: 0,
        color: '#FFD600'
      },
      {
        name: 'Saturn',
        radius: 25,
        distance: 350,
        speed: 0.008,
        angle: 0,
        class: 'saturn'
      },
      {
        name: 'Uranus',
        radius: 22,
        distance: 400,
        speed: 0.006,
        angle: 0,
        color: '#00BFA5'
      },
      {
        name: 'Neptune',
        radius: 21,
        distance: 450,
        speed: 0.004,
        angle: 0,
        color: '#304FFE'
      },
      {
        name: 'Pluto',
        radius: 12,
        distance: 500,
        speed: 0.003,
        angle: 0,
        color: '#9E9E9E'
      }
    ];

    const stars = [];
    const numStars = 500;

    function randomRange(min, max) {
      return Math.random() * (max - min) + min;
    }

    function createStars() {
      for (let i = 0; i < numStars; i++) {
        const star = {
          x: randomRange(0, canvas.width),
          y: randomRange(0, canvas.height),
          radius: Math.random() * 2,
          color: '#ffffff',
          depth: Math.random() * 5
        };
        stars.push(star);
      }
    }

    function drawStars() {
      for (let i = 0; i < numStars; i++) {
        const star = stars[i];
        ctx.beginPath();
        ctx.arc(star.x, star.y, star.radius, 0, Math.PI * 2);
        ctx.fillStyle = star.color;
        ctx.globalAlpha = 1 / star.depth;
        ctx.fill();
        ctx.closePath();
        ctx.globalAlpha = 1;
      }
    }

    function drawSun() {
      ctx.beginPath();
      ctx.arc(sun.x, sun.y, sun.radius, 0, Math.PI * 2);
      ctx.fillStyle = sun.color;
      ctx.shadowBlur = 20;
      ctx.shadowColor = '#FFCA28';
      ctx.fill();
      ctx.closePath();
    }

    function drawPlanet(planet) {
      const x = sun.x + Math.cos(planet.angle) * planet.distance;
      const y = sun.y + Math.sin(planet.angle) * planet.distance;

      ctx.beginPath();
      ctx.arc(x, y, planet.radius, 0, Math.PI * 2);

      if (planet.class) {
        ctx.fillStyle = '';
        ctx.strokeStyle = '';
        canvas.classList.add(planet.class);
      } else {
        ctx.fillStyle = planet.color;
        ctx.strokeStyle = planet.color;
      }

      ctx.shadowBlur = 10;
      ctx.shadowColor = planet.color;
      ctx.fill();
      ctx.closePath();
    }

    function drawOrbit(planet) {
      ctx.beginPath();
      ctx.arc(sun.x, sun.y, planet.distance, 0, Math.PI * 2);
      const gradient = ctx.createRadialGradient(
        sun.x,
        sun.y,
        planet.distance - 5,
        sun.x,
        sun.y,
        planet.distance + 5
      );
      gradient.addColorStop(0, 'rgba(255, 255, 255, 0.2)');
      gradient.addColorStop(1, 'transparent');
      ctx.strokeStyle = gradient;
      ctx.stroke();
      ctx.closePath();
    }

    function createParticles(x, y) {
      const particles = [];
      for (let i = 0; i < 30; i++) {
        const particle = {
          x: x,
          y: y,
          radius: Math.random() * 3,
          color: '#ffffff',
          speed: {
            x: Math.random() * 3 - 1.5,
            y: Math.random() * 3 - 1.5
          },
          alpha: 1,
          life: Math.random() * 30 + 10
        };
        particles.push(particle);
      }
      return particles;
    }

    function drawParticles(particles) {
      particles.forEach((particle, index) => {
        if (particle.alpha <= 0) {
          particles.splice(index, 1);
        } else {
          particle.x += particle.speed.x;
          particle.y += particle.speed.y;
          particle.alpha -= 0.01;
          particle.life--;
          
          ctx.beginPath();
          ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2);
          ctx.fillStyle = `rgba(255, 255, 255, ${particle.alpha})`;
          ctx.fill();
          ctx.closePath();
        }
      });
    }

    function update() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);

      drawStars();
      drawSun();

      planets.forEach((planet, index) => {
        planet.angle += planet.speed;
        drawOrbit(planet);
        drawPlanet(planet);

        if (index > 0) {
          const prevPlanet = planets[index - 1];
          planet.x = prevPlanet.x + Math.cos(planet.angle) * planet.distance;
          planet.y = prevPlanet.y + Math.sin(planet.angle) * planet.distance;
        } else {
          planet.x = sun.x + Math.cos(planet.angle) * planet.distance;
          planet.y = sun.y + Math.sin(planet.angle) * planet.distance;
        }
      });

      requestAnimationFrame(update);
    }

    createStars();
    update();

    window.addEventListener('resize', function () {
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;
      stars.length = 0;
      createStars();
    });

    canvas.addEventListener('click', function (event) {
      const x = event.clientX;
      const y = event.clientY;
      const particles = createParticles(x, y);
      drawParticles(particles);
    });
  </script>
</body>

</html>

结果图截图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值