使用canvas制作一个烟花的动画效果

209 阅读2分钟

"```markdown

使用Canvas制作烟花动画效果

引言

在Web开发中,Canvas API 提供了一种强大的方式来绘制图形和实现动画效果。本文将介绍如何使用HTML5 Canvas制作一个简单的烟花动画效果。

准备工作

首先,我们需要在HTML中创建一个Canvas元素:

<!DOCTYPE html>
<html lang=\"en\">
<head>
    <meta charset=\"UTF-8\">
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
    <title>烟花动画</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
        canvas {
            display: block;
            background: black;
        }
    </style>
</head>
<body>
    <canvas id=\"canvas\"></canvas>
    <script src=\"script.js\"></script>
</body>
</html>

JavaScript代码

script.js文件中,我们将实现烟花的动画效果:

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

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

// 烟花类
class Firework {
    constructor(x, y) {
        this.x = x; // 烟花位置
        this.y = y;
        this.particles = []; // 存储烟花粒子
        this.exploded = false; // 是否爆炸
        this.init();
    }

    init() {
        // 创建烟花粒子
        for (let i = 0; i < 100; i++) {
            this.particles.push(new Particle(this.x, this.y));
        }
    }

    update() {
        if (this.exploded) {
            this.particles.forEach(p => p.update());
        } else {
            this.y -= 4; // 烟花上升
            if (this.y <= canvas.height / 2) {
                this.exploded = true; // 到达最高点
            }
        }
    }

    draw() {
        if (this.exploded) {
            this.particles.forEach(p => p.draw());
        } else {
            ctx.fillStyle = 'white';
            ctx.beginPath();
            ctx.arc(this.x, this.y, 5, 0, Math.PI * 2);
            ctx.fill();
        }
    }
}

// 粒子类
class Particle {
    constructor(x, y) {
        this.x = x;
        this.y = y;
        this.size = Math.random() * 5 + 1; // 粒子大小
        this.speedX = Math.random() * 6 - 3; // X方向速度
        this.speedY = Math.random() * 6 - 3; // Y方向速度
        this.alpha = 1; // 透明度
    }

    update() {
        this.x += this.speedX;
        this.y += this.speedY;
        this.alpha -= 0.02; // 渐隐
    }

    draw() {
        ctx.save();
        ctx.globalAlpha = this.alpha;
        ctx.fillStyle = 'hsl(' + Math.random() * 360 + ', 100%, 50%)';
        ctx.beginPath();
        ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
        ctx.fill();
        ctx.restore();
    }
}

// 主动画循环
let fireworks = [];
function animate() {
    ctx.clearRect(0, 0, canvas.width, canvas.height); // 清除画布
    fireworks.forEach((firework, index) => {
        firework.update();
        firework.draw();
        if (firework.exploded && firework.particles.every(p => p.alpha <= 0)) {
            fireworks.splice(index, 1); // 移除已完成的烟花
        }
    });
    requestAnimationFrame(animate);
}

// 生成烟花
setInterval(() => {
    const x = Math.random() * canvas.width;
    const y = canvas.height;
    fireworks.push(new Firework(x, y));
}, 1000);

animate();

代码解释

  1. Canvas设置:通过getContext('2d')获取2D上下文,设置Canvas的宽高为窗口的宽高。
  2. 烟花类:创建烟花的位置和粒子,控制烟花的上升和爆炸。
  3. 粒子类:定义颗粒的运动和渐隐效果。
  4. 动画循环:使用requestAnimationFrame来不断更新和绘制烟花效果。
  5. 生成烟花:每隔一秒随机生成一个新的烟花。

结束语

通过以上代码,我们成功制作了一个简单的烟花动画效果。你可以根据需要调整粒子数量、颜色和生成频率,创造出不同的视觉效果。尽情享受你的编程乐趣吧!