js粒子效果(二)

  效果:

 

代码: 

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

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

        canvas {
            display: block;
        }
    </style>
</head>

<body>
    <canvas id="particle-canvas"></canvas>
    <script>
        // 获取Canvas元素和2D上下文
        const canvas = document.getElementById('particle-canvas');
        const ctx = canvas.getContext('2d');

        // 设置Canvas的宽度和高度为窗口的宽度和高度
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        // 鼠标坐标
        let mouseX = 0;
        let mouseY = 0;

        // 创建树叶粒子对象
        class LeafParticle {
            constructor(x, y) {
                this.x = x;
                this.y = y;
                this.size = Math.random() * 3 + 2;
                this.speedX = (Math.random() - 0.5) * 5;
                this.speedY = (Math.random() - 0.5) * 5;
            }

            update() {
                this.x += this.speedX;
                this.y += this.speedY;
                if (this.size > 0.2) this.size -= 0.1;
            }

            draw() {
                const distanceToMouse = Math.sqrt((this.x - mouseX) ** 2 + (this.y - mouseY) ** 2);
                const maxDistance = 100;

                const colorIntensity = Math.min(1, 1 - distanceToMouse / maxDistance);

                const gradient = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.size);
                gradient.addColorStop(0, `rgba(137, 247, 254, ${colorIntensity})`);
                gradient.addColorStop(1, `rgba(102, 166, 255, ${colorIntensity * 0.8})`);
                ctx.fillStyle = gradient;

                ctx.strokeStyle = `rgba(137, 247, 254, ${colorIntensity})`;

                ctx.beginPath();
                ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
                ctx.closePath();

                ctx.fill();
                ctx.stroke();
            }
        }
        // 创建多个树叶粒子
        const leaves = [];

        function createLeaves(e) {
            mouseX = e.x;
            mouseY = e.y;

            for (let i = 0; i < 5; i++) {
                leaves.push(new LeafParticle(mouseX, mouseY));
            }
        }

        // 监听鼠标移动事件
        window.addEventListener('mousemove', createLeaves);

        // 动画循环
        function animate() {
            // 清空Canvas
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            // 更新和绘制每个树叶粒子
            for (let i = 0; i < leaves.length; i++) {
                leaves[i].update();
                leaves[i].draw();
            }

            // 移除透明度较低的树叶粒子
            leaves.forEach((leaf, index) => {
                if (leaf.size <= 0.2) {
                    leaves.splice(index, 1);
                }
            });

            // 递归调用动画函数
            requestAnimationFrame(animate);
        }

        // 启动动画
        animate();

    </script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值