原生js 10个小球碰撞特效
感觉这个作者写得挺牛逼的,一点注释没有,还是挺难看懂的。 下面是源码
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
<!doctype html>
<html>
<head>
<title>10个小球碰撞 js</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style type="text/css">
body {
margin: 0;
padding: 0;
text-align: center;
}
#screen {
width: 800px;
height: 640px;
position: relative;
background: #ccccff;
margin: 0 auto;
vertical-align: bottom
}
#inner {
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
}
#screen p {
color: white;
font: bold 14px;
}
#inner div{
border-radius: 50%;
}
.one {
background: #FFF228;
background-size: 100% auto;
}
.two {
background: #00008B;
background-size: auto 100%;
}
.three {
background: #008000;
background-size: auto 100%;
}
.four {
background: #212121;
background-size: auto 100%;
}
.five {
background: #A4E9C1;
background-size: auto 100%;
}
.six {
background: #C12E2A;
background-size: auto 100%;
}
.seven {
background: #B4D100;
background-size: auto 100%;
}
.eight {
background: #577EB2;
background-size: auto 100%;
}
.nine {
background: #A94442;
background-size: auto 100%;
}
.ten {
background: #ADADAD;
background-size: auto 100%;
}
</style>
</head>
<body>
<div id="screen">
<p>hi test it!</p>
<div id="inner">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
<div class="four"></div>
<div class="five"></div>
<div class="six"></div>
<div class="seven"></div>
<div class="eight"></div>
<div class="nine"></div>
<div class="ten"></div>
</div>
</div>
<input type="button" id="start" value="start">
<input type="button" id="stop" value="stop">
<br><br><br>
<script type="text/javascript">
var getFlag = function(id) {
return document.getElementById(id);
}
var extend = function(des, src) {
for(p in src) {
des[p] = src[p];
}
return des;
}
var clss = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'];
var Ball = function(diameter, classn) {
var ball = document.createElement("div");
ball.className = classn;
with(ball.style) {
width = height = diameter + 'px';
position = 'absolute';
}
return ball;
}
var Screen = function(cid, config) {
var self = this;
if(!(self instanceof Screen)) {
return new Screen(cid, config)
}
config = extend(Screen.Config, config)
self.container = getFlag(cid);
self.ballsnum = config.ballsnum;
self.diameter = 80;
self.radius = self.diameter / 2;
self.spring = config.spring;
self.bounce = config.bounce;
self.gravity = config.gravity;
self.balls = [];
self.timer = null;
self.L_bound = 0;
self.R_bound = self.container.clientWidth;
self.T_bound = 0;
self.B_bound = self.container.clientHeight;
};
Screen.Config = {
ballsnum: 10,
spring: 0.8,
bounce: -0.9,
gravity: 0.05
};
Screen.prototype = {
initialize: function() {
var self = this;
self.createBalls();
self.timer = setInterval(function() {
self.hitBalls()
}, 30)
},
createBalls: function() {
var self = this,
num = self.ballsnum;
var frag = document.createDocumentFragment();
for(i = 0; i < num; i++) {
var ball = new Ball(self.diameter, clss[i]);
ball.diameter = self.diameter;
ball.radius = self.radius;
ball.style.left = (Math.random() * self.R_bound) + 'px';
ball.style.top = (Math.random() * self.B_bound) + 'px';
ball.vx = Math.random() * 6 - 3;
ball.vy = Math.random() * 6 - 3;
frag.appendChild(ball);
self.balls[i] = ball;
}
self.container.appendChild(frag);
},
hitBalls: function() {
var self = this,
num = self.ballsnum,
balls = self.balls;
for(i = 0; i < num - 1; i++) {
var ball1 = self.balls[i];
ball1.x = ball1.offsetLeft + ball1.radius;
ball1.y = ball1.offsetTop + ball1.radius;
for(j = i + 1; j < num; j++) {
var ball2 = self.balls[j];
ball2.x = ball2.offsetLeft + ball2.radius;
ball2.y = ball2.offsetTop + ball2.radius;
dx = ball2.x - ball1.x;
dy = ball2.y - ball1.y;
var dist = Math.sqrt(dx * dx + dy * dy);
var misDist = ball1.radius + ball2.radius;
if(dist < misDist) {
var angle = Math.atan2(dy, dx);
tx = ball1.x + Math.cos(angle) * misDist;
ty = ball1.y + Math.sin(angle) * misDist;
ax = (tx - ball2.x) * self.spring;
ay = (ty - ball2.y) * self.spring;
ball1.vx -= ax;
ball1.vy -= ay;
ball2.vx += ax;
ball2.vy += ay;
}
}
}
for(i = 0; i < num; i++) {
self.moveBalls(balls[i]);
}
},
moveBalls: function(ball) {
var self = this;
ball.vy += self.gravity;
ball.style.left = (ball.offsetLeft + ball.vx) + 'px';
ball.style.top = (ball.offsetTop + ball.vy) + 'px';
var L = self.L_bound,
R = self.R_bound,
T = self.T_bound,
B = self.B_bound,
BC = self.bounce;
if(ball.offsetLeft < L) {
ball.style.left = L;
ball.vx *= BC;
} else if(ball.offsetLeft + ball.diameter > R) {
ball.style.left = (R - ball.diameter) + 'px';
ball.vx *= BC;
} else if(ball.offsetTop < T) {
ball.style.top = T;
ball.vy *= BC;
}
if(ball.offsetTop + ball.diameter > B) {
ball.style.top = (B - ball.diameter) + 'px';
ball.vy *= BC;
}
}
}
window.onload = function() {
var sc = null;
document.getElementById("inner").innerHTML = '';
sc = new Screen('inner', {
ballsnum: 10,
spring: 0.3,
bounce: -0.9,
gravity: 0.01
});
sc.initialize();
getFlag('start').onclick = function() {
document.getElementById("inner").innerHTML = '';
sc = new Screen('inner', {
ballsnum: 10,
spring: 0.3,
bounce: -0.9,
gravity: 0.01
});
sc.initialize();
}
getFlag('stop').onclick = function() {
clearInterval(sc.timer);
}
}
</script>
</body>
</html>