写一个简单的烟花绽放网页
时间: 2023-04-01 20:03:44 浏览: 176
HTML5烟花绽放网页动画特效
很高兴回答你的问题。以下是一个简单的烟花绽放网页的代码示例:
<!DOCTYPE html>
<html>
<head>
<title>烟花绽放</title>
<style>
body {
background-color: black;
}
canvas {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
var canvas = document.getElementById("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ctx = canvas.getContext("2d");
function random(min, max) {
return Math.random() * (max - min) + min;
}
function Particle(x, y, radius, color, velocity) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.velocity = velocity;
this.alpha = 1;
this.draw = function() {
ctx.save();
ctx.globalAlpha = this.alpha;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, , Math.PI * 2, false);
ctx.fillStyle = this.color;
ctx.fill();
ctx.restore();
}
this.update = function() {
this.draw();
this.x += this.velocity.x;
this.y += this.velocity.y;
this.alpha -= .01;
}
}
function createParticles(x, y) {
for (var i = ; i < 30; i++) {
var particle = new Particle(x, y, random(2, 5), "hsl(" + random(, 360) + ", 50%, 50%)", {
x: random(-5, 5),
y: random(-5, 5)
});
particles.push(particle);
}
}
var particles = [];
canvas.addEventListener("click", function(event) {
createParticles(event.clientX, event.clientY);
});
function animate() {
requestAnimationFrame(animate);
ctx.clearRect(, , canvas.width, canvas.height);
for (var i = ; i < particles.length; i++) {
if (particles[i].alpha > ) {
particles[i].update();
} else {
particles.splice(i, 1);
}
}
}
animate();
</script>
</body>
</html>
希望能对你有所帮助。
阅读全文