-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
122 lines (111 loc) · 4.19 KB
/
app.js
File metadata and controls
122 lines (111 loc) · 4.19 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
let hexjs = (function() {
let draw = SVG('drawing').viewbox(-10, -10, 930, 790).panZoom({zoomMin: 0.1, zoomMax: 10});
let app = new Vue({
el: '#hexmap',
data: {
num_hexes: 0,
hexes: [],
selected: null,
patterns: {},
showImagePicker: false,
images: [],
},
methods: {
imageClicked: function(evt) {
console.log(evt.srcElement.id);
this.showImagePicker = true;
},
imageSelected: function(evt) {
let el = findAncestor(evt.srcElement, 'hex-selected');
let id = el.firstChild.innerHTML.trim();
console.log(id);
let node = SVG.get(id);
node.fill(evt.srcElement.src)
this.showImagePicker = false;
this.selected.image = evt.srcElement.src;
},
resetZoom: function(evt) {
this.zoom(1)
}
}
})
function findAncestor(el, cls) {
while ((el = el.parentElement) && !el.classList.contains(cls));
return el;
}
function readTextFile(file, callback) {
let rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
function hexCoords(x_in, y_in) {
let R = 50
let r = Math.sqrt(3) / 2 * R
let x = (2 * r) * x_in + r + (y_in % 2) * r
let y = (1.5 * R) * y_in + R
return [
[Math.round(x + 0), Math.round(y + R)],
[Math.round(x + r), Math.round(y + R/2)],
[Math.round(x + r), Math.round(y - R/2)],
[Math.round(x + 0), Math.round(y - R)],
[Math.round(x - r), Math.round(y - R/2)],
[Math.round(x - r), Math.round(y + R/2)],
]
}
function addSVGPatterns(hexes) {
var n = 1
for (let hex_name in hexes) {
let hex = hexes[hex_name]
let tokens = hex.image.split('/')
let image = 'https://raw.githubusercontent.com/jholloc/hexmap/master/' + tokens[tokens.length - 1]
if (!(image in app.patterns)) {
app.patterns[image] = 'pattern' + n
n += 1
draw.defs().pattern(null, null).id(app.patterns[image]).width('100%').height('100%')
.attr({ patternContentUnits: 'objectBoundingBox', patternUnits: null, x: null, y: null })
.image(image, 1, 1)
app.images.push(image);
}
}
}
function addSVGPolygons(hexes) {
for (let hex_name in hexes) {
let hex = hexes[hex_name]
let coords = hexCoords(hex.q, hex.r)
let tokens = hex.image.split('/')
let image = 'https://raw.githubusercontent.com/jholloc/hexmap/master/' + tokens[tokens.length - 1]
//let pattern = app.patterns[image]
//let poly = draw.polygon(coords.join(' ')).fill('url(#' + pattern + ')').stroke('#999DA3')
let poly = draw.polygon(coords.join(' ')).fill(image).stroke('#999DA3')
poly.click(() => {
app.showImagePicker = false;
if (app.selected) {
app.selected.selectize(false, {deepSelect: true})
}
let pattern = SVG.get(poly.node.attributes.fill.value)
app.selected = poly;
app.selected.image = pattern.node.children[0].href.baseVal;
poly.selectize({deepSelect: true})
})
}
}
function onLoaded(text) {
var data = JSON.parse(text);
app.hexes = data['hexes'];
app.num_hexes = Object.keys(app.hexes).length;
addSVGPatterns(app.hexes);
addSVGPolygons(app.hexes);
}
return {
load: function() {
readTextFile("https://raw.githubusercontent.com/jholloc/hexmap/master/test.json", onLoaded);
}
};
})();
hexjs.load();