纯html生日祝福含有烟花
时间: 2023-11-17 11:04:09 浏览: 157
可以使用HTML5的canvas标签和JavaScript来实现纯HTML生日祝福含有烟花的效果。以下是一个简单的例子:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>生日祝福</title>
<style>
canvas {
position: absolute;
top: 0;
left: 0;
z-index: -1;
}
</style>
</head>
<body>
<h1>生日快乐!</h1>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ctx = canvas.getContext("2d");
var particles = [];
var particleCount = 100;
var particleSpeed = 2;
var particleSize = 3;
var colors = ["#f44336", "#e91e63", "#9c27b0", "#673ab7", "#3f51b5", "#2196f3", "#03a9f4", "#00bcd4", "#009688", "#4CAF50", "#8BC34A", "#CDDC39", "#FFEB3B", "#FFC107", "#FF9800", "#FF5722", "#795548"];
var mouseX = canvas.width / 2;
var mouseY = canvas.height / 2;
function Particle(x, y) {
this.x = x;
this.y = y;
this.speed = particleSpeed;
this.angle = Math.random() * 360;
this.color = colors[Math.floor(Math.random() * colors.length)];
this.size = Math.floor(Math.random() * particleSize) + 1;
this.vx = Math.cos(this.angle) * this.speed;
this.vy = Math.sin(this.angle) * this.speed;
this.gravity = 0.1;
this.opacity = Math.random();
this.update = function() {
this.x += this.vx;
this.y += this.vy;
this.vy += this.gravity;
this.opacity -= 0.005;
if (this.opacity < 0) {
this.opacity = 0;
}
};
this.draw = function() {
ctx.globalAlpha = this.opacity;
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
};
}
function createParticles() {
for (var i = 0; i < particleCount; i++) {
var particle = new Particle(mouseX, mouseY);
particles.push(particle);
}
}
function updateParticles() {
for (var i = 0; i < particles.length; i++) {
var particle = particles[i];
particle.update();
particle.draw();
if (particle.opacity <= 0) {
particles.splice(i, 1);
}
}
}
function loop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
createParticles();
updateParticles();
requestAnimationFrame(loop);
}
canvas.addEventListener("mousemove", function(event) {
mouseX = event.clientX;
mouseY = event.clientY;
});
loop();
</script>
</body>
</html>
```
你可以将上述代码复制到一个HTML文件中并在浏览器中打开,就可以看到一个含有烟花的生日祝福页面了。
阅读全文