页面烟花特效:用 canvas-confetti 庆祝客流破万

历史项目有个新需求:当客流达到整万数时,页面来一段庆祝特效。研究了一圈,最后选了 canvas-confetti,效果还不错——两侧礼花 + 中央爆炸 + 全屏贺词,持续约 43 秒。

点这里查看演示效果

实现思路

整体分三块:

  1. 全屏贺词层:半透明黑底 + 变色文字,用 jQuery fadeIn / fadeOut 控制显隐。
  2. 两侧礼花requestAnimationFrame 循环,从左右边缘向中间喷射粒子。
  3. 中央爆炸setInterval 每 2 秒触发一轮,在屏幕中央随机位置做 360° 扩散。

依赖两个 CDN 脚本:jQuery 和 canvas-confetti。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>烟花效果</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
</head>
<style>
#message {
font-size: 100px;
font-weight: bold;
color: #fff;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
z-index: 50;
background-color: rgba(0, 0, 0, 0.7);
animation: color-change 2s infinite;
}

@keyframes color-change {
0% { color: #ff3333; }
25% { color: #DC143C; }
50% { color: #FF1493; }
75% { color: #8B0000; }
100% { color: #ff3333; }
}
</style>
<body>

<div id="message" style="display: none;"></div>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/canvas-confetti@1.9.3/dist/confetti.browser.min.js"></script>

<script>
function showCelebration(number) {
const duration = 43000;
const endTime = Date.now() + duration;
const colors = ['#bb0000', '#ff00ff', '#00bbff', '#00ff00', '#ffff00', '#ff8800'];

const messageDiv = document.getElementById('message');
messageDiv.innerHTML = `热烈庆贺客流总数突破<br><br>${number}`;
$(messageDiv).fadeIn(500);

function randomInRange(min, max) {
return Math.random() * (max - min) + min;
}

function launchFireworks() {
(function frame() {
confetti({ particleCount: 6, angle: 60, spread: 55, origin: { x: 0 }, colors });
confetti({ particleCount: 6, angle: 120, spread: 55, origin: { x: 1 }, colors });
if (Date.now() < endTime) requestAnimationFrame(frame);
})();
}

function launchCentralExplosions() {
const interval = setInterval(() => {
for (let i = 0; i < 3; i++) {
setTimeout(() => {
confetti({
spread: 360, ticks: 120, gravity: 0, decay: 0.94,
startVelocity: 20,
colors: ['#ff4500', '#ff6347', '#ffd700'],
particleCount: 60, scalar: 1.2,
origin: { x: randomInRange(0.4, 0.6), y: randomInRange(0.4, 0.6) },
shapes: ['star', 'circle']
});
}, i * 200);
}
}, 2000);
setTimeout(() => clearInterval(interval), duration);
}

launchFireworks();
launchCentralExplosions();
setTimeout(() => $(messageDiv).fadeOut(500), duration);
}

showCelebration(60000000);
</script>
</body>
</html>

延伸阅读

做完这个以后,又发现一个朋友 Caleb Miller 做的几乎一比一仿真的烟花效果,也值得收藏:

https://codepen.io/MillerTime/pen/XgpNwb

Notice: 正常情况下,这里会有一个基于utteranc.es的留言系统,如果看不到,可能要想想办法才能看到。

Powered by Hexo and Hexo-theme-hiker

Copyright © 2012 - 2026 tiaobug.com All Rights Reserved.

鲁ICP备2024124237号-1