HTML3D粒子代碼是一種通過在網頁中添加3D粒子效果的方法。它可以增加網頁的動感和視覺效果,吸引用戶的注意力。要添加HTML3D粒子效果,可以使用以下的代碼:
<!DOCTYPE html><html><head><title>HTML3D粒子效果</title><style>body { background-color: black; } canvas { position: absolute; top: 0; left: 0; z-index: -1; } </style></head><body><canvas id="myCanvas"></canvas><script>var canvas = document.getElementById("myCanvas"); canvas.width = window.innerWidth; canvas.height = window.innerHeight; var ctx = canvas.getContext("2d"); var particles = []; var particleCount = 200; var particleColors = [ "#FFD700", //金色 "#B22222", //深紅色 "#228B22", //森林綠 "#1E90FF", //深藍色 ]; function Particle() { this.radius = Math.random() * 8 + 2; this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height / 3 - canvas.height / 5; this.vx = Math.random() * 2 - 1; this.vy = Math.random() * 2 - 1; this.color = particleColors[Math.floor(Math.random() * particleColors.length)]; } Particle.prototype.draw = function() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI); ctx.fillStyle = this.color; ctx.fill(); } Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; if (this.x< -this.radius || this.x >canvas.width + this.radius || this.y< -this.radius * 2 || this.y >canvas.height - this.radius) { this.x = Math.random() * canvas.width; this.y = Math.random() * canvas.height / 5 - canvas.height / 5; } } function createParticles() { for (var i = 0; i< particleCount; i++) { var p = new Particle(); particles.push(p); } } function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (var i = 0; i< particles.length; i++) { particles[i].draw(); } } function update() { for (var i = 0; i< particles.length; i++) { particles[i].update(); } } createParticles(); setInterval(function() { draw(); update(); }, 30); </script></body></html>以上代碼使用了HTML5的canvas標簽來創建粒子效果,使用了JavaScript來控制粒子的運動和顏色??梢酝ㄟ^修改粒子數量、粒子大小和顏色等參數來調整HTML3D粒子效果的表現。將代碼復制到HTML文件中,即可在瀏覽器中展示出效果。