用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%)`),可以根据需要修改 [2025-04-23 08:29:48 | AI写代码神器 | 1035点数解答]
- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF - 8"> <meta name="viewport" content="width=device-width, initial - scale = 1.0"> <title>爱心</title> </head> <body> <canvas id="myCanvas" width="200" height="200"></canvas> <script> const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); ctx.beginPath(); ctx.moveTo(100, 20); ctx.bezierCurveTo(140, 20, 160, 60, 100, 100); ctx.bezierCurveTo(165点数解答 | 2025-03-23 15:00:43)153
- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>爱心</title> <style> .heart { width: 100px; height: 90px; position: relative; margin: 100px auto; } .heart:before, .heart:after { content: ""; position: absolute; top: 0; left: 0; width: 50px; height: 80px; background: red; border-radius: 50p(198点数解答 | 2025-05-01 13:24:06)93
- 爱心(71点数解答 | 2025-03-01 16:58:36)149
- <%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!-- 请在此处编辑您的代码--> <!--引入java util 包 --> <!-----------begin-----------> //补充代码 <!-----------end-----------> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>insert title here</title> </head> <body> <!--使用 list 存储 map 集合,并利用 for 循环遍历十次,将年龄(age) 和姓名(name)增加到容器中(240点数解答 | 2024-11-25 09:17:43)169
- 以下是一个非常简化的 微信钱包前端演示代码(仅用于界面概念展示,无真实支付功能) ,包含 HTML、CSS、JavaScript 基础结构,模拟钱包首页、零钱、支付密码等基础交互逻辑(实际微信钱包是庞大复杂系统,涉及安全加密、金融接口、合规流程等,此为纯前端演示): HTML 部分(index.html) <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> <title>微信钱包演示</title> <style> /* CSS 样式写在这里,也可外联 */ body { margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helveti(2097点数解答 | 2025-07-11 18:06:46)79
- 你好。程序运行后:字体设置项做得很好。但,存在的问题是:1、没有自动按菜单生成练习文本,无法练习。2、没有键盘布局面板。3、没有练习文本,没有键盘布局面板,也无法对当前练习的文本通过键盘布局进行常亮提示。4、背景图打开后,无法在窗口显示。5、打开背景歌曲无法正常播放。6、内容界面用一个窗口显示,不要充满整个电脑屏幕,如果是手机 ,屏幕没这么宽。7、键盘字符映射是真实键盘所有键盘的映射代码表。请求修改。 你写的源程序代码如下: ### HTML 文件 (`index.html`) ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>英文打字练习程序</title> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>英文打字练习程序</h1> <d(2538点数解答 | 2025-08-20 12:28:06)48
- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Three.js 示例</title> <style> body { margin: 0; } canvas { display: block; } </style> </head> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r146/three.min.js"></script> <script> // 你的 Three.js 代码 const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); (164点数解答 | 2025-02-27 23:30:20)135
- 帮我美化和优化以下代码:<!--#include file="inc/conn.asp"--> <!--#include file="inc/config.asp"--> <% dim verifycode,verifycode2 if verifycode<>verifycode2 then response.write"location.href='index.asp'</script>" response.end() founderr=true else %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3c.org/tr/1999/rec-html401-19991224/loose.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title><%=sitename%></title> <meta http-equiv=content-type content="text/html; charset=gb(45点数解答 | 2024-07-11 14:16:55)246
- <%@ page contenttype="text/html; charset=utf-8" pageencoding="utf-8" %> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>document</title> <style> @import url('https://fonts.googleapis.com/css?family=montserrat:400,800'); * { box-sizing: border-box; } body, html {(324点数解答 | 2024-10-30 19:49:49)178
- <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>HTML 5</title> </head> <body> </body> </html>(222点数解答 | 2025-02-18 14:20:46)1656
- data:text/html,<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>给男朋友的消息</title></head><body><a href="javascript:void(0);" onclick="alert('我可爱的男朋友,好想你呀')">点击这里查看消息</a></body></html>(139点数解答 | 2025-03-17 22:07:34)170
- ```html <!DOCTYPE html> <html> <head> <title>生日祝福</title> <style> body { background-color: #f0f8ff; text-align: center; } #cake { width: 200px; } #fireworks { width: 200px; } </style> </head> <body> <h1>生日快乐!</h1> <img id="cake" src="https://example.com/cake.jpg" alt="生日蛋糕"> <img id="fireworks" src="https://example.com/fireworks.jpg" alt="烟花"> <p>愿你的每一天都充满惊喜和欢笑,梦想成真,幸福永远!</p> </body> </html> ``` (164点数解答 | 2025-03-19 22:07:03)117