星空粒子特效加代码雨加3d立方体

星空粒子特效加代码雨加3d立方体

前言

由于最近发现我自制的网页和别人的网页有亿点区别,我收scratch粒子特效的启迪,用html做了一个粒子特效

粒子特效

直接把代码贴这里了,我今天电脑的截屏功能出了点小问题,所以截不了图了。
代码:

<!DOCTYPE html>
<html>
<head>
  <title>document</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
    }
    canvas {
      display: block;
      background: #000;
      background-size: cover;
    }
  </style>
</head>
<body>
  <canvas id="myCanvas"></canvas>

  <script>
    const canvas = document.getElementById("myCanvas");
    const ctx = canvas.getContext("2d");
    const width = window.innerWidth;
    const height = window.innerHeight;

    canvas.width = width;
    canvas.height = height;

    const particles = [];
    const connections = [];
    const particleCount = 300;
    const particleSpeed = 1;
    const particleSize = 2;
    const maxDistance = 100;
    const lightningColor = "#fff";

    class Particle {
      constructor() {
        this.x = Math.random() * width;
        this.y = Math.random() * height;
        this.color = "#fff";
        this.angle = Math.random() * 360;
        this.speed = Math.random() * particleSpeed;
        this.opacity = Math.random() * 0.5 + 0.5;
      }

      update() {
        this.x += Math.cos(this.angle) * this.speed;
        this.y += Math.sin(this.angle) * this.speed;

        if (this.x < 0 || this.x > width || this.y < 0 || this.y > height) {
          this.x = Math.random() * width;
          this.y = Math.random() * height;
        }
      }

      draw() {
        ctx.beginPath();
        ctx.arc(this.x, this.y, particleSize, 0, Math.PI * 2);
        ctx.fillStyle = `rgba(255, 255, 255, ${this.opacity})`;
        ctx.fill();
      }
    }

    function createParticles() {
      for (let i = 0; i < particleCount; i++) {
        particles.push(new Particle());
      }
    }

    function drawConnections() {
      for (let i = 0; i < particles.length; i++) {
        for (let j = i + 1; j < particles.length; j++) {
          const dx = particles[i].x - particles[j].x;
          const dy = particles[i].y - particles[j].y;
          const distance = Math.sqrt(dx * dx + dy * dy);

          if (distance < maxDistance) {
            ctx.beginPath();
            ctx.moveTo(particles[i].x, particles[i].y);
            ctx.lineTo(particles[j].x, particles[j].y);
            ctx.strokeStyle = lightningColor;
            ctx.lineWidth = 0.2 * (1 - distance / maxDistance);
            ctx.stroke();
            ctx.closePath();
          }
        }
      }
    }

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

      for (const particle of particles) {
        particle.update();
        particle.draw();
      }

      drawConnections();

      requestAnimationFrame(animate);
    }

    document.addEventListener("mousemove", (e) => {
      const mouseX = e.clientX;
      const mouseY = e.clientY;

      for (const particle of particles) {
        const dx = mouseX - particle.x;
        const dy = mouseY - particle.y;
        const distance = Math.sqrt(dx * dx + dy * dy);

        if (distance < maxDistance) {
          particle.angle = Math.atan2(dy, dx);
          particle.speed = 5;
        } else {
          particle.speed = Math.random() * particleSpeed;
        }
      }
    });

    createParticles();
    animate();
  </script>
</body>
</html>

回顾

顺便回一下代码雨和立方体的功能:
立方体:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 200px;
            height: 200px;
            border: 1px solid #fff;
            background: rgba(255, 255, 255,0.75);
            color: #fff;
            font-size: 100px;
            line-height: 200px;
            text-align: center;
            position: absolute;
            transition: 2s;
        }
        body{
            perspective: 800px;
            display: flex;
            justify-content: center;
            align-items: center;
            background: #000;
            height: 100vh;
        }
        .box:nth-of-type(1){transform: rotateY(90deg) translateZ(100px);}
        .box:nth-of-type(2){transform: rotateY(-90deg) translateZ(100px);}
        .box:nth-of-type(3){transform: rotateX(90deg) translateZ(100px);}
        .box:nth-of-type(4){transform: rotateX(-90deg) translateZ(100px);}
        .box:nth-of-type(5){transform: rotateY(0deg) translateZ(100px);}
        .box:nth-of-type(6){transform: rotateY(180deg) translateZ(100px);}
        .boxs{
            position: absolute;
            transform: rotateX(45deg) rotateY(45deg);
            transform-style: preserve-3d;
            width: 200px;
            height: 200px;
            animation: go 3s infinite linear;
            animation-play-state: running;
        }
        @keyframes go{
            0%{transform: rotateX(0deg) rotateY(0deg);}
            100%{transform: rotateX(720deg) rotateY(360deg);}
        }
    </style>
</head>
<body>
    <div class="boxs">
        <div class="box first"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
    </div>
</body>
</html>

最后代码雨:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Code</title>
    <style>
        body{
            margin: 0;
            overflow: hidden;
        }
        
    </style>
</head>

<body>
<canvas id="myCanvas"></canvas>
<script>
    const width = document.getElementById("myCanvas").width = screen.availWidth;
    const height = document.getElementById("myCanvas").height = screen.availHeight;
    const ctx = document.getElementById("myCanvas").getContext("2d");
    const arr = Array(Math.ceil(width / 10)).fill(0);
    const str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split("");

