-
Notifications
You must be signed in to change notification settings - Fork 53
/
part6c.html
97 lines (70 loc) · 1.68 KB
/
part6c.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
<!DOCTYPE html>
<html>
<title>Creative Coding Yea!</title>
<head>
<meta charset="utf-8">
<script language="javascript" src="js/creative_coding.js"></script>
<script language="javascript" src="js/canvas.js"></script>
<link rel="stylesheet" href="css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
</head>
<body>
<script>
var ctx = createCanvas("canvas1");
var number_of_balls = 300;
var balls = [];
var max_distance = 90;
// push a ball and it's values into the array
for (var i = 0; i < number_of_balls; i++) {
addBall(i);
}
function addBall(_i){
var ball = {
x: random(w),
y: random(h),
speed_x: posNeg() * random(0.2, 1),
speed_y: posNeg() * random(0.2, 1),
size: 5,
colour: rgb(255),
}
balls.push(ball);
}
function draw(){
ctx.background(0);
moveBall();
drawBall();
}
function moveBall(){
for (var i = 0; i < balls.length; i++) {
var b = balls[i];
b.x += b.speed_x;
b.y += b.speed_y;
if (bounce(b.x, 0, w)) b.speed_x *=-1;
if (bounce(b.y, 0, h)) b.speed_y *=-1;
}
}
function drawBall(){
for (var i = 0; i < balls.length; i++) {
var b = balls[i];
drawConnections(i);
ctx.fillStyle = b.colour;
ctx.fillEllipse(b.x, b.y, b.size);
}
}
function drawConnections(_i) {
for (var j = 0; j < balls.length; j++) {
b1 = balls[_i];
b2 = balls[j];
var distance = dist(b1.x, b1.y, b2.x, b2.y);
if (j!=i) {
if ( distance <= max_distance) {
ctx.strokeStyle = rgb(255);
ctx.lineWidth = 1 - distance/max_distance;
ctx.line(b1.x, b1.y, b2.x, b2.y);
}
}
}
}
</script>
</body>
</html>