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>
相关推荐
六bring个六8 分钟前
文件系统交互实现
开发语言·c++·qt·交互
jingyu飞鸟13 分钟前
Centos7系统(最小化安装)安装zabbix7版本详细文章、nginx源代码配置、php源代码、mysql-yum安装
开发语言·php
dhxhsgrx23 分钟前
PYTHON训练营DAY27
开发语言·python
小山菌23 分钟前
mac中加载C++动态库文件
开发语言·c++·macos
关于不上作者榜就原神启动那件事43 分钟前
Java基础学习
java·开发语言·学习
橙子199110161 小时前
在 Kotlin 中,什么是解构,如何使用?
android·开发语言·kotlin
OK_boom1 小时前
React-useRef
javascript·react.js·ecmascript
未来之窗软件服务1 小时前
solidwors插件 开发————仙盟创梦IDE
前端·javascript·数据库·ide·仙盟创梦ide
Q_Q19632884751 小时前
python的家教课程管理系统
开发语言·spring boot·python·django·flask·node.js·php
小白学大数据1 小时前
基于Scrapy-Redis的分布式景点数据爬取与热力图生成
javascript·redis·分布式·scrapy