-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcube.htm
More file actions
187 lines (164 loc) · 3.87 KB
/
cube.htm
File metadata and controls
187 lines (164 loc) · 3.87 KB
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
180
181
182
183
184
185
186
187
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Cube rotation</title>
<style>
#canvas {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 100%;
height: 100%;
display: block;
}
canvas {
image-rendering: optimizeSpeed;
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-optimize-contrast;
image-rendering: -o-crisp-edges;
image-rendering: crisp-edges;
-ms-interpolation-mode: nearest-neighbor;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) {window.setTimeout(callback, 1000 / 60);};
})();
</script>
<script>
var q = window.innerHeight / window.innerWidth;
var w = 320, h = Math.floor(q * w), d = 200;
var canvas = document.getElementById("canvas");
canvas.height = h;
canvas.width = w;
var ctx = canvas.getContext("2d");
var Vertex = function()
{
this.x = 0;
this.y = 0;
this.z = 0;
this.px = 0;
this.py = 0;
};
var cx = w / 2, cy = h / 2;
var vertices = new Array(8);
function createVertex(i)
{
vertices[i].x = i & 1 ? -50 : 50;
vertices[i].y = i & 2 ? -50 : 50;
vertices[i].z = i & 4 ? -50 : 50;
}
function drawStar(x, y, color)
{
ctx.fillStyle = color;
ctx.fillRect(x, y, 1, 1);
}
function drawLine(x1, y1, x2, y2, color)
{
var dy = Math.round(Math.abs(y2 - y1));
var dx = Math.round(Math.abs(x2 - x1));
var step = 0;
var xsign = x1 > x2 ? -1 : 1;
var ysign = y1 > y2 ? -1 : 1;
var x = Math.round(x1);
var y = Math.round(y1);
if (dx > dy)
{
for (i = 0; i < dx; i++)
{
x += xsign;
step += dy;
if (step >= dx)
{
y += ysign;
step -= dx;
}
drawStar(x, y, color);
}
}
else
{
for (i = 0; i < dy; i++)
{
y += ysign;
step += dx;
if (step >= dy)
{
x += xsign;
step -= dy;
}
drawStar(x, y, color);
}
}
}
function clearScreen()
{
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, w, h);
}
var kotx = 0;
var koty = 0;
var kotz = 0;
function moveCube()
{
clearScreen();
for (var i = 0; i < vertices.length; i++)
{
var vertex = vertices[i];
/* Rotation */
var x2 = Math.cos(koty) * vertex.x - Math.sin(koty) * vertex.z;
var y2 = Math.cos(kotz) * vertex.y - Math.sin(kotz) * x2;
var z2 = Math.cos(koty) * vertex.z + Math.sin(koty) * vertex.x;
var x1 = Math.cos(kotz) * x2 + Math.sin(kotz) * vertex.y;
var y1 = Math.cos(kotx) * y2 + Math.sin(kotx) * z2;
var z1 = Math.cos(kotx) * z2 - Math.sin(kotx) * y2;
var k = d / (z1 + 2 * d);
vertex.px = Math.round(k * x1 + cx);
vertex.py = Math.round(k * y1 + cy);
if (0 <= vertex.px && vertex.px <= w && 0 <= vertex.py && vertex.py <= h)
{
drawStar(vertex.px, vertex.py, '#fff');
}
}
/* Edges */
for (var i = 0; i < vertices.length; i++)
{
for (j = 1; j < 7; j = 2 * j)
{
var k = i ^ j;
if (i >= k)
{
continue;
}
drawLine(vertices[i].px, vertices[i].py, vertices[k].px, vertices[k].py, '#a00')
}
}
}
function startVertexRotation()
{
kotx += 0.01;
koty += 0.01;
kotz += 0.002;
moveCube();
requestAnimFrame(startVertexRotation);
}
function vertexRotation()
{
clearScreen();
for (var i = 0; i < 8; i++)
{
vertices[i] = new Vertex();
createVertex(i);
}
startVertexRotation();
}
window.addEventListener("load", vertexRotation);
</script>
</body>
</html>