❤️创意网页:制作一个绚丽的烟花效果(HTML、CSS和JavaScript实现)

博主:命运之光

🌸专栏:Python星辰秘典

🐳专栏:web开发(简单好用又好看)

❤️专栏:Java经典程序设计

☀️博主的其他文章:点击进入博主的主页

前言:欢迎踏入我的Web项目专栏,一段神奇而令人陶醉的数字世界!

🌌在这里,我将带您穿越时空,揭开属于Web的奥秘。通过HTML、CSS和JavaScript的魔力,我创造了一系列令人惊叹的Web项目,它们仿佛是从梦境中涌现而出。

🌌在这个专栏中,您将遇到华丽的界面,如流星划过夜空般迷人;您将感受到动态的交互,如魔法般让您沉浸其中;您将探索响应式设计的玄妙,让您的屏幕变幻出不同的绚丽景象。

🌌无论您是一个探险家还是一位嗜血的代码巫师,这个专栏将成为您的魔法书。我将分享每个项目的秘密,解开编码的谜题,让您也能够拥有制作奇迹的力量。

🌌准备好了吗?拿起您的键盘,跟随我的指引,一起进入这个神秘而充满惊喜的数字王国。在这里,您将找到灵感的源泉,为自己创造出一段奇幻的Web之旅!

目录

简介

动态图展示

静态图展示

图片1

图片2

图片3 

技术栈

创建Canvas

JavaScript代码

粒子类

烟花类

动画循环

鼠标点击触发烟花

运行效果

项目完整代码

代码的使用方法(超简单什么都不用下载)

🍓1.打开记事本 

🍓2.将上面的源代码复制粘贴到记事本里面将文件另存为HTML文件点击保存即可

🍓3.打开html文件(大功告成(●'◡'●))

结论


简介

烟花效果是一个令人着迷的动画特效,它给网页带来了生动的视觉体验。在本文中,我们将使用HTML、CSS和JavaScript来创建一个简单但绚丽的烟花效果。我们将介绍粒子系统的概念和烟花的爆炸效果,通过调整粒子的属性和参数,使烟花效果看起来更加真实和丰富。


动态图展示


静态图展示

图片1

图片2

图片3 


技术栈

在实现这个烟花效果中,我们将使用以下技术:

  • HTML: 构建页面结构,添加Canvas元素。

  • CSS: 设置画布和背景样式。

  • JavaScript: 创建粒子类、烟花类,并实现动画效果。


创建Canvas

首先,我们需要在HTML文件中创建一个Canvas元素,用于绘制烟花的效果。我们将为这个Canvas元素添加一个唯一的ID,方便在JavaScript中引用。

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Fireworks Effect</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
      background-color: black;
    }
    canvas {
      display: block;
    }
  </style>
</head>
<body>
  <canvas id="fireworksCanvas"></canvas>
</body>
</html>

JavaScript代码

接下来,我们使用JavaScript来实现烟花效果。首先,我们需要在页面中获取Canvas元素,并设置其宽度和高度与浏览器窗口一致。

const canvas = document.getElementById('fireworksCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

粒子类

我们创建一个Particle类来表示烟花的粒子。每个粒子具有位置、颜色、半径、速度和透明度等属性。

class Particle {
  constructor(x, y, color, velocityX, velocityY) {
    this.x = x;
    this.y = y;
    this.color = color;
    this.velocityX = velocityX;
    this.velocityY = velocityY;
    this.radius = 2.5;
    this.opacity = 1;
  }

  update() {
    this.x += this.velocityX;
    this.y += this.velocityY;
    this.velocityY += 0.1;
    this.opacity -= 0.01;
  }

  draw(ctx) {
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
    ctx.fillStyle = this.color;
    ctx.globalAlpha = this.opacity;
    ctx.shadowBlur = 10;
    ctx.shadowColor = this.color;
    ctx.fill();
  }
}

烟花类

接下来,我们创建一个Firework类来表示烟花本身。每朵烟花由多个粒子组成,具有初始位置和速度。我们将在烟花类中初始化这些粒子,并在每帧中更新和绘制它们。

class Firework {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    this.particles = [];
    for (let i = 0; i < 50; i++) {
      const color = `hsl(${Math.random() * 360}, 100%, 50%)`;
      const velocityX = (Math.random() - 0.5) * 6;
      const velocityY = Math.random() * -15;
      this.particles.push(new Particle(x, y, color, velocityX, velocityY));
    }
  }

  update() {
    this.particles.forEach(particle => particle.update());
  }

  draw(ctx) {
    this.particles.forEach(particle => particle.draw(ctx));
  }
}

动画循环

最后,我们将创建一个动画循环,更新和绘制所有的烟花。

let fireworks = [];

function animate() {
  ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
  ctx.fillRect(0, 0, canvas.width, canvas.height);

  fireworks.forEach((firework, index) => {
    if (firework.particles[0].opacity <= 0) {
      fireworks.splice(index, 1);
    } else {
      firework.update();
      firework.draw(ctx);
    }
  });

  requestAnimationFrame(animate);
}

鼠标点击触发烟花

最后一步是添加鼠标点击事件监听器,每次点击鼠标时触发一朵烟花。

canvas.addEventListener('click', (event) => {
  const x = event.clientX;
  const y = event.clientY;
  fireworks.push(new Firework(x, y));
});

// 启动动画
animate();

运行效果

现在,打开你的HTML文件,你应该可以看到一个黑色的页面。在页面的任意位置点击鼠标,你将看到一个简单但绚丽的烟花效果。


