超炫3D动态爱心粒子代码揭秘:开发中需小心处理的三大要点
# 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爱心粒子</title> <style> body { margin: 0; overflow: hidden; background: #000; } canvas { display: block; } </style> </head> <body> <canvas id="heartCanvas"></canvas> <script> const canvas = document.getElementById('heartCanvas'); const ctx = canvas.getContext('2d'); // 设置canvas尺寸为窗口大小 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 粒子数组 let particles = []; const particleCount = 1500; // 粒子数量 // 鼠标位置 let mouseX = 0; let mouseY = 0; // 爱心参数方程 function heartShape(t) { const scale = 10; const x = 16 * Math.pow(Math.sin(t), 3) * scale; const y = -(13 * Math.cos(t) - 5 * Math.cos(2*t) - 2 * Math.cos(3*t) - Math.cos(4*t)) * scale; return { x, y }; } // 粒子类 class Particle { constructor() { this.reset(); this.z = Math.random() * 400 - 200; // z坐标 } reset() { this.t = Math.random() * Math.PI * 2; // 参数t const heart = heartShape(this.t); this.x = heart.x; this.y = heart.y; this.size = Math.random() * 2 + 1; this.speed = 0.002 + Math.random() * 0.002; this.color = `hsl(${Math.random() * 60 + 300}, 100%, 50%)`; // 粉色系 this.alpha = 0.8 + Math.random() * 0.2; } update() { this.t += this.speed; if (this.t > Math.PI * 2) this.t = 0; const heart = heartShape(this.t); this.targetX = heart.x; this.targetY = heart.y; // 平滑移动到目标位置 this.x += (this.targetX - this.x) * 0.05; this.y += (this.targetY - this.y) * 0.05; // 3D旋转效果 const rotation = Date.now() * 0.001; const cos = Math.cos(rotation); const sin = Math.sin(rotation); // 应用3D变换 const x3d = this.x * cos - this.z * sin; const z3d = this.x * sin + this.z * cos; // 透视投影 const scale = 500 / (500 + z3d); this.screenX = x3d * scale + canvas.width / 2; this.screenY = this.y * scale + canvas.height / 2; this.screenSize = this.size * scale; } draw() { ctx.save(); ctx.globalAlpha = this.alpha; ctx.fillStyle = this.color; // 绘制粒子 ctx.beginPath(); ctx.arc(this.screenX, this.screenY, this.screenSize, 0, Math.PI * 2); ctx.fill(); // 鼠标交互效果 const dx = this.screenX - mouseX; const dy = this.screenY - mouseY; const distance = Math.sqrt(dx * dx + dy * dy); if (distance < 100) { ctx.beginPath(); ctx.strokeStyle = this.color; ctx.globalAlpha = (100 - distance) / 100 * 0.5; ctx.lineWidth = 1; ctx.moveTo(this.screenX, this.screenY); ctx.lineTo(mouseX, mouseY); ctx.stroke(); } ctx.restore(); } } // 初始化粒子 function initParticles() { particles = []; for (let i = 0; i < particleCount; i++) { particles.push(new Particle()); } } // 动画循环 function animate() { // 清除画布 ctx.fillStyle = 'rgba(0, 0, 0, 0.1)'; ctx.fillRect(0, 0, canvas.width, canvas.height); // 更新和绘制所有粒子 particles.forEach(particle => { particle.update(); particle.draw(); }); requestAnimationFrame(animate); } // 鼠标移动事件 canvas.addEventListener('mousemove', (e) => { mouseX = e.clientX; mouseY = e.clientY; }); // 窗口大小改变事件 window.addEventListener('resize', () => { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }); // 启动动画 initParticles(); animate(); </script> </body> </html> ``` 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 1. 粒子数量过多会导致性能下降,需要根据设备性能调整粒子数量 2. 3D变换计算中的三角函数运算可能消耗较多计算资源 3. 透视投影的参数设置不当会导致3D效果失真 ######[AI写代码神器 | 1253点数解答 | 2025-11-05 14:26:43]
- C语言:利用函数实现输入三个整数并从大到小排序输出(字节豆包 | 246点数解答 | 2024-10-22 22:06:20)295
- C语言:输入三个整数并从大到小排序输出的实现代码(字节豆包 | 305点数解答 | 2024-10-22 22:07:27)356
- C语言:输入三个整数并从大到小排序输出的实现(字节豆包 | 259点数解答 | 2024-10-25 00:53:10)245
- 工程师必知:软件系统用户文档编写九大要点揭秘(百度文心 | 350点数解答 | 2023-11-09 18:23:53)197
- CentOS下Java进程出现futex_wait_queue_me问题的处理全流程(阿里通义 | 283点数解答 | 2023-11-20 14:51:26)468
- 替雷利珠单抗致免疫相关性糖尿病:病例报道与机制探讨,为临床用药敲响警钟( | 164点数解答 | 2024-10-11 14:52:52)270
- 神秘网页链接背后:暗藏的HTML代码玄机待解!(阿里通义 | 92点数解答 | 2025-07-18 18:23:11)173
- 3D打印机步进电动机闭环控制系统:实现0.1毫米级精度与极速响应!(讯飞星火 | 744点数解答 | 2024-07-03 15:18:13)323
- 爱心:让世界更美好的积极情感力量(字节豆包 | 71点数解答 | 2025-03-01 16:58:36)283
- HTML 画布绘制爱心代码优化与注意要点揭秘(字节豆包 | 165点数解答 | 2025-03-23 15:00:43)288
- 代码实现红色爱心:结构清晰且优化建议与逻辑要点大揭秘(字节豆包 | 198点数解答 | 2025-05-01 13:24:06)229
- HTML 代码问题分析、修正及开发小心点揭秘:打造完美爱心动画 (字节豆包 | 1993点数解答 | 2025-11-02 23:37:54)99