-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
81 lines (75 loc) · 2.61 KB
/
test.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
var app = new Vue({
el: '#hexview',
data: {
selected: []
}
})
var draw = SVG('drawing').size(1200, 1200)
function readTextFile(file, callback) {
var 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 hex_coords(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)],
]
}
let selected = null
//usage:
readTextFile("https://raw.githubusercontent.com/jholloc/hexmap/master/test.json", function(text) {
var data = JSON.parse(text);
let patterns = {}
var n = 1
for (let hex_name in data['hexes']) {
let hex = data['hexes'][hex_name]
let tokens = hex.image.split('/')
let image = 'https://raw.githubusercontent.com/jholloc/hexmap/master/' + tokens[tokens.length - 1]
if (!(image in patterns)) {
patterns[image] = 'pattern' + n
n += 1
draw.defs().pattern(null, null).id(patterns[image]).width('100%').height('100%')
.attr({ patternContentUnits: 'objectBoundingBox', patternUnits: null, x: null, y: null })
.image(image, 1, 1)
}
}
for (let hex_name in data['hexes']) {
let hex = data['hexes'][hex_name]
let coords = hex_coords(hex.q, hex.r)
let tokens = hex.image.split('/')
let image = 'https://raw.githubusercontent.com/jholloc/hexmap/master/' + tokens[tokens.length - 1]
let pattern = patterns[image]
console.log(pattern)
let poly = draw.polygon(coords.join(' ')).fill('url(#' + pattern + ')').stroke('#999DA3')
poly.click(() => {
if (selected) {
selected.selectize(false, {deepSelect: true})
}
let pattern = SVG.get(poly.node.attributes.fill.value)
app.selected = [
{ label: 'hexid', value: poly.node.id, type: 'text' },
{ label: 'fill', value: pattern.node.children[0].href.baseVal, type: 'image' }
]
poly.selectize({deepSelect: true})
selected = poly
})
// .animate(10000).rotate(360).loop()
// poly.selectize();
// <polygon fill="url(#pattern1)" points="43,100 86,75 86,25 43,0 0,25 0,75" style="stroke: #999DA3;"/>
}
});