-
Notifications
You must be signed in to change notification settings - Fork 53
/
part12g.html
118 lines (89 loc) · 2.26 KB
/
part12g.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
<!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 ctx2 = createCanvas("canvas2");
var colours = new colourPool()
.add("#FBA83F", 0.9)
.add("#E84004", 0.9)
.add("#CCD9AC", 0.9)
.add("#BFFFCB", 0.6)
.add("#D8DEC5", 0.6)
.add("#FBA83F", 0.6)
.add("#E84004")
.add("#CCD9AC");
// create particles
var balls = [];
var number_of_balls = 26;
var max_trails = 30;
ctx2.lineWidth = 0.1;
for (var i = 0; i < number_of_balls; i++) {
balls.push(new Ball(w/2, random(h), colours.get(), random(60,120)));
}
function Ball(_x, _y, _c, _sz){
this.x = _x;
this.y = _y;
this.speed_x = random(-8,8);
this.speed_y = random(-8,8);
this.sz = _sz;
this.s = 0;
this.r = random(360);
this.counter = random(360);
this.trails = [];
this.colour = _c;
}
function draw(){
ctx.clearRect(0, 0, w, h);
for (var i = 0; i < balls.length; i++) {
var b = balls[i];
moveBall(b);
for (var j = 0; j < b.trails.length; j++) {
drawBall(b.trails[j]);
}
drawBall(b);
}
}
function moveBall(b){
b.x += b.speed_x;
b.y += b.speed_y;
b.r += b.speed_x/2;
if(b.x > w - b.sz/2 || b.x < b.sz/2){
b.speed_x *= -1;
}
if(b.y > h - b.sz/2 || b.y < b.sz/2) {
b.speed_y *= -1;
}
// for (var j = 0; j < b.trails.length; j++) {
// var bb = b.trails[j];
// bb.x += bb.speed_x;
// bb.y += bb.speed_y;
// }
var x = b.x;
var y = b.y;
b.trails.push(new Ball(b.x, b.y, rgba(0, 0.4), b.sz-1));
if (b.trails.length > max_trails) b.trails.splice(0,1);
}
function drawBall(b){
ctx.save();
ctx.translate(b.x, b.y);
ctx.rotateDeg(b.r);
b.s = 10 + Math.abs(Math.sin((b.counter + frameCount)/100) * b.sz);
//ctx.strokeStyle = "red";
ctx.fillStyle = b.colour;
//ctx.fillStyle = rgba(200, 0.2);
ctx.fillEllipse(0, 0, b.s, b.s);
//ctx.roundRect(0, 0, b.s, b.s, 8);
ctx.restore();
}
</script>
</body>
</html>