html动态粒子爱心代码
时间: 2024-05-21 15:09:15 浏览: 195
HTML动态粒子爱心代码是一种基于HTML、CSS和JavaScript技术实现的动态效果,它通过利用JavaScript粒子引擎技术,将一些彩色的小圆点组合成了一个可爱的爱心形状,让它随着鼠标的移动而动态地漂浮、旋转、变形等,非常具有观赏性。
下面是一个HTML动态粒子爱心代码示例,你可以在浏览器中打开查看效果:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>HTML动态粒子爱心代码</title>
<style>
canvas {
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var particles = [];
for (var i = 0; i < 100; i++) {
particles.push(new create_particle());
}
function create_particle() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.vx = Math.random() * 10 - 5;
this.vy = Math.random() * 10 - 5;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.fillStyle = "red";
for (var i = 0; i < particles.length; i++) {
var p = particles[i];
ctx.moveTo(p.x, p.y);
ctx.arc(p.x, p.y, 5, 0, Math.PI * 2, true);
p.x += p.vx;
p.y += p.vy;
if (p.x < -50) p.x = canvas.width + 50;
if (p.y < -50) p.y = canvas.height + 50;
if (p.x > canvas.width + 50) p.x = -50;
if (p.y > canvas.height + 50) p.y = -50;
}
ctx.fill();
requestAnimationFrame(draw);
}
draw();
</script>
</body>
</html>
```
阅读全文