-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcube.html
179 lines (150 loc) · 3.34 KB
/
cube.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content=
"width=device-width, initial-scale=1.0" />
<title>3D Cube</title>
<style>
body {
margin: 0;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas> </canvas>
<script>
// Constants
const BACKGROUND_COLOR = "black";
const CUBE_COLOR = "yellow";
const POINT_2D = function (x, y) {
this.x = x;
this.y = y;
};
const POINT_3D = function (x, y, z) {
this.x = x;
this.y = y;
this.z = z;
};
// Set up the canvas and context
var canvas = document.querySelector("canvas");
var context = canvas.getContext("2d");
// Dimensions
var h = document.documentElement.clientHeight;
var w = document.documentElement.clientWidth;
canvas.height = h;
canvas.width = w;
// Colors and lines
context.fillStyle = BACKGROUND_COLOR;
context.strokeStyle = CUBE_COLOR;
context.lineWidth = w / 100;
context.lineCap = "round";
// Cube parameters
var cube_x = 0;
var cube_y = 0;
var cube_z = 250;
var cube_size = 100;
var cube_vertices = [
// Back 4 vertices
new POINT_3D(
cube_x - cube_size,
cube_y - cube_size,
cube_z - cube_size
),
new POINT_3D(
cube_x + cube_size,
cube_y - cube_size,
cube_z - cube_size
),
new POINT_3D(
cube_x + cube_size,
cube_y + cube_size,
cube_z - cube_size
),
new POINT_3D(
cube_x - cube_size,
cube_y + cube_size,
cube_z - cube_size
),
// Front 4 vertices
new POINT_3D(
cube_x - cube_size,
cube_y - cube_size,
cube_z + cube_size
),
new POINT_3D(
cube_x + cube_size,
cube_y - cube_size,
cube_z + cube_size
),
new POINT_3D(
cube_x + cube_size,
cube_y + cube_size,
cube_z + cube_size
),
new POINT_3D(
cube_x - cube_size,
cube_y + cube_size,
cube_z + cube_size
),
];
var cube_edges = [
[0, 1],
[1, 2],
[2, 3],
[3, 0], // Back face
[4, 5],
[5, 6],
[6, 7],
[7, 4], // Front face
[0, 4],
[1, 5],
[2, 6],
[3, 7], // Connecting sides
];
function project(points3d, w, h) {
// Creating 2d point for every 3d point
var points2d = new Array(points3d.length);
var focal_length = 200;
for (let index = points3d.length - 1;
index > -1; --index) {
let p = points3d[index];
let x = p.x * (focal_length / p.z) + w * 0.5;
let y = p.y * (focal_length / p.z) + h * 0.5;
points2d[index] = new POINT_2D(x, y);
}
return points2d;
}
// Set up the animation loop
var timeDifference,
timeLast = 0;
requestAnimationFrame(loop);
function loop(currentTime) {
timeDifference = currentTime - timeLast;
timeLast = currentTime;
// Background
context.fillRect(0, 0, w, h);
// Draw each edge
var vertices = project(cube_vertices, w, h);
for (let edge of cube_edges) {
context.beginPath();
context.moveTo(
vertices[edge[0]].x,
vertices[edge[0]].y
);
context.lineTo(
vertices[edge[1]].x,
vertices[edge[1]].y
);
context.stroke();
}
// Call the next frame
requestAnimationFrame(loop);
}
</script>
</body>
</html>