در این قسمت میخوام ششمین کد ( code ) جالب رو در اختیارتون بدم.
در ابتدا تعدادی توپ وجود دارن. اگر کلیک راست رو نگه دارین و درگ کنید ، توپها پخش و پلا میشن. اگر در فضای خالی هم کلیک کنید ، یک توپ به توپهای قبلی اضافه میشه.
کد HTML :
1
|
<canvas id=“c”></canvas>
|
کدهای CSS :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
* {
margin: 0;
padding: 0;
}
body,html {
background: #FFF8E7;
width: 100%;
height: 100%;
}
canvas {
background: #FFF8E7;
width: 1000px;
height: 360px;
margin: 0 auto;
display: block;
border: 1px dashed #aaa;
border-top: none;
}
|
کدهای Javascript :
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
|
var NUM_BALLS = 130,
DAMPING = 0.7,
GRAVITY = 0.6,
MOUSE_SIZE = 50,
SPEED = 1;
var canvas, ctx, TWO_PI = Math.PI * 2, balls = [], mouse = {down:false,x:0,y:0};
window.requestAnimFrame =
window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
var Ball = function(x, y, radius) {
this.x = x;
this.y = y;
this.px = x;
this.py = y;
this.fx = 0;
this.fy = 0;
this.radius = radius;
};
Ball.prototype.apply_force = function(delta) {
delta *= delta;
this.fy += GRAVITY;
this.x += this.fx * delta;
this.y += this.fy * delta;
this.fx = this.fy = 0;
};
Ball.prototype.verlet = function() {
var nx = (this.x * 2) – this.px;
var ny = (this.y * 2) – this.py;
this.px = this.x;
this.py = this.y;
this.x = nx;
this.y = ny;
};
Ball.prototype.draw = function(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, TWO_PI);
ctx.fill();
};
//—————————————
var resolve_collisions = function(ip) {
var i = balls.length;
while (i—) {
var ball_1 = balls[i];
if (mouse.down) {
var diff_x = ball_1.x – mouse.x;
var diff_y = ball_1.y – mouse.y;
var dist = Math.sqrt(diff_x * diff_x + diff_y * diff_y);
var real_dist = dist – (ball_1.radius + MOUSE_SIZE);
if (real_dist < 0) {
var depth_x = diff_x * (real_dist / dist);
var depth_y = diff_y * (real_dist / dist);
ball_1.x -= depth_x * 0.005;
ball_1.y -= depth_y * 0.005;
}
}
var n = balls.length;
while (n—) {
if (n == i) continue;
var ball_2 = balls[n];
var diff_x = ball_1.x – ball_2.x;
var diff_y = ball_1.y – ball_2.y;
var length = diff_x * diff_x + diff_y * diff_y;
var dist = Math.sqrt(length);
var real_dist = dist – (ball_1.radius + ball_2.radius);
if (real_dist < 0) {
var vel_x1 = ball_1.x – ball_1.px;
var vel_y1 = ball_1.y – ball_1.py;
var vel_x2 = ball_2.x – ball_2.px;
var vel_y2 = ball_2.y – ball_2.py;
var depth_x = diff_x * (real_dist / dist);
var depth_y = diff_y * (real_dist / dist);
ball_1.x -= depth_x * 0.5;
ball_1.y -= depth_y * 0.5;
ball_2.x += depth_x * 0.5;
ball_2.y += depth_y * 0.5;
if (ip) {
var pr1 = DAMPING * (diff_x*vel_x1+diff_y*vel_y1) / length,
pr2 = DAMPING * (diff_x*vel_x2+diff_y*vel_y2) / length;
vel_x1 += pr2 * diff_x – pr1 * diff_x;
vel_x2 += pr1 * diff_x – pr2 * diff_x;
vel_y1 += pr2 * diff_y – pr1 * diff_y;
vel_y2 += pr1 * diff_y – pr2 * diff_y;
ball_1.px = ball_1.x – vel_x1;
ball_1.py = ball_1.y – vel_y1;
ball_2.px = ball_2.x – vel_x2;
ball_2.py = ball_2.y – vel_y2;
}
}
}
}
};
var check_walls = function() {
var i = balls.length;
while (i—) {
var ball = balls[i];
if (ball.x < ball.radius) {
var vel_x = ball.px – ball.x;
ball.x = ball.radius;
ball.px = ball.x – vel_x * DAMPING;
} else if (ball.x + ball.radius > canvas.width) {
var vel_x = ball.px – ball.x;
ball.x = canvas.width – ball.radius;
ball.px = ball.x – vel_x * DAMPING;
}
if (ball.y < ball.radius) {
var vel_y = ball.py – ball.y;
ball.y = ball.radius;
ball.py = ball.y – vel_y * DAMPING;
} else if (ball.y + ball.radius > canvas.height) {
var vel_y = ball.py – ball.y;
ball.y = canvas.height – ball.radius;
ball.py = ball.y – vel_y * DAMPING;
}
}
};
var update = function() {
//var time = new Date().getTime();
var iter = 6;
var delta = SPEED / iter;
while (iter—) {
var i = balls.length;
while (i—) {
balls[i].apply_force(delta);
balls[i].verlet();
}
resolve_collisions();
check_walls();
var i = balls.length;
while (i—) balls[i].verlet();
resolve_collisions(1);
check_walls();
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = ‘rgba(27,155,244,0.3)’;
var i = balls.length;
while (i—) balls[i].draw(ctx);
if (mouse.down) {
ctx.fillStyle = ‘rgba(0,0,0,0.1)’;
ctx.strokeStyle = ‘rgba(0,0,0,0.2)’;
ctx.beginPath();
ctx.arc(mouse.x, mouse.y, MOUSE_SIZE, 0, TWO_PI);
ctx.fill();
ctx.stroke();
}
requestAnimFrame(update);
//console.log(new Date().getTime() – time);
};
var add_ball = function(x, y, r) {
var x = x || Math.random() * (canvas.width – 60) + 30,
y = y || Math.random() * (canvas.height – 60) + 30,
r = r || 10 + Math.random() * 20,
s = true,
i = balls.length;
while (i—) {
var ball = balls[i];
var diff_x = ball.x – x;
var diff_y = ball.y – y;
var dist = Math.sqrt(diff_x * diff_x + diff_y * diff_y);
if (dist < ball.radius + r) {
s = false;
break;
}
}
if (s) balls.push(new Ball(x, y, r));
};
window.onload = function() {
canvas = document.getElementById(‘c’);
ctx = canvas.getContext(‘2d’);
canvas.width = 1000;
canvas.height = 360;
while (NUM_BALLS—) add_ball();
canvas.onmousedown = function(e) {
if (e.which == 1) {
add_ball(mouse.x, mouse.y);
} else if (e.which == 3) {
mouse.down = true;
document.body.style.cursor = ‘none’;
}
e.preventDefault();
};
canvas.onmouseup = function(e) {
if (e.which == 3) {
mouse.down = false;
document.body.style.cursor = ‘default’;
}
e.preventDefault();
};
canvas.onmousemove = function(e) {
var rect = this.getBoundingClientRect();
mouse.x = e.clientX – rect.left;
mouse.y = e.clientY – rect.top;
};
canvas.onmouseout = function(e) {
mouse.down = false;
document.body.style.cursor = ‘default’;
};
canvas.oncontextmenu = function(e) {
e.preventDefault();
return false;
};
update();
|