元宵节快乐:用代码庆祝传统节日 | 南锋

南锋

南奔万里空,脱死锋镝余

元宵节快乐:用代码庆祝传统节日

明天就是元宵节了,简单弄一个祝福代码,祝大家元宵节快乐~

代码:

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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>元宵节快乐</title>
<style>
body { padding: 0; margin: 0; overflow: hidden; background-color: #000;
}
canvas { padding: 0; margin: 0; }
footer{
text-align:center;
}
footer h2{
color:#FFFF00;
font-size:12px;
}

</style>
<script type="text/javascript">
var PI = Math.PI;
var PI_2 = PI * 2;


var defaultConfig = {
duration: 2000,
// ms
delay: 0,
// ms
radius: 5,
// px
amount: 100,
// particle number
speed: 12,
gravity: 0.05,
friction: 0.96,
reduction: 0.98,
left: 0.5,
top: 0.3,
color: "#ff0000"
};


window.addEventListener("load",
function() {
try{
Canvas.canvas = document.querySelector("canvas");
Canvas.canvas.width = document.documentElement.clientWidth;
Canvas.canvas.height = document.documentElement.clientHeight - 40;
Canvas.context = Canvas.canvas.getContext("2d");
Canvas.context.fillStyle = "rgba(0, 0, 0, 0.05)";
}catch(e){
document.querySelector("h2").text = "您的浏览器暂不支持,请升级后观看!";
}
//setInterval(exupdate, 1000 / 60);
var num = 20;
for (var i = 0; i < num; i++) {
Canvas.add(new Firework({
duration: 800,
delay: 50 * i,
amount: 20,
left: 1 / num * i,
top: 0.3,
gravity: 0.5,
reduction: 1,
radius: 2,
friction: 0.9,
speed: 15,
color: "#ff6"
}));
}

Canvas.start();
},
false);

function Firework(config) {
this.setConfig(config || {});
this.particleImage = createParticleImage(this.radius, this.color);
this.diameter = this.radius * 2;
this.isActive = true;
this.fadeoutOpacity = 1;
}

Firework.prototype = {
setConfig: function(config) {
for (var key in defaultConfig) {
if (config[key] === undefined) {
this[key] = defaultConfig[key];
} else {
this[key] = config[key];
}
}
},

initParticles: function() {
this.particles = [];
var x = this.canvas.width * this.left;
var y = this.canvas.height * this.top;
var maxSpeed = (this.speed / 2) * (this.speed / 2);

while (this.particles.length < this.amount) {
var vx = (Math.random() - 0.5) * this.speed;
var vy = (Math.random() - 0.5) * this.speed;
if (vx * vx + vy * vy <= maxSpeed) {
this.particles.push(new Particle(x, y, vx, vy));
}
}
},

update: function(passed) {
if (this.isActive === false || this.started(passed) === false) return;

if (this.ended(passed)) {
this.fadeout();
return;
}
this.move();
this.render();
},

move: function() {
var particles = this.particles;
var particle;
for (var i = 0,
len = particles.length; i < len; i++) {
particle = particles[i];
particle.vx *= this.friction;
particle.vy = particle.vy * this.friction + this.gravity;
particle.x += particle.vx;
particle.y += particle.vy;
}
},

render: function() {
this.context.globalAlpha = 1;
this.renderParticles();
},

renderParticles: function() {
var diameter = this.diameter *= this.reduction;
var context = this.context;
var particles = this.particles;
var particleImage = this.particleImage;
var p;
for (var i = 0,
len = particles.length; i < len; i++) {
p = particles[i];
context.drawImage(particleImage, p.x, p.y, diameter, diameter);
}
},

started: function(passed) {
return this.delay < passed;
},

ended: function(passed) {
return this.duration + this.delay < passed;
},

fadeout: function() {
this.fadeoutOpacity -= 0.1;
if (this.fadeoutOpacity <= 0) {
this.isActive = false;
return;
}
this.move();
this.context.globalAlpha = this.fadeoutOpacity;
this.renderParticles();
}
};

function Particle(x, y, vx, vy) {
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
}

var Canvas = {
fireworks: [],

add: function(firework) {
firework.canvas = this.canvas;
firework.context = this.context;
firework.initParticles();
this.fireworks.push(firework);
},

start: function() {
this.startTime = Number(new Date());
this.update();
},

fill: function() {
this.context.globalAlpha = 1;
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
},

// main loop
update: function() {
this.fill();
var passed = new Date() - this.startTime;
var activeFireworkCount = 0;
this.fireworks.forEach(function(firework) {
if (firework.isActive) {
firework.update(passed);
activeFireworkCount++;
}
}.bind(this));

if (0 < activeFireworkCount) {
requestAnimationFrame(this.update.bind(this));
} else {
requestAnimationFrame(this.fadeout.bind(this, 20));
}
},

fadeout: function(count) {
if (count < 0) {
this.context.fillStyle = "black";
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
drawText();
return; // animation end

}
this.context.globalAlpha = 1;
this.context.fillStyle = "rgba(0, 0, 0, 0.15)";
this.fill();
requestAnimationFrame(this.fadeout.bind(this, count - 1));
}
};
function drawText(){
setInterval(exupdate, 1000 / 60);
}

