forked from nodetiles/nodetiles-core
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutfgrid.js
83 lines (71 loc) · 1.85 KB
/
utfgrid.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
/**
* Returns a UTFGrid.
*
* Usage:
* var gridJSON = UTFGrid.generate(64, function(coord) {
* // lookup the color at point coord in image
* // lookup feature associated with color
* // return feature
* });
*
* eg. the delegate function should return a feature
* when passed a {x: x, y: y} object
*
* Credit: Rob Brackett (mr0grog)
*/
var UTFGrid = function (size, delegate) {
this.size = size;
this.delegate = delegate;
this._features = [];
this._codePoints = {};
this._lastCodePoint = 32;
};
UTFGrid.generate = function(size, delegate) {
return (new UTFGrid(size, delegate)).encode();
};
UTFGrid.prototype = {
constructor: UTFGrid,
encode: function() {
return JSON.stringify(this.encodeAsObject());
},
encodeAsObject: function() {
var grid = {
grid: [],
keys: [""],
data: {}
};
for (var y = 0; y < this.size; y++) {
var gridRow = "";
for (var x = 0; x < this.size; x++) {
var feature = this.delegate({x: x, y: y});
if (feature) {
var id = this._features.indexOf(feature);
if (id === -1) {
id = this._features.push(feature) - 1;
grid.keys.push(id.toString(10));
grid.data[id] = feature;
}
gridRow += String.fromCharCode(this._codePointForId(id));
}
else {
gridRow += " ";
}
}
grid.grid.push(gridRow);
}
return grid;
},
_codePointForId: function(id) {
if (!this._codePoints[id]) {
// Skip '"' and '\'
var codePoint = ++this._lastCodePoint;
if (codePoint === 34 || codePoint === 92) {
codePoint += 1;
this._lastCodePoint += 1;
}
this._codePoints[id] = codePoint;
}
return this._codePoints[id];
}
};
module.exports = UTFGrid;