html爱心粒子特效高级代码。
时间: 2024-11-29 08:14:08 浏览: 64
HTML爱心粒子特效通常是通过CSS和JavaScript结合实现动态效果的。以下是一个简单的例子,利用CSS3动画和HTML5 canvas创建爱心粒子:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<style>
#canvas {
border: 1px solid #ccc;
background-color: #f1f1f1;
}
.particle {
position: absolute;
animation: heartBounce 2s infinite;
}
@keyframes heartBounce {
0% {transform: scale(1); opacity: 1;}
50% {transform: scale(1.2) rotate(-20deg); opacity: .8;}
100% {transform: scale(1); opacity: 1;}
}
</style>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script>
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
function Particle(x, y, dx, dy, color) {
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.color = color;
// 粒子大小和形状
this.radius = Math.random() * 10 + 5;
this.size = 2 * this.radius;
}
Particle.prototype.draw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2);
ctx.fillStyle = this.color;
ctx.fill();
};
setInterval(function() {
var particles = [];
for (var i = 0; i < 200; i++) {
var x = Math.random() * canvas.width;
var y = Math.random() * canvas.height;
var dx = Math.random() - 0.5;
var dy = Math.random() - 0.5;
var color = 'hsl('+(Math.random()*360)+', 70%, 50%)';
particles.push(new Particle(x, y, dx, dy, color));
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
particles.forEach(function(particle) {
particle.draw();
particle.x += particle.dx;
particle.y += particle.dy;
if (particle.x > canvas.width + particle.size || particle.x < -particle.size ||
particle.y > canvas.height + particle.size || particle.y < -particle.size) {
particle.x = Math.random() * canvas.width;
particle.y = Math.random() * canvas.height;
particle.dx = Math.random() - 0.5;
particle.dy = Math.random() - 0.5;
}
});
}, 30);
</script>
</body>
</html>
```
这个示例创建了200个随机颜色的心形粒子,它们在画布上移动并在达到边缘时反弹。这只是一个基础版本,你可以进一步添加更复杂的动画、交互或者其他自定义元素。
阅读全文