-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPanZoomRegion.js
121 lines (97 loc) · 2.26 KB
/
PanZoomRegion.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
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
function PanZoomRegion(opts) {
var camera = opts.camera;
var autoSetCamera = opts.autoSetCamera !== false;
this.zoomValue = 1;
var fullWidth = 100,
fullHeight = 100,
width = 1,
height = 1,
zoomMax = opts.zoomMax || 0.000001,
aspect = 1,
left = 0,
right = 1,
top = 0,
bottom = 1;
function setCamera() {
camera.setViewOffset(
width,
height,
left,
top * height,
right-left,
(bottom-top) * height
);
}
function precomposeViewport(outer) {
camera.setViewOffset(
outer.view.fullWidth,
outer.view.fullHeight,
outer.view.offsetX + left * outer.view.width,
outer.view.offsetY + top * outer.view.height,
outer.view.width * (right-left),
outer.view.height * (bottom-top)
);
}
function contain() {
var overlap = Math.min(left, 0);
left -= overlap;
right -= overlap;
overlap = Math.max(right, 1)-1;
left -= overlap;
right -= overlap;
var overlap = Math.min(top, 0);
top -= overlap;
bottom -= overlap;
overlap = Math.max(bottom, 1)-1;
top -= overlap;
bottom -= overlap;
left = Math.max(left, 0);
right = Math.min(right, 1);
top = Math.max(top, 0);
bottom = Math.min(bottom, 1);
}
function pan(x, y) {
x = x / fullWidth * (right - left);
left += x;
right += x;
y = y / fullHeight * (bottom - top);
top += y;
bottom += y;
contain();
if(autoSetCamera) setCamera();
}
function zoom(x, y, zoom) {
if(this.zoomValue <= zoomMax && zoom < 1) return;
zoom = Math.min(50, Math.max(0.01, zoom));
var ratioX = x / fullWidth;
var ratioY = y / fullHeight;
var focusX = left + ratioX * (right - left);
var focusY = top + ratioY * (bottom - top);
left = ((left - focusX) * zoom) + focusX;
right = ((right - focusX) * zoom) + focusX;
top = ((top - focusY) * zoom) + focusY;
bottom = ((bottom - focusY) * zoom) + focusY;
contain();
this.zoomValue *= zoom;
if(autoSetCamera) setCamera();
}
function setSize(w, h) {
fullWidth = w;
fullHeight = h;
height = h / w;
if(autoSetCamera) setCamera();
}
function reset() {
left = 0;
right = 1;
top = 0;
bottom = 1;
if(autoSetCamera) setCamera();
}
this.pan = pan;
this.zoom = zoom;
this.precomposeViewport = precomposeViewport;
this.setSize = setSize;
this.reset = reset;
}
module.exports = PanZoomRegion;