用html+css+js生成一个放烟花的页面

本文介绍了如何使用HTML构建页面结构,CSS设置样式,特别是用JavaScript和Canvas库创建动态的烟花动画效果。提供了一个简单的HTML和JS代码示例,展示了在用户点击时生成烟花粒子的效果。
摘要由CSDN通过智能技术生成
<p>要生成一个放烟花的页面,你需要使用HTML,CSS和JS。</p>
<p>HTML用来创建网页的结构,在这里可以创建一个容器来存放烟花的动画效果。</p>
<p>CSS用来设置网页的样式,比如背景颜色、字体、颜色等。</p>
<p>JS用来创建烟花的动画效果,可以通过使用JS库,例如Canvas或SVG,来实现这个效果。</p>
<p>以下是一个简单的HTML和JS代码示例:</p>
<p>```html
<!DOCTYPE html></p>
<html>
<head>
  <style>
    #fireworks {
      background-color: black;
      height: 100%;
      width: 100%;
    }
  </style>
</head>
<body>
  <div id="fireworks"></div>
  <script>
    const canvas = document.getElementById("fireworks");
    const ctx = canvas.getContext("2d");

<pre><code>let particles = [];

function draw() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);

  for (let i = 0; i &lt; particles.length; i++) {
    const particle = particles[i];

    particle.update();

    ctx.beginPath();
    ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2);
    ctx.fillStyle = particle.color;
    ctx.fill();
  }

  requestAnimationFrame(draw);
}

class Particle {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.color = &quot;white&quot;;
    this.radius = Math.random() * 2 + 1;
    this.velocity = {
      x: Math.random() * 4 - 2,
      y: Math.random() * -5 - 5,
    };
  }

  update() {
    this.x += this.velocity.x;
    this.y += this.velocity.y;
    this.radius -= 0.01;

    if (this.radius &gt; 0) {
      particles.push(new Particle(this.x, this.y));
    }
  }
}

canvas.addEventListener(&quot;click&quot;, (e) =&gt; {
  particles.push(new Particle(e.clientX, e.clientY));
});

draw();
</code></pre>
<p>  </script></p>
</body>
</html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值