Repository navigation
Expand file tree
/
Copy pathzoomView.js
More file actions
141 lines (139 loc) · 3.33 KB
/
Copy pathzoomView.js
File metadata and controls
141 lines (139 loc) · 3.33 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//handles zoom and pan
//https://stackoverflow.com/questions/44009094/how-to-bound-image-pan-when-zooming-html-canvas/44015705#44015705
const zoomView = (() => {
const matrix = [1, 0, 0, 1, 0, 0]; // current view transform
const invMatrix = [1, 0, 0, 1, 0, 0]; // current inverse view transform
var m = matrix; // alias
var im = invMatrix; // alias
var scale = 1; // current scale
const bounds = {
topLeft: 0,
left: 0,
right: 200,
bottom: 200,
};
var useConstraint = true; // if true then limit pan and zoom to
// keep bounds within the current context
var maxScale = 1;
const workPoint1 = {x: 0, y: 0};
const workPoint2 = {x: 0, y: 0};
const wp1 = workPoint1; // alias
const wp2 = workPoint2; // alias
var ctx;
const pos = {// current position of origin
x: 0,
y: 0,
};
var dirty = true;
const API = {
canvasDefault() {
ctx.setTransform(1, 0, 0, 1, 0, 0);
},
apply() {
if (dirty) {
this.update();
}
ctx.setTransform(m[0], m[1], m[2], m[3], m[4], m[5]);
},
getScale() {
return scale;
},
getMaxScale() {
return maxScale;
},
matrix, // expose the matrix
invMatrix, // expose the inverse matrix
update() { // call to update transforms
dirty = false;
m[3] = m[0] = scale;
m[1] = m[2] = 0;
m[4] = pos.x;
m[5] = pos.y;
if (useConstraint) {
this.constrain();
}
this.invScale = 1 / scale;
// calculate the inverse transformation
var cross = m[0] * m[3] - m[1] * m[2];
im[0] = m[3] / cross;
im[1] = -m[1] / cross;
im[2] = -m[2] / cross;
im[3] = m[0] / cross;
},
constrain() {
maxScale = Math.min(
ctx.canvas.width / (bounds.right - bounds.left),
ctx.canvas.height / (bounds.bottom - bounds.top)
);
if (scale < maxScale) {
m[0] = m[3] = scale = maxScale;
}
wp1.x = bounds.left;
wp1.y = bounds.top;
this.toScreen(wp1, wp2);
if (wp2.x > 0) {
m[4] = pos.x -= wp2.x;
}
if (wp2.y > 0) {
m[5] = pos.y -= wp2.y;
}
wp1.x = bounds.right;
wp1.y = bounds.bottom;
this.toScreen(wp1, wp2);
if (wp2.x < ctx.canvas.width) {
m[4] = (pos.x -= wp2.x - ctx.canvas.width);
}
if (wp2.y < ctx.canvas.height) {
m[5] = (pos.y -= wp2.y - ctx.canvas.height);
}
},
toWorld(from_x, from_y) { // convert screen to world coords
var xx, yy;
var pointW = {};
if (dirty) {
this.update();
}
xx = from_x - m[4];
yy = from_y - m[5];
pointW.x = xx * im[0] + yy * im[2];
pointW.y = xx * im[1] + yy * im[3];
return pointW;
},
toScreen(from, point = {}){ // convert world coords to screen coords
if (dirty) {
this.update();
}
point.x = from.x * m[0] + from.y * m[2] + m[4];
point.y = from.x * m[1] + from.y * m[3] + m[5];
return point;
},
scaleAt(x_from, y_from, amount) { // at in screen coords
if (dirty) {
this.update();
}
scale *= amount;
pos.x = x_from - (x_from - pos.x) * amount;
pos.y = y_from - (y_from - pos.y) * amount;
dirty = true;
},
move(move_x, move_y) { // move is in screen coords
pos.x += move_x;
pos.y += move_y;
dirty = true;
},
setContext(context) {
ctx = context;
dirty = true;
},
setBounds(top, left, right, bottom) {
bounds.top = top;
bounds.left = left;
bounds.right = right;
bounds.bottom = bottom;
useConstraint = true;
dirty = true;
}
};
return API;
})();
export default zoomView;