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>
相关推荐
王伯安呢6 分钟前
Java开发环境配置入门指南
java·开发语言·jvm·eclipse·环境搭建·新手
·前路漫漫亦灿灿9 分钟前
C++-类型转换
开发语言·c++
灵感__idea13 分钟前
JavaScript高级程序设计(第5版):前端的能力边界
前端·javascript·程序员
华洛14 分钟前
SEO还没死,GEO之战已经开始
前端·javascript·产品
Kyln.Wu24 分钟前
【python实用小脚本-205】[HR揭秘]手工党逐行查Bug的终结者|Python版代码质量“CT机”加速器(建议收藏)
开发语言·python·bug
计算机毕业设计木哥26 分钟前
Python毕业设计推荐:基于Django的饮食计划推荐与交流分享平台 饮食健康系统 健康食谱计划系统
开发语言·hadoop·spring boot·后端·python·django·课程设计
rockmelodies34 分钟前
Java安全体系深度研究:技术演进与攻防实践
java·开发语言·安全
OEC小胖胖35 分钟前
Next.js 介绍:为什么选择它来构建你的下一个 Web 应用?
开发语言·前端·web·next.js
代码栈上的思考44 分钟前
深入解析 Java 内存可见性问题:从现象到 volatile 解决方案
java·开发语言
F2E_Zhangmo4 小时前
基于cornerstone3D的dicom影像浏览器 第三章 拖拽seriesItem至displayer上显示第一张dicom
前端·javascript·cornerstone·cornerstone3d·cornerstonejs