function rain() {
    ctx.fillStyle = "rgba(0,0,0,0.05)";
    ctx.fillRect(0, 0, width, height);
    ctx.fillStyle = "#0f0";
    arr.forEach(function (value, index) {
        ctx.fillText(str[Math.floor(Math.random() * str.length)], index * 10, value + 10);
        arr[index] = value >= height || value > 8888 * Math.random() ? 0 : value + 10;
    });
}

setInterval(rain, 30);
</script>
</body>
</html>

都很Nice,让我们把这三段代码放在一起,由于某种原因,粒子特效的代码没放进去,最终代码长这样:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 200px;
            height: 200px;
            border: 1px solid #fff;
            background: rgba(255, 255, 255,0.25);
            color: #fff;
            font-size: 100px;
            line-height: 200px;
            text-align: center;
            position: absolute;
            transition: 2s;
        }
        body{
            perspective: 800px;
            display: flex;
            justify-content: center;
            align-items: center;
            background: #000;
            height: 100vh;
        }
        .box:nth-of-type(1){transform: rotateY(90deg) translateZ(100px);}
        .box:nth-of-type(2){transform: rotateY(-90deg) translateZ(100px);}
        .box:nth-of-type(3){transform: rotateX(90deg) translateZ(100px);}
        .box:nth-of-type(4){transform: rotateX(-90deg) translateZ(100px);}
        .box:nth-of-type(5){transform: rotateY(0deg) translateZ(100px);}
        .box:nth-of-type(6){transform: rotateY(180deg) translateZ(100px);}
        .boxs{
            position: absolute;
            transform: rotateX(45deg) rotateY(45deg);
            transform-style: preserve-3d;
            width: 200px;
            height: 200px;
            animation: go 3s infinite linear;
            animation-play-state: running;
        }
        @keyframes go{
            0%{transform: rotateX(0deg) rotateY(0deg);}
            100%{transform: rotateX(180deg) rotateY(180deg);}
        }
    </style>
</head>
<body>
    <div class="boxs">
        <div class="box first"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
    </div>
    <canvas id="myCanvas"></canvas>
    <script>
        const width = document.getElementById("myCanvas").width = screen.availWidth;
        const height = document.getElementById("myCanvas").height = screen.availHeight;
        const ctx = document.getElementById("myCanvas").getContext("2d");
        const arr = Array(Math.ceil(width / 10)).fill(0);
        const str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split("");
    
    function rain() {
        ctx.fillStyle = "rgba(0,0,0,0.05)";
        ctx.fillRect(0, 0, width, height);
        ctx.fillStyle = "#0f0";
        arr.forEach(function (value, index) {
            ctx.fillText(str[Math.floor(Math.random() * str.length)], index * 10, value + 10);
            arr[index] = value >= height || value > 8888 * Math.random() ? 0 : value + 10;
        });
    }
    
    setInterval(rain, 30);
    </script>
    
</body>

注意所有我的HTML代码都可以复制之后直接用,不用改其他的任何东西。看一下这段代码的效果:
在这里插入图片描述
还可以,但总觉的少了点什么。

最后

敬请期待!

  • 25
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的HTML星空粒子特效代码: ```html <!DOCTYPE html> <html> <head> <style> body { margin: 0; overflow: hidden; } canvas { display: block; } </style> </head> <body> <canvas id="starfield"></canvas> <script> // 创建画布 var canvas = document.getElementById("starfield"); var ctx = canvas.getContext("2d"); // 设置画布大小 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 创建星星数组 var stars = []; var numStars = 100; // 初始化星星 for (var i = 0; i < numStars; i++) { stars[i] = { x: Math.random() * canvas.width, y: Math.random() * canvas.height, radius: Math.random() * 1.5 + 1, vx: Math.floor(Math.random() * 3) - 1.5, vy: Math.floor(Math.random() * 3) - 1.5 }; } // 绘制星星 function drawStars() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = "white"; for (var i = 0; i < numStars; i++) { var star = stars[i]; ctx.beginPath(); ctx.arc(star.x, star.y, star.radius, 0, Math.PI * 2, false); ctx.fill(); } } // 更新星星位置 function updateStars() { for (var i = 0; i < numStars; i++) { var star = stars[i]; // 更新位置 star.x += star.vx; star.y += star.vy; // 边界检测 if (star.x < 0 || star.x > canvas.width) { star.vx = -star.vx; } if (star.y < 0 || star.y > canvas.height) { star.vy = -star.vy; } } } // 动画循环 function animate() { drawStars(); updateStars(); requestAnimationFrame(animate); } // 开始动画 animate(); </script> </body> </html> ``` 这段代码使用HTML5的canvas元素绘制了一个星空粒子特效。它创建了一个画布,然后在画布上绘制了一定数量的随机大小和位置的白色圆形来表示星星。每个星星都有一个随机的速度,它们会在画布上移动,并在达到边界时反弹。通过不断更新星星的位置并重新绘制它们,实现了一个看起来像星空的动态效果。你可以将该代码复制粘贴到一个HTML文件中,然后在浏览器中打开该文件来查看效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值