3d爱心粒子代码html
时间: 2023-10-17 21:07:01 浏览: 540
以下是一个简单的 3D 爱心粒子效果的 HTML 代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>3D Heart Particles</title>
<style>
body {
margin: 0;
padding: 0;
background-color: #000;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var W = window.innerWidth;
var H = window.innerHeight;
canvas.width = W;
canvas.height = H;
var particles = [];
var particle_count = 100;
for (var i = 0; i < particle_count; i++) {
particles.push(new particle());
}
function particle() {
this.speed = {x: -1 + Math.random() * 2, y: -5 + Math.random() * 5};
if (Math.random() < 0.5) {
this.pos = {x: W / 2 - Math.random() * W, y: H / 2 - Math.random() * H};
} else {
this.pos = {x: W / 2 + Math.random() * W, y: H / 2 + Math.random() * H};
}
this.size = Math.random() * 10 + 2;
this.life = Math.random() * 15 + 10;
this.alpha = Math.random() * 0.5 + 0.5;
this.color = "#ff00ff";
}
function draw() {
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "rgba(0, 0, 0, 0.1)";
ctx.fillRect(0, 0, W, H);
ctx.globalCompositeOperation = "lighter";
for (var i = 0; i < particles.length; i++) {
var p = particles[i];
ctx.beginPath();
p.opacity = Math.round(p.life) / 100;
var gradient = ctx.createRadialGradient(p.pos.x, p.pos.y, 0, p.pos.x, p.pos.y, p.size);
gradient.addColorStop(0, "rgba(255, 255, 255, " + p.opacity + ")");
gradient.addColorStop(0.5, "rgba(255, 255, 255, " + p.opacity + ")");
gradient.addColorStop(1, "rgba(255, 255, 255, 0)");
ctx.fillStyle = gradient;
ctx.arc(p.pos.x, p.pos.y, p.size, Math.PI * 2, false);
ctx.fill();
p.pos.x += p.speed.x;
p.pos.y += p.speed.y;
p.life -= 0.3;
if (p.life < 0) {
particles[i] = new particle();
}
}
}
setInterval(draw, 30);
</script>
</body>
</html>
```
阅读全文