if (Function.prototype.bind === undefined) {
Function.prototype.bind = function(obj) {
var slice = [].slice,
args = slice.call(arguments, 1),
self = this,
bound = function() {
return self.apply(obj || window, args.concat(slice.call(arguments)));
};
bound.prototype = this.prototype;
return bound;
};
}

function createParticleImage(radius, color) {
var size = radius * 2;
var canvas = document.createElement("canvas");
canvas.width = canvas.height = size;
var context = canvas.getContext("2d");

var gradient = context.createRadialGradient(radius, radius, 0, radius, radius, size);
gradient.addColorStop(0, "white");
gradient.addColorStop(0.1, "white");
gradient.addColorStop(0.3, color);
gradient.addColorStop(1, "rgba(0, 0, 0, 0)");

context.fillStyle = gradient;
context.beginPath();
context.arc(radius, radius, radius, 0, PI_2, true);
context.fill();

var particle = new Image();
particle.src = canvas.toDataURL();
return particle;
}


(function(w, r) {
w['r' + r] = w['r' + r] || w['webkitR' + r] || w['mozR' + r] || w['msR' + r] || w['oR' + r] ||
function(c) {
w.setTimeout(c, 1000 / 60);
};
})(window, 'equestAnimationFrame');



function exupdate() {
var canvas = document.getElementById("firework");
var cxt = canvas.getContext("2d");
var width = document.documentElement.clientWidth;
var height = document.documentElement.clientHeight;
if (Math.random() < 0.1) {
explode(Math.random() * width, Math.random() * height, randomColor());
}

cxt.clearRect(0, 0, width, height);
for (var i = particles.length - 1; i >= 0; i--) {
var p = particles[i];

p.vx *= p.deceleration;
p.vy *= p.deceleration;
p.vy += p.gravity;
p.x += p.vx;
p.y += p.vy;
p.energy *= p.deceleration;
p.a = Math.atan2(p.vy, p.vx);
p.tx = p.x + p.length * Math.cos(p.a);
p.ty = p.y + p.length * Math.sin(p.a);

if (p.energy < 0.05) {
particles.splice(i, 1);
} else {
cxt.beginPath();
cxt.fillStyle = p.color;
cxt.fillRect(p.x, p.y, p.tx - p.x, p.ty - p.y);
cxt.fill();
cxt.stroke();
cxt.closePath();
}
}
var x = parseInt(width)/2;
var y = parseInt(height)/2
cxt.font = "60px 宋体 bold";
cxt.textAlign = 'center';
cxt.fillStyle = colors[parseInt(cIndex)%25];
cxt.fillText('元宵节快乐!', x, y, 400);
if(timeTick >10){
cIndex = parseInt(cIndex) + 1;
timeTick = 0;
}
timeTick = parseInt(timeTick) + 1;
}
var colors = ["#fff","#ccc","#ff0000","#00ff00","#0000ff","#00ffff","#ffff00","#FF7F00","#FF1493","#9B30FF","#9AFF9A",
"#FFC1C1","#FFE1FF","#8B7B8B","#8B6969","#548B54","#551A8B","#8B0A50","#8B4500","#8b8b00","#008b8b","#00008b",
"#008b00","#8b0000","#666","#000"];
var cIndex = 0;
var timeTick = 0;
var particles = [];
function explode(x, y, color) {
//document.getElementById("showInfo").innerHTML = color;
var c = 400;
while (c-- >0 ) {
var p = {};
p.energy = Math.random() * 5;
var angle = Math.random() * Math.PI * 2;
var v = Math.random() * 10;
p.vx = Math.cos(angle) * v;
p.vy = Math.sin(angle) * v;
p.x = x;
p.y = y;
p.color = color;

p.deceleration = 0.95;
p.gravity = 0.05;
p.length = 4;
particles.push(p);
}
}

function randomColor() {
return "#" + parseInt(Math.random()*9) + "" + parseInt(Math.random()*9) + ""
+ parseInt(Math.random()*9) + "" + parseInt(Math.random()*9) + ""
+ parseInt(Math.random()*9) + "" + parseInt(Math.random()*9);
}
</script>
</head>

<body>
<canvas id="firework">

</canvas>
<footer>
</footer>
</body>

</html>
+