CSS+Canvas绘制最美星空(一闪一闪亮晶晶效果+流星划过)

1.效果

在这里插入图片描述

2.代码

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

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    body,
    html {
      margin: 0;
      padding: 0;
      overflow: hidden;
    }

    body {
      background: #000;
    }

    canvas {
      width: 100%;
      height: 100vh;
      display: block;
    }
  </style>
  <title>星空</title>
</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;

    class Star {
      constructor(x, y, brightness, type) {
        this.x = x;
        this.y = y;
        this.brightness = brightness;
        this.alpha = 1;
        this.fade = Math.random() * 0.05 + 0.01;
        this.type = type;
        this.size = (type === 'cross') ? 2 : 1; // 根据类型设置大小
      }

      draw() {
        ctx.beginPath();
        if (this.type === 'cross') {
          ctx.strokeStyle = `rgba(255, 255, 255, ${this.alpha})`;
          ctx.lineWidth = 1; // 设置线条粗细
          ctx.moveTo(this.x, this.y - 1 * this.size);
          ctx.lineTo(this.x, this.y + 1 * this.size);
          ctx.moveTo(this.x - 1 * this.size, this.y);
          ctx.lineTo(this.x + 1 * this.size, this.y);
          ctx.stroke();
        } else {
          ctx.fillStyle = `rgba(255, 255, 255, ${this.alpha})`;
          ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
          ctx.fill();
        }
      }

      update() {
        this.brightness += Math.random() * 0.1 - 0.05;
        this.brightness = Math.min(Math.max(this.brightness, 0), 1);

        this.alpha += this.fade;
        if (this.alpha > 1 || this.alpha < 0) {
          this.fade = -this.fade;
        }
      }
    }

    let stars = [];
    const numStars = 200;

    function createStars() {
      for (let i = 0; i < numStars; i++) {
        let type = Math.random() > 0.5 ? 'cross' : 'particle';
        stars.push(new Star(
          Math.random() * canvas.width,
          Math.random() * canvas.height,
          Math.random(),
          type
        ));
      }
    }

    function animateStars() {
      ctx.clearRect(0, 0, canvas.width, canvas.height);
      for (let i = 0; i < stars.length; i++) {
        stars[i].update();
        stars[i].draw();
      }
      requestAnimationFrame(animateStars);
    }

    let shootingStar = null;

    function createShootingStar() {
      const startX = Math.random() * canvas.width * 0.5; // 从左侧开始
      const startY = 0; // 从顶部开始
      const speed = 4 + Math.random() * 6;
      const angle = Math.PI / 6 + Math.random() * Math.PI / 3; // 修改划过的角度范围

      // 增加长度、宽度和颜色属性
      shootingStar = { x: startX, y: startY, speed, angle, length: 80 + Math.random() * 40, thickness: 1 + Math.random() * 2, color: 'rgba(255, 255, 255, 1)' };
    }

    function animateShootingStar() {
      if (!shootingStar) {
        if (Math.random() < 0.01) {
          createShootingStar();
        }
      } else {
        const { x, y, speed, angle, length, thickness, color } = shootingStar;
        ctx.save();
        ctx.translate(x, y);
        ctx.rotate(angle);

        const gradient = ctx.createLinearGradient(0, 0, length, 0);
        gradient.addColorStop(0, color);
        gradient.addColorStop(1, 'rgba(255, 255, 255, 0)');

        ctx.strokeStyle = gradient;
        ctx.lineWidth = thickness;
        ctx.beginPath();
        ctx.moveTo(0, 0);
        ctx.lineTo(length, 0);
        ctx.stroke();

        ctx.restore();

        shootingStar.x += speed * Math.cos(angle);
        shootingStar.y += speed * Math.sin(angle);

        if (shootingStar.x > canvas.width || shootingStar.y > canvas.height) {
          shootingStar = null;
        }
      }
      requestAnimationFrame(animateShootingStar);
    }

    createStars();
    animateStars();
    animateShootingStar();
  </script>
</body>

</html>

3.结语

创建一个.html文件,复制代码进去就可以看到效果啦~满屏星星很好看,有普通粒子星星,也有一闪一闪的十字星星,不定时会有流星划过,很好看的夜空,如果想要星星变大,可自行调整粒子大小

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
JS+Canvas可以用来绘制各种类型的图形,包括列表和标题栏。下面是一个简单的例子,展示如何使用Canvas绘制一个包含列表和标题栏的图形: ```html <!DOCTYPE html> <html> <head> <title>Canvas绘制列表和标题栏</title> </head> <body> <canvas id="canvas" width="400" height="400"></canvas> <script> var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); // 绘制标题栏 ctx.fillStyle = '#333'; ctx.fillRect(0, 0, canvas.width, 50); ctx.fillStyle = '#fff'; ctx.font = '20px Arial'; ctx.fillText('列表标题', 10, 30); // 绘制列表项 ctx.fillStyle = '#ccc'; ctx.fillRect(0, 50, canvas.width, 50); ctx.fillStyle = '#000'; ctx.font = '16px Arial'; ctx.fillText('列表项1', 10, 85); ctx.fillStyle = '#eee'; ctx.fillRect(0, 100, canvas.width, 50); ctx.fillText('列表项2', 10, 135); ctx.fillStyle = '#ccc'; ctx.fillRect(0, 150, canvas.width, 50); ctx.fillText('列表项3', 10, 185); </script> </body> </html> ``` 在这个例子中,我们首先获取了一个Canvas元素,然后使用getContext()方法获取了Canvas的上下文。接着,我们使用上下文来绘制一个标题栏和三个列表项。我们设置标题栏的颜色为深灰色,绘制一个填充矩形,然后在标题栏上绘制了白色的标题文字。接着,我们绘制了三个列表项,每个列表项包含一个浅灰色的填充矩形和黑色的文本。最后我们得到了一个带有标题栏和三个列表项的Canvas图形。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值