-
Notifications
You must be signed in to change notification settings - Fork 5
/
biaobai.html
386 lines (331 loc) · 12.3 KB
/
biaobai.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> love for Feb.14 2015 </title>
<style type="text/css">
body {margin:0; padding:0; wdith:100%; height: 100%}
canvas {display:block; background-color:#000}
</style>
</head>
<body bgcolor='#000'>
<script src='//code.jquery.com/jquery.js'></script>
<script>
/*
vector.js
常用二维三维矢量操作view-source:http://zichi.date/2015/love-on-20150214/
@author zichi
*/
/* 二维矢量类 */
function Vector2(x, y) {
this.x = x || 0;
this.y = y || 0;
}
Vector2.prototype.reset = function(x, y) {
this.x = x || this.x;
this.y = y || this.y;
};
Vector2.prototype.scale = function(s) {
this.x *= s;
this.y *= s;
};
Vector2.prototype.clone = function() {
return new Vector2(this.x, this.y);
};
Vector2.prototype.add = function(v) {
this.x += v.x;
this.y += v.y;
};
Vector2.prototype.addNew = function(v) {
return new Vector2(this.x + v.x, this.y + v.y);
};
Vector2.prototype.minus = function(v) {
this.x -= v.x;
this.y -= v.y;
};
Vector2.prototype.minusNew = function(v) {
return new Vector2(this.x - v.x, this.y - v.y);
};
Vector2.prototype.getMid = function(v) {
return new Vector2((this.x + v.x) / 2, (this.y + v.y) / 2);
};
// 向量点积得到夹角cos值,大于0为0~90度
Vector2.prototype.dot = function(v) {
return this.x * v.x + this.y * v.y;
};
Vector2.prototype.length = function() {
return Math.sqrt(this.sqrLength());
};
Vector2.prototype.sqrLength = function() {
return this.x * this.x + this.y * this.y;
};
Vector2.prototype.getDistance = function(v) {
var dis = (this.x - v.x) * (this.x - v.x) + (this.y - v.y) * (this.y - v.y);
return Math.sqrt(dis);
};
// 标准化,单位长度为1
Vector2.prototype.normalize = function() {
var inv = 1 / this.length();
return new Vector2(this.x * inv, this.y * inv);
};
Vector2.prototype.getAngle = function() {
return Math.atan2(this.y, this.x);
};
/* 三维矢量类 */
function Vector3(x, y, z) {
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
}
Vector3.prototype.scale = function(s) {
this.x *= s;
this.y *= s;
this.z *= s;
};
Vector3.prototype.reset = function(x, y, z) {
this.x = x || this.x;
this.y = y || this.y;
this.z = z || this.z;
};
Vector3.prototype.clone = function() {
return new Vector3(this.x, this.y, this.z);
};
Vector3.prototype.add = function(v) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
};
Vector3.prototype.addNew = function(v) {
return new Vector3(this.x + v.x, this.y + v.y, this.z + v.z);
};
Vector3.prototype.minus = function(v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
};
Vector3.prototype.minusNew = function(v) {
return new Vector3(this.x - v.x, this.y - v.y, this.z - v.z);
};
// 向量点积得到夹角cos值,大于0为0~90度
Vector3.prototype.dot = function(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
};
Vector3.prototype.length = function() {
return Math.sqrt(this.sqrLength());
};
Vector3.prototype.sqrLength = function() {
return this.x * this.x + this.y * this.y + this.z * this.z;
};
Vector3.prototype.getDistance = function(v) {
var dis = (this.x - v.x) * (this.x - v.x) + (this.y - v.y) * (this.y - v.y) + (this.z - v.z) * (this.z - v.z);
return Math.sqrt(dis);
};
// 标准化,单位长度为1
Vector3.prototype.normalize = function() {
var inv = 1 / this.length();
return new Vector3(this.x * inv, this.y * inv, this.z * inv);
}
// 矢量旋转
Vector3.prototype.rotateX = function(angleX) {
var cosx = Math.cos(angleX);
var sinx = Math.sin(angleX);
var y1 = this.y * cosx - this.z * sinx;
var z1 = this.y * sinx + this.z * cosx;
this.y = y1;
this.z = z1;
};
Vector3.prototype.rotateY = function(angleY) {
var cosy = Math.cos(angleY);
var siny = Math.sin(angleY);
var x1 = this.z * siny + this.x * cosy;
var z1 = this.z * cosy - this.x * siny;
this.x = x1;
this.z = z1;
};
Vector3.prototype.rotateP = function(v, angleP) {
var c = Math.cos(angleP);
var s = Math.sin(angleP);
// v为旋转轴单位向量
var x = v.x;
var y = v.y;
var z = v.z;
var new_x = (x * x * (1 - c) + c) * this.x + (x * y * (1 - c) - z * s) * this.y + (x * z * (1 - c) + y * s) * this.z;
var new_y = (y * x * (1 - c) + z * s) * this.x + (y * y * (1 - c) + c) * this.y + (y * z * (1 - c) - x * s) * this.z;
var new_z = (x * z * (1 - c) - y * s) * this.x + (y * z * (1 - c) + x * s) * this.y + (z * z * (1 - c) + c) * this.z;
this.reset(new_x, new_y, new_z);
};
</script>
<script>
window.onload = function() {
function Ball(v, current, r, g, b, a) {
this.pos2 = v; // 目标位置
this.now = current;
this.r = r;
this.g = g;
this.b = b;
this.a = a;
this.state = 1;
}
Ball.prototype.draw = function() {
ctx.beginPath();
ctx.fillStyle = 'rgba(' + this.r + ',' + this.g + ',' + this.b + ',' + this.a +')';
ctx.arc(this.now.x, this.now.y, 1, 0, Math.PI * 2, true);
ctx.fill();
}
var stage = {
balls: [], //
// balls[0] I
// balls[1] love
// balls[2] 慧
// balls[3] love.png
// balls[4] forever
// balls[5] 签名
index: +new Date,
init: function() {
// ctx.globalCompositeOperation = 'lighter'
for(var i = 0; i < 6; i++)
this.balls[i] = [];
window.canvas = document.createElement('canvas');
document.body.appendChild(window.canvas);
canvas.height = window.height = window.innerHeight;
canvas.width = window.width = window.innerWidth;
window.ctx = canvas.getContext('2d');
},
putText: function() {
var ocanvas = document.createElement('canvas');
// document.body.appendChild(ocanvas);
ocanvas.height = window.innerHeight;
ocanvas.width = window.innerWidth;
var octx = ocanvas.getContext('2d');
octx.font = "100px 微软雅黑"
octx.textAlign = "center";
octx.textBaseline = "middle";
octx.fillStyle = 'rgba(117,74,14,1)';
octx.fillText('I', width/2+150, height/2);
this.getTextPoints(octx, 0);
octx.clearRect(0, 0, width, height)
octx.font = "100px 微软雅黑"
octx.textAlign = "center";
octx.textBaseline = "middle";
octx.fillStyle = 'rgba(166,74,79,1)';
octx.fillText('love', width/2, height/2);
this.getTextPoints(octx, 1);
octx.clearRect(0, 0, width, height)
octx.font = "120px 微软雅黑"
octx.textAlign = "center";
octx.textBaseline = "middle";
octx.fillStyle = 'rgba(142,101,2,1)';
octx.fillText('慧', width/2 - 180, height/2 );
this.getTextPoints(octx, 2);
octx.clearRect(0, 0, width, height)
octx.font = "120px 微软雅黑"
octx.textAlign = "center";
octx.textBaseline = "middle";
octx.fillStyle = 'rgba(152,52,13,1)';
octx.fillText('4ever', width/2+50, height/2+10);
this.getTextPoints(octx, 4);
octx.clearRect(0, 0, width, height)
octx.font = "20px 微软雅黑"
// octx.font = "bold 12px serif"
octx.textAlign = "center";
octx.textBaseline = "middle";
octx.fillStyle = 'rgba(166,74,79,1)';
octx.fillText('————— 2017.2.29 by suping', width/2+70, height/2+90);
this.getTextPoints(octx, 5);
octx.clearRect(0, 0, width, height)
var img = new Image();
img.src = "http://zichi.date/2015/love-on-20150214/img/love.png";
// var img = document.getElementById('img');
var that = this;
img.onload = function () {
octx.drawImage(img, width/2 -150 - 185, height/2-40);
that.getTextPoints(octx, 3);
}
// this.balls.sort(function (a, b) {
// return a.pos2.x > b.pos2.x ? 1 : 0;
// });
},
draw: function() {
ctx.clearRect(0, 0, width, height);
for(var i = 0; i < 6; i++) {
if(i === 3 && +new Date - this.index < 6000) continue;
if(i === 4 && +new Date - this.index < 7000) continue;
if(i === 5 && +new Date - this.index < 10000) continue;
for(var j = 0, len = this.balls[i].length; j < len; j++) {
this.balls[i][j].draw();
}
for(var j = 0; j < this.balls[i].length; j++) {
if(i === 1 && this.balls[i][j].now.y > height)
this.balls[i].splice(j, 1);
}
}
},
update: function() {
for(var i = 0; i < 6; i++) {
if(i === 3 && +new Date - this.index < 6000) continue;
if(i === 4 && +new Date - this.index < 7000) continue;
if(i === 5 && +new Date - this.index < 10000) continue;
for(var j = 0, len = this.balls[i].length; j < len; j++) {
var b = this.balls[i][j];
var addV = b.pos2.minusNew(b.now);
addV.scale(0.1);
b.now.add(addV);
}
}
// love
if(+new Date - this.index > 3000) {
for(var i = 0, len = this.balls[1].length; i < len; i++) {
var b = this.balls[1][i];
if(b.state === 1) {
b.state = 2;
b.pos2 = new Vector2(Math.random() * width, height + 1)
}
}
}
// I
if(+new Date - this.index > 4000) {
for(var i = 0, len = this.balls[0].length; i < len; i++) {
var b = this.balls[0][i];
if(b.state === 1) {
b.state = 2;
b.pos2 = new Vector2(b.pos2.x - 500, b.pos2.y)
}
}
}
},
getTextPoints: function(c, id) {
var array = this.balls[id];
var num = 0;
var imgData = c.getImageData(0, 0, width, height);
for(var i = 0; i < imgData.width; i+=3)
for(var j = 0; j < imgData.height; j+=3) {
var index = (i + j * imgData.width) * 4 + 3;
if(imgData.data[index]) {
var r = imgData.data[index - 3];
var g = imgData.data[index - 2];
var b = imgData.data[index - 1];
var a = imgData.data[index];
var current = new Vector2(width * Math.random(), height * Math.random());
if(id !== 3 && id !== 4 && id !==5 )
array.push(new Ball(new Vector2(i, j), current, r, g, b, a));
else if(id === 3 || id === 4) {
// console.log(1)
cc = new Vector2(Math.random() * width, height + 1);
array.push(new Ball(new Vector2(i, j), cc, r, g, b, a));
} else {
array.push(new Ball(new Vector2(i, j), new Vector2(i + Math.random() * 20 - 10 , j + Math.random() * 20 - 10 ), r, g, b, a));
}
}
}
}
};
stage.init();
stage.putText();
setInterval(function() {
stage.draw();
stage.update();
}, 1000/60)
}
</script>
</body>
</html>