用html写一段烟花效果
时间: 2023-03-13 18:14:26 浏览: 88
我可以给你一个简单的示例:<html>
<head>
<title>烟花效果</title><style>
body{
background-color: #000000;
}
</style><script type="text/javascript">var canvas;
var ctx;
var particles = [];window.onload = function(){
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
for(var i=0; i<50; i++){
particles.push(new Particle());
}
setInterval(update, 1000/60);
}function update(){
ctx.clearRect(0,0,canvas.width,canvas.height);
for(var i=0; i<particles.length; i++){
var p = particles[i];
p.update();
p.draw();
}
}function Particle(){
this.x = Math.random()*canvas.width;
this.y = canvas.height+Math.random()*100;
this.vx = Math.random()*2-1;
this.vy = -5 + Math.random()*4;
this.radius = Math.random()*2+1;
this.color = {
r: Math.round(Math.random()*255),
g: Math.round(Math.random()*255),
b: Math.round(Math.random()*255)
}
this.update = function(){
this.x += this.vx;
this.y += this.vy;
if(this.y <= 0){
this.x = Math.random()*canvas.width;
this.y = canvas.height+Math.random()*100;
}
}
this.draw = function(){
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2, false);
ctx.fillStyle = "rgb("+this.color.r+","+this.color.g+","+this.color.b+")";
ctx.fill();
}
}</script></head><body>
<canvas id="canvas"></canvas>
</body></html>
阅读全文