手把手教你用 HTML 实现超炫动态粒子爱心代码
# HTML动态粒子爱心代码 以下是一个使用HTML、CSS和JavaScript创建的动态粒子爱心效果: ```html <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>动态粒子爱心</title> <style> body { margin: 0; overflow: hidden; background-color: #000; } canvas { display: block; } </style> </head> <body> <canvas id="heartCanvas"></canvas> <script> const canvas = document.getElementById('heartCanvas'); const ctx = canvas.getContext('2d'); // 设置画布大小为窗口大小 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 粒子数组 const particles = []; const particleCount = 150; // 粒子类 class Particle { constructor() { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height; this.size = Math.random() * 3 + 1; this.speedX = Math.random() * 2 - 1; this.speedY = Math.random() * 2 - 1; this.color = `hsl(${Math.random() * 60 + 330}, 100%, 50%)`; } update() { // 爱心形状的引力 const dx = this.x - canvas.width / 2; const dy = this.y - canvas.height / 2; const distance = Math.sqrt(dx * dx + dy * dy); // 爱心方程: (x² + y² - 1)³ - x²y³ = 0 const heartX = dx / 50; const heartY = -dy / 50; const heartEq = Math.pow((heartX * heartX + heartY * heartY - 1), 3) - heartX * heartX * heartY * heartY * heartY; if (heartEq < 0) { // 如果在爱心内部,则向中心吸引 const forceDirectionX = dx / distance; const forceDirectionY = dy / distance; const force = (100 - distance) / 100; this.speedX -= forceDirectionX * force * 0.2; this.speedY -= forceDirectionY * force * 0.2; } // 更新位置 this.x += this.speedX; this.y += this.speedY; // 边界检查 if (this.x < 0 || this.x > canvas.width) { this.speedX *= -0.5; } if (this.y < 0 || this.y > canvas.height) { this.speedY *= -0.5; } } draw() { ctx.fillStyle = this.color; ctx.beginPath(); ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2); ctx.fill(); } } // 初始化粒子 function init() { for (let i = 0; i < particleCount; i++) { particles.push(new Particle()); } } // 动画循环 function animate() { ctx.clearRect(0, 0, canvas.width, canvas.height); // 更新和绘制所有粒子 for (let i = 0; i < particles.length; i++) { particles[i].update(); particles[i].draw(); } requestAnimationFrame(animate); } // 窗口大小改变时调整画布 window.addEventListener('resize', function() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }); init(); animate(); </script> </body> </html> ``` ## 代码说明 1. 这段代码创建了一个黑色的页面,上面有多个彩色粒子 2. 粒子会受到爱心形状的引力影响,逐渐形成爱心图案 3. 粒子在爱心内部时会受到向中心的引力,在外部则自由移动 4. 爱心形状是通过数学方程 `(x² + y² - 1)³ - x²y³ = 0` 定义的 ## 注意事项 1. 粒子数量(`particleCount`)可以根据性能需求调整,数值越大效果越密集但对性能要求越高 2. 爱心的大小和位置可以通过调整代码中的除数(如50)来改变 3. 粒子颜色范围目前设置为红色到粉色(`hsl(330-390, 100%, 50%)`),可以根据需要修改 ######[AI写代码神器 | 1035点数解答 | 2025-04-23 08:29:48]
- HTML 代码问题分析、修正及开发小心点揭秘:打造完美爱心动画 (字节豆包 | 1993点数解答 | 2025-11-02 23:37:54)105
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)368
- HTML 画布绘制爱心代码优化与注意要点揭秘(字节豆包 | 165点数解答 | 2025-03-23 15:00:43)296
- 代码实现红色爱心:结构清晰且优化建议与逻辑要点大揭秘(字节豆包 | 198点数解答 | 2025-05-01 13:24:06)236
- 优化高中英语核心词汇短语HTML页面,这些建议不容错过!(DeepSeek | 217点数解答 | 2025-07-01 23:41:02)201
- Java JSP 代码:用 List 存储 Map 集合并循环添加姓名和年龄(GPT | 240点数解答 | 2024-11-25 09:17:43)258
- 微信钱包前端演示代码:从问题修正到完善功能及安全提示(字节豆包 | 2097点数解答 | 2025-07-11 18:06:46)166
- 英文打字练习程序:从问题重重到全面优化的蜕变(字节豆包 | 2538点数解答 | 2025-08-20 12:28:06)174
- 51 单片机:定时器 0 实现 8 个 LED 循环点亮,附代码及优化建议(字节豆包 | 1193点数解答 | 2024-12-27 15:10:29)338
- 爱心:让世界更美好的积极情感力量(字节豆包 | 71点数解答 | 2025-03-01 16:58:36)289
- 超炫!飘花爱心祝福HTML特效代码及开发避坑指南 (DeepSeek | 792点数解答 | 2025-11-08 02:17:45)129
- 此需求是代码优化,并非生成标题,以下是优化后的代码,若你需要标题请提供合适的描述信息: ```asp <!-- 包含数据库连接文件 --> <!--#include file="inc/conn.asp"--> <!-- 包含配置文件 --> <!--#include file="inc/config.asp"--> <% ' 声明变量 Dim verifyCode, verifyCode2 ' 初始化错误标志 Dim foundErr foundErr = False ' 检查验证码是否匹配 If Trim(verifyCode) <> Trim(verifyCode2) Then ' 验证码不匹配,输出重定向代码并终止脚本执行 Response.Write "<script language='javascript'>location.href='index.asp'</script>" Response.End foundErr = True Else %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- 设置页面标题 --> <title><%=sitename%></title> <!-- 设置字符编码 --> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> </head> <body> <!-- 页面内容 --> </body> </html> <% End If %> ``` (GPT | 45点数解答 | 2024-07-11 14:16:55)368