-
Notifications
You must be signed in to change notification settings - Fork 53
/
part12d.html
108 lines (83 loc) · 1.88 KB
/
part12d.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
<!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;
ctx2.lineWidth = 1;
for (var i = 0; i < number_of_balls; i++) {
addBall(w/2, random(h));
}
function addBall(_x, _y){
var ball = {
x: _x,
y: _y,
speed_x: random(-5,5),
speed_y: random(-5,5),
sz: random(60,120),
s: 0,
r: 0,
colour: colours.get()
}
balls.push(ball);
}
function draw(){
ctx.background(255);
ctx2.clearRect(0, 0, w, h);
for (var i = 0; i < balls.length; i++) {
var b = balls[i];
moveBall(b);
ctx.save();
ctx2.save();
ctx.translate(b.x, b.y);
ctx2.translate(b.x, b.y);
drawBall(b);
ctx.restore();
ctx2.restore();
}
}
function moveBall(b){
b.x += b.speed_x;
b.y += b.speed_y;
b.r += b.speed_x/3;
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;
}
}
function drawBall(b){
ctx.rotateDeg(b.r);
ctx2.rotateDeg(b.r);
b.s = 10 + Math.abs(Math.sin(frameCount/100) * b.sz);
ctx.strokeStyle = "black";
ctx.fillStyle = colours.get();
ctx.roundRect(0, 0, b.s, b.s, 8);
ctx2.strokeStyle = "black";
ctx2.fillStyle = b.colour;
ctx2.roundRect(0, 0, b.s, b.s, 8);
}
</script>
</body>
</html>