-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtile.js
83 lines (69 loc) · 2.34 KB
/
tile.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
function defaultScale(t) {
return t.k;
}
function defaultTranslate(t) {
return [t.x, t.y];
}
function constant(x) {
return function() {
return x;
};
}
export default function() {
let x0 = 0, y0 = 0, x1 = 960, y1 = 500;
let clampX = true, clampY = true;
let tileSize = 256;
let scale = defaultScale;
let translate = defaultTranslate;
let zoomDelta = 0;
function tile() {
const scale_ = +scale.apply(this, arguments);
const translate_ = translate.apply(this, arguments);
const z = Math.log2(scale_ / tileSize);
const z0 = Math.round(Math.max(z + zoomDelta, 0));
const k = Math.pow(2, z - z0) * tileSize;
const x = +translate_[0] - scale_ / 2;
const y = +translate_[1] - scale_ / 2;
const xmin = Math.max(clampX ? 0 : -Infinity, Math.floor((x0 - x) / k));
const xmax = Math.min(clampX ? 1 << z0 : Infinity, Math.ceil((x1 - x) / k));
const ymin = Math.max(clampY ? 0 : -Infinity, Math.floor((y0 - y) / k));
const ymax = Math.min(clampY ? 1 << z0 : Infinity, Math.ceil((y1 - y) / k));
const tiles = [];
for (let y = ymin; y < ymax; ++y) {
for (let x = xmin; x < xmax; ++x) {
tiles.push([x, y, z0]);
}
}
tiles.translate = [x / k, y / k];
tiles.scale = k;
return tiles;
}
tile.size = function(_) {
return arguments.length ? (x0 = y0 = 0, x1 = +_[0], y1 = +_[1], tile) : [x1 - x0, y1 - y0];
};
tile.extent = function(_) {
return arguments.length ? (x0 = +_[0][0], y0 = +_[0][1], x1 = +_[1][0], y1 = +_[1][1], tile) : [[x0, y0], [x1, y1]];
};
tile.scale = function(_) {
return arguments.length ? (scale = typeof _ === "function" ? _ : constant(+_), tile) : scale;
};
tile.translate = function(_) {
return arguments.length ? (translate = typeof _ === "function" ? _ : constant([+_[0], +_[1]]), tile) : translate;
};
tile.zoomDelta = function(_) {
return arguments.length ? (zoomDelta = +_, tile) : zoomDelta;
};
tile.tileSize = function(_) {
return arguments.length ? (tileSize = +_, tile) : tileSize;
};
tile.clamp = function(_) {
return arguments.length ? (clampX = clampY = !!_, tile) : clampX && clampY;
};
tile.clampX = function(_) {
return arguments.length ? (clampX = !!_, tile) : clampX;
};
tile.clampY = function(_) {
return arguments.length ? (clampY = !!_, tile) : clampY;
};
return tile;
}