3d粒子爱心代码html源码
时间: 2024-05-18 20:09:19 浏览: 457
html+css3(canvas画布)实现爱心源码,html文件打开即可
以下是一个简单的3D粒子爱心代码HTML源码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>3D粒子爱心效果</title>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: #000;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var W = canvas.width = window.innerWidth;
var H = canvas.height = window.innerHeight;
var particleCount = 200;
var particles = [];
function Particle() {
this.x = Math.random() * W;
this.y = Math.random() * H;
this.vx = Math.random() * 20 - 10;
this.vy = Math.random() * 20 - 10;
this.gravity = 0.3;
this.alpha = Math.random();
this.color = 'rgb(' + Math.floor(Math.random() * 255) + ',' + Math.floor(Math.random() * 255) + ',' + Math.floor(Math.random() * 255) + ')';
}
Particle.prototype.draw = function() {
ctx.globalAlpha = this.alpha;
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, 5, 0, Math.PI * 2, false);
ctx.fill();
};
function init() {
for (var i = 0; i < particleCount; i++) {
particles.push(new Particle());
}
}
function update() {
ctx.clearRect(0, 0, W, H);
for (var i = 0; i < particleCount; i++) {
var p = particles[i];
p.vy += p.gravity;
p.x += p.vx;
p.y += p.vy;
if (p.x > W || p.x < 0 || p.y > H || p.y < 0) {
particles[i] = new Particle();
}
p.draw();
}
}
init();
setInterval(update, 1000 / 60);
</script>
</body>
</html>
```
这个代码使用了HTML5的Canvas标签和JavaScript来实现3D粒子爱心效果。它通过在Canvas上绘制大量的带有随机颜色和透明度的小圆点来模拟出一个3D的爱心效果。通过改变圆点的位置和速度,可以让它们在屏幕上自由运动,从而创造出动态的效果。
阅读全文