JavaScript + HTML5 Canvas 实现互动爱心雨

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>情人节快乐!</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }

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

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

        // 爱心类
        class Heart {
            constructor() {
                this.x = Math.random() * canvas.width;
                this.y = Math.random() * -canvas.height;
                this.size = Math.random() * 10 + 10;
                this.speed = Math.random() * 2 + 1;
            }

            draw() {
                ctx.beginPath();
                const t = Date.now() / 1000;
                const x = 16 * Math.pow(Math.sin(t), 3);
                const y = 13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t);
                const scaledX = this.x + x * this.size;
                const scaledY = this.y - y * this.size;
                ctx.moveTo(scaledX, scaledY);
                ctx.bezierCurveTo(scaledX + this.size, scaledY - this.size, scaledX + 2 * this.size, scaledY + this.size, scaledX, scaledY + 2 * this.size);
                ctx.bezierCurveTo(scaledX - 2 * this.size, scaledY + this.size, scaledX - this.size, scaledY - this.size, scaledX, scaledY);
                ctx.fillStyle = 'red';
                ctx.fill();
            }

            move() {
                this.y += this.speed;
                if (this.y > canvas.height) {
                    this.y = -this.size;
                    this.x = Math.random() * canvas.width;
                }
            }
        }

        // 创建爱心数组
        const hearts = [];
        for (let i = 0; i < 100; i++) {
            hearts.push(new Heart());
        }

        // 动画循环
        function animate() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            hearts.forEach(heart => {
                heart.draw();
                heart.move();
            });
            requestAnimationFrame(animate);
        }

        animate();

        // 鼠标互动
        canvas.addEventListener('mousemove', (e) => {
            const mouseX = e.clientX;
            const mouseY = e.clientY;
            hearts.forEach(heart => {
                const dx = mouseX - heart.x;
                const dy = mouseY - heart.y;
                const distance = Math.sqrt(dx * dx + dy * dy);
                if (distance < 100) {
                    const angle = Math.atan2(dy, dx);
                    heart.x -= Math.cos(angle) * 2;
                    heart.y -= Math.sin(angle) * 2;
                }
            });
        });
    </script>
</body>

</html>
相关推荐
琹箐13 分钟前
Ant ASpin自定义 indicator 报错
前端·javascript·typescript
小小小小小惠18 分钟前
Responsetype blob会把接口接收的二进制文件转换成blob格式
前端·javascript
爱电摇的小码农18 分钟前
【深度探究系列(5)】:前端开发打怪升级指南:从踩坑到封神的解决方案手册
前端·javascript·css·vue.js·node.js·html5·xss
爱编程的喵1 小时前
React入门实战:从静态渲染到动态状态管理
前端·javascript
L_autinue_Star1 小时前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
唐叔在学习1 小时前
不用装插件!3轮对话,我用油猴脚本+AI复刻了掘金闪念笔记,真香!
javascript·浏览器
AliciaIr1 小时前
深入React事件机制:解密“合成事件”与“事件委托”的底层奥秘
javascript·react.js
元气小嘉1 小时前
前端技术小结
开发语言·前端·javascript·vue.js·人工智能
励志的大鹰哥2 小时前
V少JS基础班之第七弹
开发语言·javascript·ecmascript
cccyi72 小时前
Vue3基础知识
javascript·vue.js