forked from rrreese/Hexagon.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexagon.js
198 lines (150 loc) · 5.84 KB
/
hexagon.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
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
// Hex math defined here: http://blog.ruslans.com/2011/02/hexagonal-grid-math.html
function HexagonGrid(canvasId, radius) {
this.radius = radius;
this.height = Math.sqrt(3) * radius;
this.width = 2 * radius;
this.side = (3 / 2) * radius;
this.canvas = document.getElementById(canvasId);
this.context = this.canvas.getContext('2d');
this.canvasOriginX = 0;
this.canvasOriginY = 0;
this.canvas.addEventListener("mousedown", this.clickEvent.bind(this), false);
};
HexagonGrid.prototype.drawHexGrid = function (rows, cols, originX, originY, isDebug) {
this.canvasOriginX = originX;
this.canvasOriginY = originY;
var currentHexX;
var currentHexY;
var debugText = "";
var offsetColumn = false;
for (var col = 0; col < cols; col++) {
for (var row = 0; row < rows; row++) {
if (!offsetColumn) {
currentHexX = (col * this.side) + originX;
currentHexY = (row * this.height) + originY;
} else {
currentHexX = col * this.side + originX;
currentHexY = (row * this.height) + originY + (this.height * 0.5);
}
if (isDebug) {
debugText = col + "," + row;
}
this.drawHex(currentHexX, currentHexY, "#ddd", debugText);
}
offsetColumn = !offsetColumn;
}
};
HexagonGrid.prototype.drawHexAtColRow = function(column, row, color) {
var drawy = column % 2 == 0 ? (row * this.height) + this.canvasOriginY : (row * this.height) + this.canvasOriginY + (this.height / 2);
var drawx = (column * this.side) + this.canvasOriginX;
this.drawHex(drawx, drawy, color, "");
};
HexagonGrid.prototype.drawHex = function(x0, y0, fillColor, debugText) {
this.context.strokeStyle = "#000";
this.context.beginPath();
this.context.moveTo(x0 + this.width - this.side, y0);
this.context.lineTo(x0 + this.side, y0);
this.context.lineTo(x0 + this.width, y0 + (this.height / 2));
this.context.lineTo(x0 + this.side, y0 + this.height);
this.context.lineTo(x0 + this.width - this.side, y0 + this.height);
this.context.lineTo(x0, y0 + (this.height / 2));
if (fillColor) {
this.context.fillStyle = fillColor;
this.context.fill();
}
this.context.closePath();
this.context.stroke();
if (debugText) {
this.context.font = "8px";
this.context.fillStyle = "#000";
this.context.fillText(debugText, x0 + (this.width / 2) - (this.width/4), y0 + (this.height - 5));
}
};
//Recusivly step up to the body to calculate canvas offset.
HexagonGrid.prototype.getRelativeCanvasOffset = function() {
var x = 0, y = 0;
var layoutElement = this.canvas;
if (layoutElement.offsetParent) {
do {
x += layoutElement.offsetLeft;
y += layoutElement.offsetTop;
} while (layoutElement = layoutElement.offsetParent);
return { x: x, y: y };
}
}
//Uses a grid overlay algorithm to determine hexagon location
//Left edge of grid has a test to acuratly determin correct hex
HexagonGrid.prototype.getSelectedTile = function(mouseX, mouseY) {
var offSet = this.getRelativeCanvasOffset();
mouseX -= offSet.x;
mouseY -= offSet.y;
var column = Math.floor((mouseX) / this.side);
var row = Math.floor(
column % 2 == 0
? Math.floor((mouseY) / this.height)
: Math.floor(((mouseY + (this.height * 0.5)) / this.height)) - 1);
//Test if on left side of frame
if (mouseX > (column * this.side) && mouseX < (column * this.side) + this.width - this.side) {
//Now test which of the two triangles we are in
//Top left triangle points
var p1 = new Object();
p1.x = column * this.side;
p1.y = column % 2 == 0
? row * this.height
: (row * this.height) + (this.height / 2);
var p2 = new Object();
p2.x = p1.x;
p2.y = p1.y + (this.height / 2);
var p3 = new Object();
p3.x = p1.x + this.width - this.side;
p3.y = p1.y;
var mousePoint = new Object();
mousePoint.x = mouseX;
mousePoint.y = mouseY;
if (this.isPointInTriangle(mousePoint, p1, p2, p3)) {
column--;
if (column % 2 != 0) {
row--;
}
}
//Bottom left triangle points
var p4 = new Object();
p4 = p2;
var p5 = new Object();
p5.x = p4.x;
p5.y = p4.y + (this.height / 2);
var p6 = new Object();
p6.x = p5.x + (this.width - this.side);
p6.y = p5.y;
if (this.isPointInTriangle(mousePoint, p4, p5, p6)) {
column--;
if (column % 2 == 0) {
row++;
}
}
}
return { row: row, column: column };
};
HexagonGrid.prototype.sign = function(p1, p2, p3) {
return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
};
//TODO: Replace with optimized barycentric coordinate method
HexagonGrid.prototype.isPointInTriangle = function isPointInTriangle(pt, v1, v2, v3) {
var b1, b2, b3;
b1 = this.sign(pt, v1, v2) < 0.0;
b2 = this.sign(pt, v2, v3) < 0.0;
b3 = this.sign(pt, v3, v1) < 0.0;
return ((b1 == b2) && (b2 == b3));
};
HexagonGrid.prototype.clickEvent = function (e) {
var mouseX = e.pageX;
var mouseY = e.pageY;
var localX = mouseX - this.canvasOriginX;
var localY = mouseY - this.canvasOriginY;
var tile = this.getSelectedTile(localX, localY);
if (tile.column >= 0 && tile.row >= 0) {
var drawy = tile.column % 2 == 0 ? (tile.row * this.height) + this.canvasOriginY + 6 : (tile.row * this.height) + this.canvasOriginY + 6 + (this.height / 2);
var drawx = (tile.column * this.side) + this.canvasOriginX;
this.drawHex(drawx, drawy - 6, "rgba(110,110,70,0.3)", "");
}
};