-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsoView.js
124 lines (100 loc) · 3.59 KB
/
IsoView.js
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
'use strict';
var DEG_30 = Math.PI / 3;
var DEG_60 = (Math.PI * 2) / 3;
var DEG_45 = Math.PI / 4;
var DEG_90 = Math.PI / 2;
var zHorizontalAngle = DEG_30;
var zRotationAngle = DEG_45;
var xVector = {
dX: function() {return 20 * Math.cos(zRotationAngle);},
dY: function() {return 20 * Math.sin(zRotationAngle)*(Math.cos(zHorizontalAngle));}
};
var yVector = {
dX: function() {return -20 * Math.sin(zRotationAngle);},
dY: function() {return 20 * Math.cos(zRotationAngle)*(Math.cos(zHorizontalAngle));}
};
var zVector = {
dX: function() {return 0;},
dY: function() {return -20 * Math.sin(zHorizontalAngle);}
};
var origin = new IsoPoint().addVector(xVector, 5).addVector(yVector, 5);
Grid.prototype.draw3dGrid = function() {
var i;
var context = this.isoCanvas.getContext('2d');
context.clearRect(0, 0, this.isoCanvas.width, this.isoCanvas.height);
context.globalAlpha = 1;
for (var k = 0 ; k < this.content.length ; k++) {
for (i = 0; i < this.width; i++) {
for (var j = 0; j < this.height; j++) {
var cubeOrigin = origin.addVector(zVector, k).addVector(xVector, i+1-this.width/2).addVector(yVector, j+1-this.height/2);
if (this.mustBeDrawn(k, i, j)) {
drawCube(context, cubeOrigin, this.content[k][i][j]);
}
}
}
}
};
Grid.prototype.mustBeDrawn = function(k, i, j) {
return (this.content[k][i][j] != undefined) &&
(
(k == this.content.length - 1)
|| (i == this.width - 1)
|| (j == this.height - 1)
|| (
(k+1 < this.content.length && this.content[k+1][i][j] == undefined)
|| (i+1 < this.content[0].length && this.content[k][i+1][j] == undefined)
|| (j+1 < this.content[0][0].length && this.content[k][i][j+1] == undefined)
)
)
};
function drawCube (context, cubeOrigin, color) {
var topColor = colorsHash[color][TOP_COLOR];
var t1 = cubeOrigin.addVector(xVector, -1);
var t2 = t1.addVector(yVector, -1);
var t3 = t2.addVector(xVector);
var t4 = t3.addVector(yVector);
var topPoints = [t1,t2,t3,t4];
drawFacet(context, cubeOrigin, topPoints, topColor);
var leftColor = colorsHash[color][LEFT_COLOR];
var l1 = cubeOrigin.addVector(yVector, -1);
var l2 = l1.addVector(zVector, -1);
var l3 = l2.addVector(yVector);
var l4 = l3.addVector(zVector);
var leftPoints = [l1,l2,l3,l4];
drawFacet(context, cubeOrigin, leftPoints, leftColor);
var rightColor = colorsHash[color][RIGHT_COLOR];
var r1 = cubeOrigin.addVector(xVector, -1);
var r2 = r1.addVector(zVector, -1);
var r3 = r2.addVector(xVector);
var r4 = r3.addVector(zVector);
var rightPoints = [r1,r2,r3,r4];
drawFacet(context, cubeOrigin, rightPoints, rightColor);
}
function toLeftColor(color) {
var r = _hexToR(color);
var g = _hexToG(color);
var b = _hexToB(color);
r = Math.max(0,r - 30);
g = Math.max(0,g - 30);
b = Math.max(0,b - 30);
return _rgbToHex(r,g,b);
}
function toRightColor(color) {
var r = _hexToR(color);
var g = _hexToG(color);
var b = _hexToB(color);
r = Math.max(0,r - 60);
g = Math.max(0,g - 60);
b = Math.max(0,b - 60);
return _rgbToHex(r,g,b);
}
function drawFacet (context, cubeOrigin, points, color) {
context.beginPath();
context.moveTo(cubeOrigin.x, cubeOrigin.y);
for (var i = 0 ; i < points.length ; i++) {
context.lineTo(points[i].x, points[i].y);
}
context.closePath();
context.fillStyle = color;
context.fill();
}