-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrain4.html
295 lines (272 loc) · 9.79 KB
/
rain4.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>那些年下过的大雨</title>
<meta id="viewport" name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
<link rel="stylesheet" href="lib/font/stylesheet.css">
<style>
html,body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
.container{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<a class="back" href="javascript:history.go(-1);">back</a>
<div id="jsi-rain-container" class="container"></div>
<script src="https://cdn.bootcss.com/jquery/2.2.1/jquery.slim.min.js"></script>
<script>
var RENDERER = {
INIT_RAIN_DROP_COUNT : 1500,
RAIN_DROP_COUNT : 8,
HUMAN_COUNT : 30,
COLOR : 'hsl(%hue, 20%, %luminance%)',
HUE_OFFSET : Math.PI / 1000,
LUMINANCE_OFFSET : Math.PI / 1500,
init : function(){
this.setParameters();
this.reconstructMethod();
this.createRainDrops(this.INIT_RAIN_DROP_COUNT, true);
this.createHumans();
this.render();
},
setParameters : function(){
this.$container = $('#jsi-rain-container');
this.width = this.$container.width();
this.height = this.$container.height();
this.context = $('<canvas />').attr({width : this.width, height : this.height}).appendTo(this.$container).get(0).getContext('2d');
this.rainDrops = [];
this.humans = [];
this.theta = 0;
this.phi = 0;
},
reconstructMethod : function(){
this.render = this.render.bind(this);
},
getRandomValue : function(range){
return range.min + (range.max - range.min) * Math.random();
},
createRainDrops : function(count, toInit){
for(var i = 0; i < count; i++){
this.rainDrops.push(new RAIN_DROP(this.width, this.height, toInit, this));
}
},
createHumans : function(){
for(var i = 0, length = this.HUMAN_COUNT; i < length; i++) {
this.humans.push(new HUMAN(this.width, this.height, this));
}
},
render : function(){
requestAnimationFrame(this.render);
this.color = this.COLOR.replace('%hue', 205 + 5 * Math.sin(this.phi));
this.context.fillStyle = this.color.replace('%luminance', 35 + 5 * Math.sin(this.theta));
this.context.fillRect(0, 0, this.width, this.height);
for(var i = this.rainDrops.length - 1; i >= 0; i--){
if(!this.rainDrops[i].render(this.context, false)){
this.rainDrops.splice(i, 1);
}
}
this.humans.sort(function(human1, human2){
return human1 - human2;
});
for(var i = 0, length = this.humans.length; i < length; i++){
this.humans[i].renderShadow(this.context);
}
for(var i = 0, length = this.humans.length; i < length; i++){
this.humans[i].renderSubstance(this.context);
}
for(var i = this.rainDrops.length - 1; i >= 0; i--){
if(!this.rainDrops[i].render(this.context, true)){
this.rainDrops.splice(i, 1);
}
}
this.createRainDrops(this.RAIN_DROP_COUNT, false);
this.theta += this.LUMINANCE_OFFSET;
this.theta %= Math.PI * 2;
this.phi += this.HUE_OFFSET;
this.phi %= Math.PI * 2;
}
};
var RAIN_DROP = function(width, height, toInit, renderer){
this.width = width;
this.height = height;
this.toInit = toInit;
this.renderer = renderer;
this.init();
};
RAIN_DROP.prototype = {
SCALE_RANGE : {min : 0.2, max : 1}, // 缩放比例
VELOCITY_RANGE : {min : -1.5, max : -1}, // 水平速度范围
VELOCITY_RATE : 3, // 垂直速度基值
LENGTH_RATE : 20, // 雨滴长度
ACCELARATION_RATE : 0.01, // 加速度比例
VERTICAL_OFFSET_RATE : 0.04, // 垂直偏移量
FRONT_THRESHOLD : 0.8, // 前景临界值(前面的雨滴不至于过小,后面的雨滴不至于过大)
REFLECTION_RADIUS_RATE : 0.02, // 雨滴水花半径基值
COLOR : 'rgba(255, 255, 255, 0.5)', // 雨滴颜色
RADIUS_RATE : 0.2, // 用于设置全局透明度
THRESHOLD_RATE : 0.6,
init : function(){
// 获取随机缩放比例
this.scale = this.renderer.getRandomValue(this.SCALE_RANGE);
// 雨滴长度
this.length = this.LENGTH_RATE * this.scale;
// 雨滴水平方向速度
this.vx = this.renderer.getRandomValue(this.VELOCITY_RANGE) * this.scale;
// 雨滴垂直方向速度
this.vy = this.VELOCITY_RATE * this.scale;
// 雨滴垂直方向加速度
this.ay = this.ACCELARATION_RATE * this.scale;
// 角度
this.theta = Math.atan2(this.vy, this.vx);
// 用于计算垂直方向是否出边界的偏移量
this.offset = this.height * this.VERTICAL_OFFSET_RATE;
// 雨滴水平坐标值
this.x = this.renderer.getRandomValue({min : 0, max : this.width - this.height * Math.cos(this.theta)});
// 雨滴垂直坐标值
this.y = (this.toInit ? this.renderer.getRandomValue({min : 0, max : this.height}) : 0) - this.offset;
// 水花半径
this.radius = this.length * this.REFLECTION_RADIUS_RATE;
},
render : function(context, toFront){
// 如果雨滴在前面,但是缩放比例小于规定临界值,则去掉雨滴
// 如果雨滴在后面,但是缩放比例大于规定临界值,则去掉雨滴
// 保证了在背景前面的雨滴不至于太小,在背景靠后的雨滴不至于太大
if(toFront && this.scale < this.FRONT_THRESHOLD || !toFront && this.scale >= this.FRONT_THRESHOLD){
return true;
}
context.save();
context.strokeStyle = this.COLOR;
// 如果雨滴的垂直方向坐标大于了临界值,则以水花的形式展示
// 否则以雨滴下落的方式展示
if(this.y >= this.height * (1 - (1 - this.scale) * this.THRESHOLD_RATE) - this.offset){
context.lineWidth = 3;
context.globalAlpha = (1 - this.radius / this.length / this.RADIUS_RATE) * 0.5;
context.beginPath();
context.arc(this.x, this.y, this.radius, Math.PI, Math.PI * 2, false);
context.stroke();
context.restore();
this.radius *= 1.05;
if(this.radius > this.length * this.RADIUS_RATE){
return false;
}
}else{
context.lineWidth = 1;
context.beginPath();
context.moveTo(this.x, this.y);
context.lineTo(this.x + this.length * Math.cos(this.theta), this.y + this.length * Math.sin(this.theta));
context.stroke();
context.restore();
// 通过水平速度改变水平位移
this.x += this.vx;
// 通过垂直速度改变垂直位移
this.y += this.vy;
// 通过加速度,增加垂直方向的速度
// 因此雨滴并不是沿着直线运动,而是有一定的弧度
this.vy += this.ay;
}
return true;
}
};
var HUMAN = function(width, height, renderer){
this.width = width;
this.height = height;
this.renderer = renderer;
this.init();
};
HUMAN.prototype = {
HORIZONTAL_OFFSET : 30,
VERTICAL_OFFSET_RATE_RANGE : {min : 0.05, max : 0.45},
VELOCITY_OFFSET : {min : 0.3, max : 0.6},
SHADOW_SCALE : -0.6,
init : function(){
this.setParameters(true);
},
setParameters : function(toInit){
this.direction = (Math.random() < 0.5) ? 1 : -1;
if(toInit){
this.x = this.renderer.getRandomValue({min : 0, max : this.width});
}else{
this.x = (this.direction > 0) ? -this.HORIZONTAL_OFFSET : (this.width + this.HORIZONTAL_OFFSET);
}
var position = this.renderer.getRandomValue(this.VERTICAL_OFFSET_RATE_RANGE) * this.height,
verticalPosition = this.height * this.VERTICAL_OFFSET_RATE_RANGE.max;
this.rate = 10 * (verticalPosition - position) / verticalPosition | 0;
this.y = this.height - position;
this.vx = this.renderer.getRandomValue(this.VELOCITY_OFFSET) * this.direction;
this.vxRate = (0.5 + 0.5 * (verticalPosition - position) / verticalPosition);
this.theta = 0;
},
renderSubstance : function(context){
context.save();
context.translate(this.x, this.y);
context.scale(this.direction * this.vxRate, 1 * this.vxRate);
this.renderHuman(context, 30);
context.restore();
this.theta += this.vx / 5;
this.theta %= Math.PI * 2;
this.x += this.vx * this.vxRate;
if(this.x < -this.HORIZONTAL_OFFSET && this.direction < 0 || this.x > this.width + this.HORIZONTAL_OFFSET && this.direction > 0){
this.setParameters(false);
}
},
renderShadow : function(context){
context.save();
context.translate(this.x, this.y);
context.scale(this.direction * this.vxRate, this.SHADOW_SCALE * this.vxRate);
this.renderHuman(context, 35);
context.restore();
},
renderHuman : function(context, baseColor){
var color = this.renderer.color.replace('%luminance', baseColor - this.rate);
context.fillStyle = color;
context.strokeStyle = color;
context.lineWidth = 2;
context.beginPath();
context.moveTo(-30, -60);
context.quadraticCurveTo(-20, -75, 0, -75);
context.quadraticCurveTo(20, -75, 30, -60);
context.closePath();
context.fill();
context.beginPath();
context.moveTo(0, -80);
context.lineTo(0, -35);
context.stroke();
context.beginPath();
context.arc(-10, -55, 5, 0, Math.PI * 2, false);
context.fill();
context.beginPath();
context.moveTo(-15, -50);
context.quadraticCurveTo(-20, -40, -15, -30);
context.lineTo(-5, -30);
context.quadraticCurveTo(0, -40, -5, -50);
context.closePath();
context.fill();
for(var i = -1; i <= 1; i += 2){
context.beginPath();
context.moveTo(-13, -30);
context.lineTo(-13 + 7 * Math.sin(this.theta) * i, -5);
context.lineTo(-3 + 7 * Math.sin(this.theta) * i, -5);
context.lineTo(-7 + 7 * Math.sin(this.theta) * i, -10);
context.lineTo(-7, -30);
context.closePath();
context.fill();
}
}
};
$(function(){
RENDERER.init();
});
</script>
</body>
</html>