-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.js
More file actions
134 lines (129 loc) · 4.5 KB
/
canvas.js
File metadata and controls
134 lines (129 loc) · 4.5 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
function hexCoordsToCanvasCoords(screenX, screenY, evenHexColumn, settings) {
var x = screenX * settings.columnSpacing;
var y = screenY * settings.rowSpacing + screenX * settings.rowSpacing * 0.5;
return {x: x + settings.canvas.width/2, y: y + settings.canvas.height/2};
}
function drawSimpleHexagon(c, centerX, centerY, d, color, holeFactor=0.0) {
r = d/2;
R = r / Math.cos(Math.PI / 6);
c.beginPath();
c.moveTo(centerX + R, centerY);
c.lineTo(centerX + R/2, centerY + r);
c.lineTo(centerX - R/2, centerY + r);
c.lineTo(centerX - R, centerY);
c.lineTo(centerX - R/2, centerY - r);
c.lineTo(centerX + R/2, centerY - r);
c.closePath();
if(holeFactor > 0){
hole_r = r * holeFactor;
hole_R = R * holeFactor;
c.moveTo(centerX + hole_R, centerY);
c.lineTo(centerX + hole_R/2, centerY - hole_r);
c.lineTo(centerX - hole_R/2, centerY - hole_r);
c.lineTo(centerX - hole_R, centerY);
c.lineTo(centerX - hole_R/2, centerY + hole_r);
c.lineTo(centerX + hole_R/2, centerY + hole_r);
c.closePath();
}
c.fillStyle = color;
c.fill();
}
function drawHexagon(c, centerX, centerY, d, hexagon, highl) {
drawSimpleHexagon(c, centerX, centerY, d, hexagon.fillColor);
fontFactor = 4; // About as big as you can go for printing 3 digit coordinates
c.font = Math.floor(d/fontFactor) + "px Arial";
c.fillStyle = invertColor(c.fillStyle, 1);
c.textAlign = "center";
c.textBaseline = "middle";
c.fillText(hexagon.text, centerX, centerY);
}
function drawScene(sceneSettings, hexagons) {
sceneSettings.canvasContext.globalAlpha = 1 - 0.5*sceneSettings.transparency;
sceneSettings.visibility.list.forEach(function(coordinates){
var hexagon = hexagons.getHex(
sceneSettings.scale,
coordinates.x,
coordinates.y
);
var screenCoords = hexCoordsToCanvasCoords(
hexagon.x - sceneSettings.hexOffsetX,
hexagon.y - sceneSettings.hexOffsetY,
sceneSettings.hexOffsetX%2==0,
sceneSettings
);
var centralHex = (
screenCoords.x == sceneSettings.canvas.width/2 &&
screenCoords.y == sceneSettings.canvas.height/2);
drawHexagon(
sceneSettings.canvasContext,
screenCoords.x,
screenCoords.y,
sceneSettings.hexMinorDiameter,
hexagon
);
if(centralHex){
drawSimpleHexagon(
sceneSettings.canvasContext,
screenCoords.x,
screenCoords.y,
sceneSettings.hexMinorDiameter,
"#000000",
0.85
);
drawSimpleHexagon(
sceneSettings.canvasContext,
screenCoords.x,
screenCoords.y,
sceneSettings.hexMinorDiameter * 0.95,
"#FFFFFF",
0.95
);
}
});
// // Draws 1/4 and 3/4 lines for height and width. If you multiply all canvas
// // width/height references by 0.5 this is really useful for debugging
// // visibility calculations.
// sceneSettings.canvasContext.strokeRect(0, sceneSettings.canvas.height/4,
// sceneSettings.canvas.width, sceneSettings.canvas.height/2);
// sceneSettings.canvasContext.strokeRect(sceneSettings.canvas.width/4, 0,
// sceneSettings.canvas.width/2, sceneSettings.canvas.height);
document.getElementById('status').value='Finished';
}
function sceneVisibility(
canvas,
hexMajorDiameter,
hexMinorDiameter,
scale,
offsetX,
offsetY
){
var visibility = new Object();
// First we delimit a rhombus which encapsulates the area on screen...
visibility.firstColumn = -Math.ceil(
(canvas.width-hexMajorDiameter/2)/(3*(hexMajorDiameter/2))
); // This is elegant but technically wrong when hexHeightsFloor==0
visibility.finalColumn = -visibility.firstColumn;
var hexHeightsFloor = Math.floor(canvas.height / hexMinorDiameter);
var extraRows = 1 * Number(hexHeightsFloor%2==0);
firstRowAtZero = 0-Math.ceil(hexHeightsFloor/2);
finalRowAtZero = 0+Math.ceil(hexHeightsFloor/2);
visibility.firstRow = firstRowAtZero-Math.ceil(
visibility.finalColumn / 2
);
visibility.finalRow = -visibility.firstRow;
visibility.list = [];
for(var i = visibility.firstColumn; i <= visibility.finalColumn; i++){
for(var j = visibility.firstRow; j <= visibility.finalRow; j++){
var x = i + offsetX;
var y = j + offsetY;
// if(!(y+Math.ceil(x/2-(x%2*hexHeightsFloor%2)) < firstRowAtZero)){
if(
!(j+i/2+(Math.abs(i)%2)*((hexHeightsFloor+1)%2) < firstRowAtZero) &&
!(j+i/2-(Math.abs(i)%2)*((hexHeightsFloor+1)%2) > finalRowAtZero)
){
visibility.list.push({scale:scale, x:x, y:y});
}
}
}
return visibility;
}