用js写一个动态粒子效果的跳动的爱心

废话不多说,直接上代码:

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
</head>

<body style="background-color: rgb(0, 0, 0);">
    <title>爱心跳动粒子效果</title>
    <div class="container">
        <canvas id="myCanvas" width="1000px" height="1000px" style="background-color: black;"></canvas>
    </div>
</body>

</html>

<script>
    // 获取 canvas 元素并设置上下文  
    var canvas = document.getElementById('myCanvas');
    var ctx = canvas.getContext('2d');

    // 定义粒子的数组  
    var points = [];

    // 粒子的属性  
    var particle_limit = 44;// 粒子界限
    var particle_num = 50;   // 单个点的粒子数量
    var point_density = 0.1;// 点密度
    var point_num = 0;      // 点数量
    var sw = canvas.width;  // 画布的宽
    var sh = canvas.height; // 画布的高
    var throb = 0;
    var dir = 0;

    // 生成每个点的粒子参数
    for (var t = 0; t <= 2 * Math.PI; t += point_density) {
        var particles = []; // 创建一个空的行数组
        for (var i = 0; i <= particle_num; i++) {
            particles[i] = {
                x: Math.random() * 2 * particle_limit - particle_limit,
                y: Math.random() * 2 * particle_limit - particle_limit,
                size: Math.random() * 2.5 + 0.1,
                speedX: Math.random() * 3 - 1.5,
                speedY: Math.random() * 3 - 1.5,
            };
        }
        points.push(particles); // 将行数组添加到二维数组中
        point_num += 1;
    }

    // 画一个点
    function point(x, y, index) {
        ctx.fillStyle = '#FF0000';
        for (var i = 0; i < particle_num; i++) {
            var p = points[index][i];
            ctx.beginPath();
            ctx.arc(p.x + x, p.y + y, p.size, 0, Math.PI * 2, true);
            ctx.closePath();
            ctx.fill();
            p.x += p.speedX;
            p.y += p.speedY;
            if (p.x + x < -particle_limit + x || p.x + x > particle_limit + x) {
                p.speedX = -p.speedX;
            }
            if (p.y + y < -particle_limit + y || p.y + y > particle_limit + y) {
                p.speedY = -p.speedY;
            }
        }
    }

    // 画爱心
    function draw() {
        // 清除画布内容
        ctx.clearRect(0, 0, sw, sh);
        var i = 0;
        // 定义绘制爱心的公式
        for (var t = 0; t <= 2 * Math.PI; t += point_density) {
            var ix = (16 + throb) * Math.pow(Math.sin(t), 3);
            var iy = -(
                (13 + throb) * Math.cos(t) -
                5 * Math.cos(2 * t) -
                (2 + throb) * Math.cos(3 * t) -
                Math.cos(4 * t)
            );
            ix *= 16; // 放大 x 坐标
            iy *= 16; // 放大 y 坐标

            // 将爱心的点绘制在 Canvas 上
            point(sw / 2 + ix, sh / 2 + iy, i);
            i += 1;
        }
        // 用 setTimeout 替换 requestAnimationFrame,以确保每秒调用一次
        requestAnimationFrame(draw);
    }

    // 跳动
    function performAction() {
        if (dir == 0) {
            throb += 0.15;
        }
        else {
            throb -= 0.15;
        }
        if (throb <= 0) {
            dir = 0;
        }
        if (throb >= 1.5) {
            dir = 1;
        }
        setTimeout(performAction, 40);
    }
    performAction()
    draw();
</script>

<style>
    .container {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
        /* 设置容器高度为视口高度 */
    }
</style>

效果:
当然,我只是截了一个图,实际是跳动的。
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水水不水啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值