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>
相关推荐
柯南二号15 分钟前
【Java后端】MyBatis-Plus 原理解析
java·开发语言·mybatis
我是哈哈hh35 分钟前
【Node.js】ECMAScript标准 以及 npm安装
开发语言·前端·javascript·node.js
张元清1 小时前
电商 Feeds 流缓存策略:Temu vs 拼多多的技术选择
前端·javascript·面试
pepedd8641 小时前
浅谈js拷贝问题-解决拷贝数据难题
前端·javascript·trae
@大迁世界1 小时前
useCallback 的陷阱:当 React Hooks 反而拖了后腿
前端·javascript·react.js·前端框架·ecmascript
小高0071 小时前
📌React 路由超详解(2025 版):从 0 到 1 再到 100,一篇彻底吃透
前端·javascript·react.js
summer7771 小时前
GIS三维可视化-Cesium
前端·javascript·数据可视化
Sammyyyyy2 小时前
2025年,Javascript后端应该用 Bun、Node.js 还是 Deno?
开发语言·javascript·node.js
小高0073 小时前
面试官:npm run build 到底干了什么?从 package.json 到 dist 的 7 步拆解
前端·javascript·vue.js
William一直在路上3 小时前
Python数据类型转换详解:从基础到实践
开发语言·python