-
Notifications
You must be signed in to change notification settings - Fork 53
/
part10c.html
144 lines (109 loc) · 3.3 KB
/
part10c.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
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
<!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 = 5;
var balls = [];
var radius = 200;
var fov = 240;
var horizon_x = width / 2 //or wherever you've placed it
var horizon_y = height / 2 //again, wherever else is fine
for (var i = 0; i < number_of_balls; i++) {
addBall(i);
}
function addBall(_i){
var ball = {
x: 0,
y: 0,
z: 0,
x2d: 0,
y2d: 0,
speed: 0.00002,
scale: 1,
size: 10,
colour: rgb(0),
angle: distributeAngles(_i, number_of_balls)
}
ball.x = Math.cos(radians(ball.angle))*(radius+ball.size/2);
ball.y = Math.sin(radians(ball.angle))*(radius+ball.size/2);
ball.z = Math.sin(radians(ball.angle))*(radius+ball.size/2);
balls.push(ball);
}
function draw(){
ctx.background(245, 0.4);
horizon_y = h/2-mouseY;
ctx.save();
//ctx.fillStyle = "red";
//ctx.HfillEllipse(w/2, h/2, radius*2);
//ctx.translate(w/2, h/2);
//ctx.rotateDegrees(map(mouseX,0,w, 0, 360));
moveBall();
drawBall();
ctx.restore();
}
function moveBall(){
for (var i = 0; i < balls.length; i++) {
var b = balls[i];
// move the angle to rotate the balls
//b.angle += b.speed;
rotateX(b, radians(1));
rotateY(b, radians(1));
rotateZ(b, radians(1));
//if (i==1) console.log(b.z);
b.size = map(b.z, -fov, fov, 1, 20);
b.x2d = projection(b.x, b.z, width/2, h/2, fov);
b.y2d = projection(b.y, b.z, height/2, h/2, fov);
// b.angle_x = b.angle + (mouseX/width-0.5)/1;
// b.angle_y = b.angle + ((mouseY/height)-0.5)/1;
//if(i==0) console.log(b.angle_x);
//b.scale = fov/(b.z+fov);
//var scaleinverse = 1.0 - b.scale //the inverse of your scale_factor
// // // // b.x2d = (b.x * b.scale) + (horizon_x * scaleinverse);
// // // // b.y2d = (b.y * b.scale) + (horizon_y * scaleinverse);
//b.x2d = (b.x * b.scale) + (horizon_x * scaleinverse);
//b.y2d = (b.y * b.scale) + (horizon_y * scaleinverse);
}
}
function drawBall(){
for (var i = 0; i < balls.length; i++) {
var b = balls[i];
ctx.fillStyle = b.colour;
ctx.fillEllipse(b.x2d, b.y2d, b.size*b.scale);
//ctx.fillEllipse(b.x, b.y, b.size*b.scale);
}
}
function distributeAngles(me, total) {
return me/total * 360;
}
function rotateX(b, radians) {
//console.log(b.x);
var y = b.y;
b.y = (y * Math.cos(radians)) + (b.z * Math.sin(radians) * -1.0);
//console.log(p.y);
b.z = (y * Math.sin(radians)) + (b.z * Math.cos(radians));
}
function rotateY(p, radians) {
var x = p.x;
p.x = (x * Math.cos(radians)) + (p.z * Math.sin(radians) * -1.0);
p.z = (x * Math.sin(radians)) + (p.z * Math.cos(radians));
}
function rotateZ(p, radians) {
var x = p.x;
p.x = (x * Math.cos(radians)) + (p.y * Math.sin(radians) * -1.0);
p.y = (x * Math.sin(radians)) + (p.y * Math.cos(radians));
}
function projection(xy, z, xyOffset, zOffset, distance) {
return ((distance * xy) / (z - zOffset)) + xyOffset;
}
</script>
</body>
</html>