用 HTML 实现超炫 3D 旋转粒子爱心,代码详解与参数调整秘籍!
以下是一个使用 HTML、CSS 和 JavaScript 实现 3D 旋转粒子爱心的代码示例: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>3D Rotating Particle Heart</title> <style> /* 设置画布样式 */ body { margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; min-height: 100vh; background-color: #000; overflow: hidden; } canvas { display: block; } </style> </head> <body> <canvas id="canvas"></canvas> <script> // 获取画布元素 const canvas = document.getElementById('canvas'); // 获取 2D 绘图上下文 const ctx = canvas.getContext('2d'); // 设置画布宽度和高度为窗口的宽度和高度 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 定义粒子数量 const particleCount = 500; // 存储粒子的数组 const particles = []; // 定义粒子类 class Particle { constructor() { // 生成随机的角度 const angle = Math.random() * 2 * Math.PI; // 根据爱心曲线公式计算爱心上的点的坐标 const t = angle; 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 scale = 10; // 计算粒子的初始位置 this.x = x * scale + canvas.width / 2; this.y = -y * scale + canvas.height / 2; // 粒子的速度 this.vx = (Math.random() - 0.5) * 2; this.vy = (Math.random() - 0.5) * 2; // 粒子的颜色 this.color = `hsl(${Math.random() * 360}, 100%, 50%)`; // 粒子的透明度 this.alpha = 1; } // 更新粒子的位置和透明度 update() { this.x += this.vx; this.y += this.vy; this.alpha -= 0.005; } // 绘制粒子 draw() { ctx.beginPath(); ctx.arc(this.x, this.y, 2, 0, Math.PI * 2); ctx.fillStyle = `rgba(${parseInt(this.color.slice(4, -1).split(',')[0])}, ${parseInt(this.color.slice(4, -1).split(',')[1])}, ${parseInt(this.color.slice(4, -1).split(',')[2])}, ${this.alpha})`; ctx.fill(); } } // 初始化粒子数组 for (let i = 0; i < particleCount; i++) { particles.push(new Particle()); } // 旋转角度 let rotation = 0; // 动画循环函数 function animate() { // 清除画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 保存当前的绘图状态 ctx.save(); // 将画布的原点移动到画布中心 ctx.translate(canvas.width / 2, canvas.height / 2); // 旋转画布 ctx.rotate(rotation); // 将画布的原点移回左上角 ctx.translate(-canvas.width / 2, -canvas.height / 2); // 更新和绘制每个粒子 for (let i = 0; i < particles.length; i++) { const particle = particles[i]; particle.update(); particle.draw(); // 如果粒子的透明度小于等于 0,重新生成该粒子 if (particle.alpha <= 0) { particles[i] = new Particle(); } } // 增加旋转角度 rotation += 0.002; // 请求下一帧动画 requestAnimationFrame(animate); } // 开始动画 animate(); // 监听窗口大小变化事件,更新画布大小 window.addEventListener('resize', () => { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }); </script> </body> </html> ``` 代码解释: 1. HTML 部分:创建一个 `canvas` 元素,用于绘制 3D 旋转粒子爱心。 2. CSS 部分:设置 `body` 和 `canvas` 的样式,使 `canvas` 居中显示,并设置颜色为黑色。 3. JavaScript 部分: - 获取 `canvas` 元素和 2D 绘图上下文,并设置 `canvas` 的宽度和高度为窗口的宽度和高度。 - 定义粒子类 `Particle`,在构造函数中根据爱心曲线公式计算粒子的初始位置,同时设置粒子的速度、颜色和透明度。 - 初始化粒子数组,存储所有的粒子。 - 定义动画循环函数 `animate`,在该函数中清除画布,旋转画布,更新和绘制每个粒子,并请求下一帧动画。 - 监听窗口大小变化事件,更新 `canvas` 的大小。 额外注意的逻辑点: 1. 爱心的大小可以通过调整 `scale` 参数来改变,如果想要更大或更小的爱心,可以修改该值。 2. 粒子的数量可以通过修改 `particleCount` 变量来调整,数量越多,爱心看起来越密集,但可能会影响性能。 3. 旋转的速度可以通过修改 `rotation` 的增量值来调整,例如将 `rotation += 0.002;` 中的 `0.002` 增大或减小。 ######[AI写代码神器 | 1438点数解答 | 2025-07-05 10:16:32]
- 易语言:实现搜索通配符功能,解决搜索内容超255问题(字节豆包 | 32点数解答 | 2026-02-09 16:44:31)54
- 代码优化:“A_寻找通配符”子程序的改进之路(字节豆包 | 1248点数解答 | 2026-02-09 16:45:52)58
- 易语言:A_寻找通配符子程序完整实现及详细代码解读(字节豆包 | 1368点数解答 | 2026-02-09 16:49:12)53
- C语言:利用函数实现输入三个整数并从大到小排序输出(字节豆包 | 246点数解答 | 2024-10-22 22:06:20)303
- C语言:输入三个整数并从大到小排序输出的实现代码(字节豆包 | 305点数解答 | 2024-10-22 22:07:27)361
- C语言:输入三个整数并从大到小排序输出的实现(字节豆包 | 259点数解答 | 2024-10-25 00:53:10)249
- HTML 代码问题分析、修正及开发小心点揭秘:打造完美爱心动画 (字节豆包 | 1993点数解答 | 2025-11-02 23:37:54)104
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)362
- HTML 画布绘制爱心代码优化与注意要点揭秘(字节豆包 | 165点数解答 | 2025-03-23 15:00:43)293
- 代码实现红色爱心:结构清晰且优化建议与逻辑要点大揭秘(字节豆包 | 198点数解答 | 2025-05-01 13:24:06)233
- 深度揭秘 Oracle Workarea Size Policy:参数调优助力数据库性能飞升(阿里通义 | 372点数解答 | 2024-05-13 10:54:45)246
- 易语言代码优化:自动出售物资子程序整合与优化全解析(DeepSeek | 2181点数解答 | 2025-07-23 10:26:29)223