-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcanvas-3d-cube.html
286 lines (252 loc) · 7.04 KB
/
canvas-3d-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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>3D cube HTML5 canvas realization</title>
<script type="text/javascript">
function color(r, g, b, a)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
function point2D(x, y)
{
this.x = x;
this.y = y;
}
point2D.prototype.move = function(p2D)
{
this.x += p2D.x;
this.y += p2D.y;
}
function point3D(x, y, z)
{
this.x = x;
this.y = y;
this.z = z;
}
point3D.prototype.move = function(p3D)
{
this.x += p3D.x;
this.y += p3D.y;
this.z += p3D.z;
}
point3D.prototype.swap = function(p3D)
{
this.x = p3D.x;
this.y = p3D.y;
this.z = p3D.z;
}
point3D.prototype.rotate = function(axis, angleGr)
{
angleRad = angleGr * Math.PI / 180;
switch (axis)
{
case "x":
{
var tempPoint = new point3D(
this.x,
this.y * Math.cos(angleRad) - this.z * Math.sin(angleRad),
this.y * Math.sin(angleRad) + this.z * Math.cos(angleRad)
);
this.swap(tempPoint);
break;
}
case "y":
{
var tempPoint = new point3D(
this.x * Math.cos(angleRad) + this.z * Math.sin(angleRad),
this.y,
-this.x * Math.sin(angleRad) + this.z * Math.cos(angleRad)
);
this.swap(tempPoint);
break;
}
case "z":
{
var tempPoint = new point3D(
this.x * Math.cos(angleRad) - this.y * Math.sin(angleRad),
this.x * Math.sin(angleRad) + this.y * Math.cos(angleRad),
this.z
);
this.swap(tempPoint);
break;
}
}
}
function normal3D(p3D, length)
{
this.point = p3D;
this.length = length;
}
function poly()
{
var points = [];
for(var i = 0; i < arguments.length; i++)
points.push(arguments[i]);
this.points = points;
// Calculating normal
var v1 = new point3D(points[2].x - points[1].x, points[2].y - points[1].y, points[2].z - points[1].z);
var v2 = new point3D(points[0].x - points[1].x, points[0].y - points[1].y, points[0].z - points[1].z);
var normalP3D = new point3D(v1.y*v2.z-v2.y*v1.z, v1.z*v2.x-v2.z*v1.x, v1.x*v2.y-v2.x*v1.y);
var normalLen = Math.sqrt(normalP3D.x*normalP3D.x + normalP3D.y*normalP3D.y + normalP3D.z*normalP3D.z);
this.normal = new normal3D(normalP3D, normalLen);
}
poly.prototype.move = function(p3D)
{
for(var i = 0; i < this.points.length; i++)
{
var point = this.points[i];
point.move(p3D);
}
}
poly.prototype.rotate = function(axis, angle)
{
for(var i = 0; i < this.points.length; i++)
{
var point = this.points[i];
point.rotate(axis, angle);
}
this.normal.point.rotate(axis, angle);
}
poly.prototype.put = function(center, fillColor, edgeColor)
{
// Calulate visibility
var normalAngleRad = Math.acos(this.normal.point.z/this.normal.length);
if(normalAngleRad / Math.PI * 180 >= 90)
return;
var lightIntensity = 1 - 2 * (normalAngleRad / Math.PI);
ctx.fillStyle = 'rgba('+fillColor.r+','+fillColor.g+','+fillColor.b+','+(fillColor.a*lightIntensity)+')';
ctx.beginPath();
for(var i = 0; i < this.points.length; i++)
{
var point = this.points[i];
if(i)
ctx.lineTo(center.x + parseInt(point.x), center.y - parseInt(point.y));
else
ctx.moveTo(center.x + parseInt(point.x), center.y - parseInt(point.y));
}
ctx.fill();
ctx.lineWidth = 1;
ctx.strokeStyle = 'rgba('+edgeColor.r+','+edgeColor.g+','+edgeColor.b+','+(edgeColor.a*lightIntensity)+')';
ctx.beginPath();
var point = this.points[this.points.length-1];
ctx.moveTo(center.x + parseInt(point.x), center.y - parseInt(point.y));
for(var i = 0; i < this.points.length; i++)
{
var point = this.points[i];
ctx.lineTo(center.x + parseInt(point.x), center.y - parseInt(point.y));
}
ctx.stroke();
}
function Cube(size, fillColor, edgeColor)
{
var p000 = new point3D(0,0,0);
var p0S0 = new point3D(0,size,0);
var pSS0 = new point3D(size,size,0);
var pS00 = new point3D(size,0,0);
var p00S = new point3D(0,0,size);
var p0SS = new point3D(0,size,size);
var pSSS = new point3D(size,size,size);
var pS0S = new point3D(size,0,size);
var polys = [];
polys.push(new poly(p000,p0S0,pSS0,pS00));
polys.push(new poly(pS00,pSS0,pSSS,pS0S));
polys.push(new poly(pS0S,pSSS,p0SS,p00S));
polys.push(new poly(p00S,p0SS,p0S0,p000));
polys.push(new poly(p0S0,p0SS,pSSS,pSS0));
polys.push(new poly(p00S,p000,pS00,pS0S));
this.polys = polys;
var points = [];
points.push(p000);
points.push(p0S0);
points.push(pSS0);
points.push(pS00);
points.push(p00S);
points.push(p0SS);
points.push(pSSS);
points.push(pS0S);
for(var i = 0; i < polys.length; i++)
{
points.push(polys[i].normal.point);
}
this.points = points;
this.fillColor = fillColor;
this.edgeColor = edgeColor;
}
function move(o3D, p3D)
{
for(var i = 0; i < o3D.points.length - o3D.polys.length; i++)
{
var point = o3D.points[i];
point.move(p3D);
}
}
function put(o3D, center)
{
for(var i = 0; i < o3D.polys.length; i++)
{
var poly = o3D.polys[i];
poly.put(center, o3D.fillColor, o3D.edgeColor);
}
}
function rotate(o3D, axis, angle)
{
for(var i = 0; i < o3D.points.length; i++)
{
var point = o3D.points[i];
point.rotate(axis, angle);
}
}
function init(){
canvas = document.getElementById('3Dcube');
if (canvas.getContext){
ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgba(0, 0, 0, 1)';
ctx.fillRect(0, 0, canvas.width, canvas.height); // clear canvas
cube = new Cube(100, new color(50,50,200,1), new color(60,60,210,1));
move(cube, new point3D(-50,-50,-50));
rotate(cube, 'x', 45);
rotate(cube, 'y', 45);
rotate(cube, 'z', 45);
centerScreen = new point2D(canvas.width / 2, canvas.height / 2);
put(cube, centerScreen);
timer = setInterval(nextFrame, 1000 / 60);
}
}
function nextFrame()
{
ctx.fillStyle = 'rgba(0, 0, 0, 1)';
ctx.fillRect(0, 0, canvas.width, canvas.height); // clear canvas
rotate(cube, 'x', 0.4);
rotate(cube, 'y', 0.6);
rotate(cube, 'z', 0.3);
ctx.fillStyle = 'rgba(50, 50, 200, 1)';
ctx.strokeStyle = 'rgba(60, 60, 210, 1)';
put(cube, centerScreen);
}
</script>
<style type="text/css">
canvas { border: 0px solid black; }
</style>
</head>
<body onload="init();">
<h1>3D cube HTML5 canvas realization on 2D contex</h1>
<p>Features:
<ul>
<li>3D operations: rotating, moving object center</li>
<li>Direct illumination</li>
<li>Highlighting edges</li>
<li>Optimizations:
<ul>
<li>Skip outputting of invisible polygons</li>
<li>Skip processing of duplicated points</li>
</ul>
</li>
</ul>
</p>
<canvas id="3Dcube" width="400" height="225"></canvas>
</body>
</html>