粒子向上持续瀑布动画效果(直接粘贴到记事本改html即可)

代码: 根据个人喜好修改即可

html 复制代码
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>宽粒子向上效果</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            background-color: black;
        }
        canvas {
            display: block;
        }
    </style>
</head>
<body>
    <canvas id="flameCanvas"></canvas>
    <script>
        const canvas = document.getElementById('flameCanvas');
        const ctx = canvas.getContext('2d');

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

        class Particle {
            constructor(x, y) {
                this.x = x;
                this.y = y;
                this.size = Math.random() * 10 + 5; // 粒子大小
                this.speedY = Math.random() * -4 - 2; // 向上速度
                this.speedX = (Math.random() - 0.5) * 2; // 水平随机速度
                this.color = `rgba(255, ${Math.floor(Math.random() * 100) + 155}, 0, 0.8)`; // 橙色
                this.friction = 0.98; // 摩擦力
            }

            update() {
                this.x += this.speedX;
                this.y += this.speedY;
                this.size *= this.friction; // 粒子逐渐变小

                if (this.size < 0.5) {
                    this.size = 0; // 粒子消失
                }
            }

            draw() {
                ctx.fillStyle = this.color;
                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
                ctx.fill();
            }
        }

        class Flame {
            constructor(x, width) {
                this.x = x;
                this.width = width;
                this.particles = [];
                this.particleCount = 40; // 每次生成的粒子数量
            }

            update() {
                // 生成新粒子
                for (let i = 0; i < this.particleCount; i++) {
                    const particleX = this.x + (Math.random() - 0.5) * this.width; // 生成在宽度范围内
                    this.particles.push(new Particle(particleX, canvas.height));
                }

                // 更新粒子位置
                this.particles.forEach((particle, index) => {
                    particle.update();
                    if (particle.size <= 0) {
                        this.particles.splice(index, 1); // 移除消失的粒子
                    }
                });
            }

            draw() {
                this.particles.forEach(particle => {
                    particle.draw();
                });
            }
        }

        const flames = [];
        const flameWidth = canvas.width * 0.6; // 宽度为屏幕的50%

        // 创建一束宽粒子
        flames.push(new Flame(canvas.width / 2, flameWidth));

        function animate() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            flames.forEach(flame => {
                flame.update();
                flame.draw();
            });
            requestAnimationFrame(animate);
        }

        animate();
    </script>
</body>
</html>
相关推荐
Jonathan Star6 分钟前
沉浸式雨天海岸:用A-Frame打造WebXR互动场景
前端·javascript
老前端的功夫37 分钟前
Web应用的永生之术:PWA落地与实践深度指南
java·开发语言·前端·javascript·css·node.js
LilySesy1 小时前
ABAP+WHERE字段长度不一致报错解决
java·前端·javascript·bug·sap·abap·alv
Wang's Blog2 小时前
前端FAQ: Vue 3 与 Vue 2 相⽐有哪些重要的改进?
前端·javascript·vue.js
用户47949283569153 小时前
JavaScript 的 NaN !== NaN 之谜:从 CPU 指令到 IEEE 754 标准的完整解密
前端·javascript
醉方休3 小时前
Web3.js 全面解析
前端·javascript·electron
前端开发爱好者4 小时前
前端新玩具:Vike 发布!
前端·javascript
今天也是爱大大的一天吖4 小时前
vue2中的.native修饰符和$listeners组件属性
前端·javascript·vue.js
fxshy4 小时前
在 Vue 3 + Vite 项目中使用 Less 实现自适应布局:VW 和 VH 的应用
前端·javascript·less
奇舞精选4 小时前
AI时代的前端知识拾遗:前端事件循环机制详解(基于 WHATWG 最新规范)
前端·javascript