-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzoom_18.js
320 lines (288 loc) · 7.94 KB
/
zoom_18.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Type Matrix is [ [a00, a10], [a01, a11] ]
// Type Vector is [ x, y ]
// Type Transform is [ Matrix, Vector ]
/**
* Multiply Scalar with Vector returns a Vector.
*
* @param {number} l scalar to multiply with
* @param {Array<number>} x 2D vector.
* @return {Array<number>}
*/
var scmult = function(l, x) {
return [ l * x[0], l * x[1] ];
};
/**
* Adding two vectors is another vector.
*
* @param {Array<number>} a 2D vector.
* @param {Array<number>} b 2D vector.
* @return {Array<number>} Sum vector.
*/
var vcadd = function(a, b) {
return [ a[0] + b[0], a[1] + b[1] ];
};
/**
* Subtracting two vectors is another vector.
*
* @param {Array<number>} a 2D vector.
* @param {Array<number>} b 2D vector.
* @return {Array<number>} Difference vector.
*/
var minus = function(a, b) {
return [ a[0] - b[0], a[1] - b[1] ];
};
/**
* Dot product of two vectors is scalar.
*
* @param {Array<number>} a 2D vector.
* @param {Array<number>} b 2D vector.
* @return {number} scalar inner product.
*/
var dot = function(a, b) {
return a[0] * b[0] + a[1] * b[1];
};
/**
* Exterior Product of two vectors is a pseudoscalar.
*
* @param {Array<number>} a 2D vector.
* @param {Array<number>} b 2D vector.
* @return {number} psuedo-scalar exterior product.
*/
var wedge = function(a, b) {
return a[0] * b[1] - a[1] * b[0];
};
/**
* Apply Matrix on Vector returns a Vector.
*
* @param {Array<Array<number>>} A 2x2 Matrix
* @param {Array<number>} x 2D vector.
* @return {Array<number>} 2D vector linear product.
*/
var apply = function(A, x) {
return vcadd(scmult(x[0], A[0]), scmult(x[1], A[1]));
};
/**
* Multiply two matrices.
*
* @param {Array<Array<number>>} A 2x2 Matrix
* @param {Array<Array<number>>} B 2x2 Matrix
* @return {Array<Array<number>>} A 2x2 Matrix
*/
var mult = function(A, B) {
return [ apply(A, B[0]), apply(A, B[1]) ];
};
/**
* Represents a transform operation, Ax + b
*
* @constructor
*
* @param {Array<Array<number>>} A 2x2 Matrix.
* @param {Array<number>} b 2D scalar.
*/
function Transform(A, b) {
this.A = A;
this.b = b;
}
/**
* Given CSS Transform representation of the class.
* @return {string} CSS 2D Transform.
*/
Transform.prototype.css = function() {
var A = this.A;
var b = this.b;
return 'matrix(' + A[0][0] + ',' + A[0][1] + ',' + A[1][0] + ',' + A[1][1] +
',' + b[0] + ',' + b[1] + ')';
};
/**
* Multiply two transforms.
* Defined as
* (T o U) (x) = T(U(x))
*
* Derivation:
* T(U(x))
* = T(U.A(x) + U.b)
* = T.A(U.A(x) + U.b)) + T.b
* = T.A(U.A(x)) + T.A(U.b) + T.b
*
* @param {Transform} T
* @param {Transform} U
* @return {Transform} T o U
*/
var cascade = function(T, U) {
return new Transform(mult(T.A, U.A), vcadd(apply(T.A, U.b), T.b));
};
/**
* Creates the default rotation matrix
*
* @param {number} c x-projection (r cos(theta))
* @param {number} s y-projection (r sin(theta))
* @return {Array<Array<number>>} Rotation matrix.
*/
var rotate = function(c, s) {
return [ [ c, s], [-s, c] ];
};
/**
* Returns matrix that transforms vector a to vector b.
*
* @param {Array<number>} a 2D vector.
* @param {Array<number>} b 2D vector.
* @return {Array<Array<number>>} Rotation + Scale matrix
*/
var rotscale = function(a, b) {
var alen = dot(a, a);
var sig = dot(a, b);
var del = wedge(a, b);
return rotate( sig / alen, del / alen);
};
/**
* Zoom is a similarity preserving transform from a pair of source
* points to a new pair of destination points.
*
* @param {Array<Array<number>>} s two source points.
* @param {Array<Array<number>>} d two destination points.
*
* @return {Transform} that moves point 's' to point 'd'
*/
var zoom = function(s, d) {
// Source vector.
var a = minus(s[1], s[0]);
// Destination vector.
var b = minus(d[1], d[0]);
// Rotation needed for source to dest vector.
var rs = rotscale(a, b);
// Position of s[0] if rotation is applied.
var rs0 = apply(rs, s[0]);
// Since d[0] = rs0 + t
var t = minus(d[0], rs0);
return new Transform(rs, t);
};
/**
* Weighted average of two vectors.
*
* @param {Array<number>} u 2D vector.
* @param {Array<number>} v 2D vector.
* @param {number} progress (from 0 to 1)
* @return {Array<number>} (1-p) u + (p) v
*/
var avgVector = function(u, v, progress) {
var u1 = scmult(1 - progress, u);
var v1 = scmult(progress, v);
return vcadd(u1, v1);
};
/**
* Weighted average of two vectors.
*
* @return {Array<Array<number>>} A 2D matrix.
* @return {Array<Array<number>>} B 2D matrix.
* @param {number} progress (from 0 to 1)
* @return {Array<Array<number>>} (1-p) A + (p) B
*/
var avgMatrix = function(A, B, progress) {
return [ avgVector(A[0], B[0], progress), avgVector(A[1], B[1], progress) ];
};
/**
* Weighted average of two transforms.
* @param {Transform} Z Source Transform
* @param {Transform} I Destination Transform
* @param {number} progress (from 0 to 1)
* @return {Transform} (1-p) Z + (p) I
*/
Transform.avg = function(Z, I, progress) {
return new Transform(avgMatrix(Z.A, I.A, progress), avgVector(Z.b, I.b, progress));
};
var identity = new Transform([[1, 0], [0, 1]], [0, 0]);
/**
* @constructor
* @export
* @param {Element} elem to attach zoom handler.
*/
function Zoom(elem) {
this.elem = elem;
this.zooming = false;
this.activeZoom = identity;
this.currentZoom = null;
this.srcCoords = [0, 0];
var me = this;
var tapped = false;
elem.style['transform-origin'] = '0 0';
var getCoords = function(t) {
var oX = elem.offsetLeft;
var oY = elem.offsetTop;
return [
[t[0].pageX - oX, t[0].pageY - oY],
[t[1].pageX - oX, t[1].pageY - oY]
];
};
elem.parentNode.addEventListener('touchstart', function(evt) {
var t = evt.touches;
if (!t) {
return false;
}
evt.preventDefault();
if (t.length === 2) {
me.srcCoords = getCoords(t);
me.zooming = true;
} else if (t.length == 1) {
if (!tapped) {
tapped = setTimeout(function() {
tapped = false;
}, 300);
} else {
tapped = false;
me.reset();
}
}
});
elem.parentNode.addEventListener('touchmove', function(evt) {
var t = evt.touches;
if (!t || t.length != 2) {
return false;
}
if (!me.zooming) {
// To prevent possible potential reorder of events.
return false;
}
evt.preventDefault();
var destCoords = getCoords(t);
me.currentZoom = zoom(me.srcCoords, destCoords);
var finalT = cascade(me.currentZoom, me.activeZoom);
me.update(finalT);
});
elem.parentNode.addEventListener('touchend', function() {
if (me.zooming) {
me.activeZoom = cascade(me.currentZoom, me.activeZoom);
me.zooming = false;
}
});
}
Zoom.prototype.update = function(finalT) {
this.elem.style.transform = finalT.css();
};
Zoom.prototype.reset = function() {
if (window.requestAnimationFrame) {
var Z = this.activeZoom;
var startTime = null;
var me = this;
var step = function(time) {
if (!startTime) {
startTime = time;
}
var progress = (time - startTime)/100;
if (progress > 1) {
me.activeZoom = identity;
me.zooming = false;
me.update(me.activeZoom);
} else {
me.update(Transform.avg(Z, identity, progress));
window.requestAnimationFrame(step);
}
};
window.requestAnimationFrame(step);
} else {
this.activeZoom = identity;
this.zooming = false;
this.update(this.activeZoom);
}
};
Zoom.prototype['reset'] = Zoom.prototype.reset;
window['Zoom'] = Zoom;