-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
175 lines (162 loc) · 4.98 KB
/
main.js
File metadata and controls
175 lines (162 loc) · 4.98 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
function main() {
var canvas = document.querySelector('canvas');
function initSize() {
var percentageWidth = 50.0;
canvas.width=window.innerWidth * (percentageWidth / 100.0);
canvas.height=window.innerHeight;
}
var c = canvas.getContext('2d');
var defaultSettings = [
new Setting(
"Hex size",
"number",
"60",
"4",
"px (minor diameter)"
),
new Setting(
"Random iterations",
"number",
"1",
"4",
" Change to get a different set of random colors"
),
new Setting(
"Transparency",
"checkbox",
"",
"",
" Makes everything 50% opaque"
),
new Setting(
"View scale",
"number",
"0",
"4"
),
new Setting(
"No zoom on scaling",
"checkbox",
"",
"",
" don't change hex size when changing scale to roughly match view area"
),
new Setting(
"View coordinate X",
"number",
"0",
"4"
),
new Setting(
"View coordinate Y",
"number",
"0",
"4"
)
]
makeSettingsInterface(defaultSettings);
var mouseController = new MouseController();
canvas.addEventListener('mousewheel', function(event){
mouseController.wheel(event);
return false;
}, false);
var keyController = new KeyboardController();
window.addEventListener('keydown', function(event){
keyController.keyDown(event);
return false;
}, false);
var hexagons = new Hexagons();
var hexagonSettings = new Object();
hexagonSettings.rngSettings = new Object();
var sceneSettings = new Object();
function redraw(event=null) {
initSize();
if(event != null && event.target == document.getElementById('View scale')){
var changeDirection = {true: -1, false: 1};
changeDirection = changeDirection[
sceneSettings.viewScale > loadSetting('View scale')
];
var changeAmount = Math.pow(2, changeDirection);
newHexSize = loadSetting('Hex size');
while(sceneSettings.viewScale != loadSetting('View scale')){
if(!loadSetting('No zoom on scaling')){
newHexSize = newHexSize * changeAmount;
}
sceneSettings.viewScale += changeDirection;
document.getElementById('View coordinate X').value = Math.floor(
document.getElementById('View coordinate X').value/changeAmount
);
document.getElementById('View coordinate Y').value = Math.floor(
document.getElementById('View coordinate Y').value/changeAmount
);
}
document.getElementById('Hex size').value = parseInt(newHexSize);
}
sceneSettings.hexMinorDiameter =
loadSetting('Hex size');
sceneSettings.hexMajorDiameter =
sceneSettings.hexMinorDiameter / Math.cos(Math.PI / 6);
sceneSettings.columnSpacing =
sceneSettings.hexMajorDiameter * 3 / 4;
sceneSettings.rowSpacing =
sceneSettings.hexMinorDiameter;
sceneSettings.transparency =
loadSetting('Transparency');
sceneSettings.viewScale =
loadSetting('View scale');
sceneSettings.noZoomOnScaling =
loadSetting('No zoom on scaling');
sceneSettings.hexOffsetX =
loadSetting('View coordinate X');
sceneSettings.hexOffsetY =
loadSetting('View coordinate Y');
hexagonSettings.rngSettings.minimumIterations =
loadSetting('Random iterations');
sceneSettings.scale =
loadSetting('View scale');
sceneSettings.canvas = canvas;
sceneSettings.canvasContext = c;
sceneSettings.visibility=sceneVisibility(
canvas,
sceneSettings.hexMajorDiameter,
sceneSettings.hexMinorDiameter,
sceneSettings.scale,
sceneSettings.hexOffsetX,
sceneSettings.hexOffsetY
);
hexagons.generateVisible(sceneSettings.visibility, hexagonSettings);
// console.log(JSON.stringify(hexagons).length);
// console.log(LZString.compressToBase64(JSON.stringify(hexagons)).length);
// console.log(LZString.compressToBase64(JSON.stringify(hexagons));
document.getElementById('status').value='Rendering...';
setTimeout(function(){
drawScene(sceneSettings, hexagons);
}, 0);
function craftURL(d, l) {
url = String(l).split('?')[0];
url += '?';
d.forEach(function(defaultSetting) {
var current = loadSetting(defaultSetting.displayNameAndId);
if(current != defaultSetting.value){
url += encodeURIComponent(defaultSetting.displayNameAndId);
if(defaultSetting.inputType != "checkbox"){
url += '=' + encodeURIComponent(current);
}
url += '&';
}
});
return url.slice(0, url.length-1);
}
document.getElementById('viewURL').value = craftURL(
defaultSettings,
document.location
);
}
redraw();
window.addEventListener('resize', redraw);
for(var i = 0; i < defaultSettings.length; i++){
document.getElementById(
defaultSettings[i].displayNameAndId
).addEventListener('change', redraw);
}
}