返回

在龙年大吉之际,用JavaScript绽放璀璨烟花

前端

伴随新年的临近,我们不禁期盼着一场视觉盛宴,而烟花的绚烂便是不可错过的亮点。在这龙年伊始的吉日,让我们用JavaScript和Canvas API在数字世界中绽放一簇璀璨的烟花,同时点亮「龙年大吉」的祝福语。

让我们踏上这场用代码创造烟花奇观之旅吧!

1. 创建画布和绘制背景

我们首先创建一个元素并获取其上下文。画布将作为烟花绽放的舞台。为了营造一个充满节日气氛的背景,我们可以用渐变填充画布:

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

// 创建一个渐变背景
const gradient = ctx.createLinearGradient(0, 0, 0, canvas.height);
gradient.addColorStop(0, "#000");
gradient.addColorStop(1, "#222");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);

2. 定义烟花粒子

烟花是由无数微小的粒子组成的。我们需要创建一个Particle类来表示这些粒子:

class Particle {
  constructor(x, y, vx, vy, radius, color) {
    this.x = x;
    this.y = y;
    this.vx = vx;
    this.vy = vy;
    this.radius = radius;
    this.color = color;
    this.alpha = 1;
  }

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

  update() {
    this.x += this.vx;
    this.y += this.vy;
    this.alpha -= 0.01;
  }
}

3. 创建烟花发射器

烟花发射器负责发射粒子,创造出烟花的效果。我们需要创建一个Firework类来表示发射器:

class Firework {
  constructor(x, y, vx, vy, radius, color) {
    this.x = x;
    this.y = y;
    this.vx = vx;
    this.vy = vy;
    this.radius = radius;
    this.color = color;
    this.particles = [];
    this.exploded = false;
  }

  draw() {
    // 绘制发射器
    ctx.fillStyle = this.color;
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
    ctx.fill();

    // 绘制粒子
    for (let i = 0; i < this.particles.length; i++) {
      this.particles[i].draw();
    }
  }

  update() {
    this.x += this.vx;
    this.y += this.vy;

    // 如果发射器超出画布,则将其移除
    if (this.x < 0 || this.x > canvas.width || this.y < 0 || this.y > canvas.height) {
      fireworks.splice(fireworks.indexOf(this), 1);
      return;
    }

    // 如果发射器没有爆炸,则继续移动
    if (!this.exploded) {
      return;
    }

    // 如果发射器已爆炸,则更新粒子
    for (let i = 0; i < this.particles.length; i++) {
      this.particles[i].update();
    }
  }

  explode() {
    this.exploded = true;

    // 创建粒子
    for (let i = 0; i < 100; i++) {
      const particle = new Particle(
        this.x,
        this.y,
        Math.random() * 5 - 2.5,
        Math.random() * 5 - 2.5,
        Math.random() * 2 + 1,
        this.color
      );
      this.particles.push(particle);
    }
  }
}

4. 创建烟花显示

有了烟花粒子、发射器和画布之后,我们就可以创建烟花显示了:

const fireworks = [];
const particleColors = ["#FF0000", "#FF7F00", "#FFFF00", "#00FF00", "#00FFFF", "#0000FF"];

function createFirework() {
  const x = Math.random() * canvas.width;
  const y = canvas.height;
  const vx = 0;
  const vy = -Math.random() * 2 - 1;
  const radius = 2;
  const color = particleColors[Math.floor(Math.random() * particleColors.length)];

  const firework = new Firework(x, y, vx, vy, radius, color);
  fireworks.push(firework);
}

function updateFireworks() {
  for (let i = 0; i < fireworks.length; i++) {
    fireworks[i].update();

    // 如果发射器已爆炸,则将其移除
    if (fireworks[i].exploded && fireworks[i].particles.length === 0) {
      fireworks.splice(i, 1);
      i--;
    }
  }
}

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

  for (let i = 0; i < fireworks.length; i++) {
    fireworks[i].draw();
  }

  requestAnimationFrame(drawFireworks);
}

createFirework();
drawFireworks();
setInterval(createFirework, 300);

5. 显示「龙年大吉」文字

现在,我们来将「龙年大吉」的文字添加到烟花显示中。我们将使用Canvas API的fillText方法:

function drawText() {
  ctx.fillStyle = "#FFD700";
  ctx.font = "bold 60px Arial";
  ctx.textAlign = "center";
  ctx.fillText("龙年大吉", canvas.width / 2, canvas.height / 2);
}
drawText();

运行代码,你将看到一个绚烂的烟花在画布上绽放,同时逐渐显现出「龙年大吉」的祝福语。快来让这个烟花效果为你的龙年春节增添一份欢乐吧!