项目完整代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Fireworks Effect</title>
  <style>
    body {
      margin: 0;
      overflow: hidden;
      background-color: black;
    }
    canvas {
      display: block;
    }
  </style>
</head>
<body>
  <canvas id="fireworksCanvas"></canvas>

  <script>
    const canvas = document.getElementById('fireworksCanvas');
    const ctx = canvas.getContext('2d');
    let fireworks = [];

    // 设置画布大小
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    // 监听窗口大小变化
    window.addEventListener('resize', () => {
      canvas.width = window.innerWidth;
      canvas.height = window.innerHeight;
    });

    class Particle {
      constructor(x, y, color, velocityX, velocityY) {
        this.x = x;
        this.y = y;
        this.color = color;
        this.velocityX = velocityX;
        this.velocityY = velocityY;
        this.radius = 2.5;
        this.opacity = 1;
      }

      update() {
        this.x += this.velocityX;
        this.y += this.velocityY;
        this.velocityY += 0.1;
        this.opacity -= 0.01;
      }

      draw(ctx) {
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
        ctx.fillStyle = this.color;
        ctx.globalAlpha = this.opacity;
        ctx.shadowBlur = 10;
        ctx.shadowColor = this.color;
        ctx.fill();
      }
    }

    class Firework {
      constructor(x, y) {
        this.x = x;
        this.y = y;
        this.particles = [];
        for (let i = 0; i < 50; i++) {
          const color = `hsl(${Math.random() * 360}, 100%, 50%)`;
          const velocityX = (Math.random() - 0.5) * 6;
          const velocityY = Math.random() * -15;
          this.particles.push(new Particle(x, y, color, velocityX, velocityY));
        }
      }

      update() {
        this.particles.forEach(particle => particle.update());
      }

      draw(ctx) {
        this.particles.forEach(particle => particle.draw(ctx));
      }
    }

    function animate() {
      ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
      ctx.fillRect(0, 0, canvas.width, canvas.height);

      fireworks.forEach((firework, index) => {
        if (firework.particles[0].opacity <= 0) {
          fireworks.splice(index, 1);
        } else {
          firework.update();
          firework.draw(ctx);
        }
      });

      requestAnimationFrame(animate);
    }

    // 鼠标点击触发烟花效果
    canvas.addEventListener('click', (event) => {
      const x = event.clientX;
      const y = event.clientY;
      fireworks.push(new Firework(x, y));
    });

    // 启动动画
    animate();
  </script>
</body>
</html>

代码的使用方法(超简单什么都不用下载)

🍓1.打开记事本 

🍓2.将上面的源代码复制粘贴到记事本里面将文件另存为HTML文件点击保存即可

🍓3.打开html文件(大功告成(●'◡'●))


结论

在本文中,我们介绍了如何使用HTML、CSS和JavaScript创建一个简单的烟花效果。我们实现了粒子系统,以及烟花的爆炸效果,使得烟花效果看起来更加真实和丰富。通过调整粒子的属性和参数,你可以进一步优化烟花的效果,创造出更多种类的烟花。


本章的内容就到这里了,觉得对你有帮助的话就支持一下博主把~

🌌点击下方个人名片,交流会更方便哦~
↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓

 

  • 6
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
很抱歉,我无法为您制作一个完整的网页,但是我可以提供一些基本的HTMLCSSJavaScript代码,以帮助您入门。 HTML 代码: ```html <!DOCTYPE html> <html> <head> <title>我的网页</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <header> <h1>我的网页</h1> <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">关于我们</a></li> <li><a href="#">联系我们</a></li> </ul> </nav> </header> <main> <section> <h2>欢迎来到我的网页</h2> <p>这是一个演示用的网页,用于展示HTMLCSSJavaScript的基本用法。</p> </section> <section> <h2>最新文章</h2> <article> <h3>文章标题</h3> <p>这是一篇文章的摘要,用于吸引读者的注意。</p> <a href="#">阅读全文</a> </article> <article> <h3>文章标题</h3> <p>这是一篇文章的摘要,用于吸引读者的注意。</p> <a href="#">阅读全文</a> </article> <article> <h3>文章标题</h3> <p>这是一篇文章的摘要,用于吸引读者的注意。</p> <a href="#">阅读全文</a> </article> </section> </main> <footer> <p>版权所有 © 2021 我的网页</p> </footer> <script src="script.js"></script> </body> </html> ``` CSS 代码: ```css /* Reset CSS */ * { margin: 0; padding: 0; box-sizing: border-box; } /* Body */ body { font-family: Arial, sans-serif; font-size: 16px; line-height: 1.5; color: #333; } /* Header */ header { background-color: #f2f2f2; padding: 20px; } header h1 { font-size: 36px; margin-bottom: 10px; } nav ul { list-style: none; display: flex; } nav li { margin-right: 20px; } nav a { color: #333; text-decoration: none; } nav a:hover { color: #000; } /* Main */ main { max-width: 960px; margin: 0 auto; padding: 20px; } section { margin-bottom: 40px; } section h2 { font-size: 24px; margin-bottom: 10px; } article { margin-bottom: 20px; } article h3 { font-size: 20px; margin-bottom: 10px; } article p { margin-bottom: 10px; } article a { color: #333; text-decoration: none; } article a:hover { color: #000; } /* Footer */ footer { background-color: #f2f2f2; padding: 20px; text-align: center; } ``` JavaScript 代码: ```javascript // 在这里写 JavaScript 代码 ``` 这些代码只是一个基本的框架,您可以根据自己的需求和创意,进行修改和扩展。希望这些代码能够帮助您入门。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

命运之光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值