-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfireworks.html
242 lines (211 loc) · 7.83 KB
/
fireworks.html
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>烟花</title>
<style>
*{
margin: 0;
padding: 0;
}
html,body{
width: 100%;
height: 100%;
}
body{
position: relative;
margin: 0 auto;
}
canvas{
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
}
#bg{
z-index: 5;
}
#firework{
z-index: 8;
}
</style>
</head>
<body>
<canvas id="bg" width="360" height="600"></canvas>
<canvas id="firework" width="360" height="600"></canvas>
<script>
// 全局配置
const config = {
width: 360,
height: 600,
canvases: ['bg','firework'],
skyColor: 'hsla({hue}, 60%, {lightness}%, 0.2)',
fireworkTime: {min: 30, max: 60},
fireworkOpt: {
x: undefined,
y: undefined,
xEnd: undefined,
yEnd: undefined,
count: 300,
wait: undefined
}
};
// 粒子
class Particle{
constructor({x,y,size = 1,radius = 1.2} = {}){
this.x = x;
this.y = y;
this.size = size;
this.rate = Math.random(); //速度
this.angle = Math.PI * 2 * Math.random(); // 角度
// 粒子横向和纵向的速度
this.vx = radius * Math.cos(this.angle) * this.rate;
this.vy = radius * Math.sin(this.angle) * this.rate;
}
go(){
this.x += this.vx;
this.y += this.vy;
this.vy += 0.02; // 重力加速度
// 空气阻力
this.vx *= 0.98;
this.vy *= 0.98;
}
// 渲染粒子
render(ctx){
this.go();
ctx.beginPath();
ctx.arc(this.x,this.y,this.size,0,Math.PI * 2, false);
ctx.fill();
}
}
// 烟花
class Fireworks{
constructor({x,y = config.height,xEnd,yEnd,count = 300,wait} = {}){
this.x = x || config.width / 8 + Math.random() * config.width * 3 / 4;
this.y = y;
this.xEnd = xEnd || this.x;
this.yEnd = yEnd || config.width / 8 + Math.random() * config.width * 3 / 8;
this.size = 2;
this.velocity = -3;
this.opacity = 0.8;
this.hue = 360 * Math.random() | 0;
this.color = `hsla(${this.hue},80%,60%,1)`;
this.wait = wait || 30 + Math.random() * 30;
// 微粒个数
this.count = count;
this.particles = [];
this.createParticles();
this.status = 1;
}
// 创建微粒
createParticles(){
for(let i = 0; i < this.count; i++){
this.particles.push(new Particle({x:this.xEnd, y: this.yEnd}))
}
}
getSkyColor(){
const skyColor = {
lightness: this.status == 3 ? this.opacity : 0,
hue: this.hue
};
return skyColor;
}
// 升空
rise(){
this.y += this.velocity * 1;
this.velocity += 0.005; // 升空阻力
// 烟花到达目标位置开始渐隐
if(this.y - this.yEnd <= 50){
this.opacity = (this.y - this.yEnd) / 50;
}
// 到达目标位置,开始第二个状态
if(this.y <= this.yEnd){
this.status = 2;
}
}
// 渲染烟花 完成后返回false
render(ctx){
switch(this.status){
case 1: // 升空
ctx.save();
ctx.beginPath();
ctx.globalCompositeOperation = 'lighter';
ctx.globalAlpha = this.opacity;
ctx.translate(this.x, this.y);
ctx.scale(0.8,2.5); // 拉伸 小尾巴长度这里控制
ctx.translate(-this.x,-this.y);
ctx.fillStyle = this.color;
ctx.arc(this.x + Math.sin(Math.PI * 2 * Math.random()) / 1.2, this.y,this.size, 0,Math.PI*2,false);
ctx.fill();
ctx.restore();
this.rise();
return true;
break;
case 2: // 烟花消失 等待爆炸
if(--this.wait <= 0){
this.opacity = 1;
this.status = 3;
}
return true;
break;
case 3: // 爆炸 渲染烟花
ctx.save();
ctx.globalCompositeOperation = 'lighter';
ctx.globalAlpha = this.opacity;
ctx.fillStyle = this.color;
for(let i = 0; i < this.particles.length; ++i){
this.particles[i].render(ctx)
}
ctx.restore();
this.opacity -= 0.01;
return this.opacity > 0;
break;
default:
return false;
}
}
}
// 放烟花
const canvas = {
init: function(){
this.setProperty();
this.renderBg();
this.loop();
},
setProperty: function(){
this.fireworks = [];
this.width = config.width;
this.height = config.height;
this.fireworkTime = (config.fireworkTime.min + (config.fireworkTime.max - config.fireworkTime.min) * Math.random()) | 0;
this.bgCtx = document.querySelector('#bg').getContext('2d');
this.fireworkCtx = document.querySelector('#firework').getContext('2d');
},
renderBg(){
this.bgCtx.fillStyle = 'hsla(210,60%,5%,0.9)';
this.bgCtx.fillRect(0,0,this.width,this.height);
},
loop(){
requestAnimationFrame(this.loop.bind(this));
// this.fireworkCtx.clearRect(0,0,this.width,this.height);
// this.fireworkCtx.fillStyle = config.skyColor;
this.skyColor = {
lightness: 0,
hue: 210
};
this.fireworkCtx.fillStyle = config.skyColor.replace('{lightness}', 5 + this.skyColor.lightness * 15).replace('{hue}' , this.skyColor.hue);
this.fireworkCtx.fillRect(0,0,this.width,this.height);
if(--this.fireworkTime <= 0){
this.fireworks.push(new Fireworks(config.fireworkOpt));
this.fireworkTime = (config.fireworkTime.min + (config.fireworkTime.max - config.fireworkTime.min) * Math.random()) | 0;
}
for(let i = this.fireworks.length - 1; i >= 0; --i){
this.skyColor = this.skyColor.lightness >= this.fireworks[i].getSkyColor().lightness ? this.skyColor : this.fireworks[i].getSkyColor();
!this.fireworks[i].render(this.fireworkCtx) && this.fireworks.splice(i,1);
}
}
}
canvas.init();
</script>
</body>
</html>