粒子向上持续瀑布动画效果(直接粘贴到记事本改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>
相关推荐
我命由我1234515 分钟前
微信开发者工具 - 模拟器分离窗口与关闭分离窗口
前端·javascript·学习·微信小程序·前端框架·html·js
S***428020 分钟前
JavaScript在Web中的Angular
前端·javascript·angular.js
4***149029 分钟前
Vue代码规范详解
javascript·vue.js·代码规范
San3031 分钟前
深入理解 JavaScript 词法作用域链:从代码到底层实现机制
前端·javascript·ecmascript 6
进击的野人32 分钟前
深入理解 JavaScript Promise:原理、用法与实践
javascript·面试·ecmascript 6
我有一棵树1 小时前
file 协议与 http 协议的区别:为什么本地 HTML 无法加载相对路径 JS,以及正确的解决方式
javascript·http·html
有意义1 小时前
JavaScript 词法作用域与闭包:从底层原理到实战理解
前端·javascript·面试
AY呀1 小时前
黑马喽大闹天宫与JavaScript的寻亲记:作用域与作用域链全解析
前端·javascript·面试
梦想CAD控件2 小时前
AI生成CAD图纸(云原生CAD+AI让设计像聊天一样简单)
前端·javascript·vue.js
最爱老虎头2 小时前
Konvajs实现虚拟表格
javascript