-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathpart12b.html
102 lines (79 loc) · 1.83 KB
/
part12b.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
<!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("#FFFFFF", 0.7)
.add("#F7F7F7", 0.9)
.add("#ECECEC", 0.7)
.add("#0095A8", 0.8)
.add("#00616F", 0.5)
.add("#333333", 0.6)
.add("#FF3300", 0.4)
.add("#FF6600", 0.6);
// create particles
var balls = [];
var number_of_balls = 20;
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(40,80),
r: 0,
colour: colours.get()
}
balls.push(ball);
}
function draw(){
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);
ctx.fillStyle = colours.get();
ctx.fillPolygon(0, 0, 6, b.sz);
ctx2.fillStyle = b.colour;
ctx2.fillPolygon(0, 0, 6, b.sz);
// ctx2.strokeStyle = "black";
// ctx2.strokePolygon(0, 0, 6, b.sz);
}
</script>
</body>
</html>