forked from anitasv/zoom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zoom.js
647 lines (550 loc) · 17.1 KB
/
zoom.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
(function() {
// Type Vector is [ x, y ]
// Type Matrix is [ Vector, Vector ]
// 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;
// `matrix(elX, elDeg, elDeg, elY, scaleX, scaleY)`
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);
};
var justscale = function(a, b) {
var alen = Math.sqrt(dot(a, a));
var blen = Math.sqrt(dot(b, b));
var scale = blen / alen;
return rotate(scale, 0);
};
/**
* Zoom is a similarity preserving transform from a pair of source
* points to a new pair of destination points. If rotate it is false
* then it won't be maintaining the transfer precisely, but will only
* do scaling part of it.
*
* @param {Array<Array<number>>} s two source points.
* @param {Array<Array<number>>} d two destination points.
* @param {Boolean} rotate true - rotate; else scale.
*
* @return {Transform} that moves point 's' to point 'd'
*/
var zoom = function(s, d, rotate) {
// 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 = rotate ? rotscale(a, b) : justscale(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]);
/**
* Gives a default value for an input object.
*
* @param {Object} param input parameter, may be undefined
* @param {Object} val returned if param is undefined.
* @return {Object}
*/
var defaults = function(param, val) {
return (param === undefined) ? val : param;
};
/**
* Method to override json config objects with default
* values. If undefined in cfg corresponding value from
* cfg_def will be picked.
*
* @param {Object} cfg input parameter config.
* @param {Object} cfg_def default fallbacks.
* @return {Object} new config
*/
var default_config = function(cfg, cfg_def) {
var new_cfg = defaults(cfg, {});
for (var k in cfg_def) {
new_cfg[k] = defaults(new_cfg[k], cfg_def[k]);
}
return new_cfg;
};
/**
* @constructor
* @export
* @param {Element} elem to attach zoom handler.
* @param {Object} config to specify additiona features.
*/
function Zoom(elem, config, wnd) {
this.mayBeDoubleTap = null;
this.isAnimationRunning = false;
// SingleFinger = 1, DoubleFinger = 2, NoTouch = 0
this.curTouch = 0;
this.elem = elem;
this.activeZoom = identity;
this.resultantZoom = identity;
this.srcCoords = [0, 0];
this.destCoords = [0, 0];
var me = this;
this.config = default_config(config, {
'pan' : false,
'rotate' : true,
'minScale': null,
'maxScale': null
});
this.wnd = wnd || window;
elem.style['transform-origin'] = '0 0';
var getCoordsDouble = 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]
];
};
var getCoordsSingle = function(t) {
var oX = elem.offsetLeft;
var oY = elem.offsetTop;
var x = t[0].pageX - oX;
var y = t[0].pageY - oY;
return [
[x, y],
[x + 1, y + 1]
];
};
var getCoords = function(t) {
return t.length > 1 ? getCoordsDouble(t) : getCoordsSingle(t);
};
var setSrcAndDest = function(touches){
me.srcCoords = getCoords(touches);
me.destCoords = me.srcCoords;
};
var setDest = function(touches){
me.destCoords = getCoords(touches);
};
var handleTouchEvent = function(cb) {
return function(evt) {
evt.preventDefault();
if (me.isAnimationRunning){
return false;
}
var touches = evt.touches;
if (!touches) {
return false;
}
cb(touches);
};
};
var handleZoom = handleTouchEvent(function(touches) {
var numOfFingers = touches.length;
if (numOfFingers !== me.curTouch){
me.curTouch = numOfFingers;
me.finalize();
if (numOfFingers !== 0) {
setSrcAndDest(touches);
}
} else if (numOfFingers !== 0) {
setDest(touches);
me.previewZoom();
}
});
var handleTouchStart = handleTouchEvent(function(touches) {
if (touches.length === 1) {
if (me.mayBeDoubleTap !== null) {
clearTimeout(me.mayBeDoubleTap);
me.reset();
me.mayBeDoubleTap = null;
} else {
me.mayBeDoubleTap = setTimeout(function() {
me.mayBeDoubleTap = null;
}, 300);
}
}
});
this.pixelSize = elem.getBoundingClientRect();
elem.parentNode.addEventListener('touchstart', handleTouchStart);
elem.parentNode.addEventListener('touchstart', handleZoom);
elem.parentNode.addEventListener('touchmove', handleZoom);
elem.parentNode.addEventListener('touchend', handleZoom);
this.destroy = function() {
elem.style.removeProperty('transform');
elem.style.removeProperty('transform-origin');
elem.parentNode.removeEventListener('touchstart', handleTouchStart);
elem.parentNode.removeEventListener('touchstart', handleZoom);
elem.parentNode.removeEventListener('touchmove', handleZoom);
elem.parentNode.removeEventListener('touchend', handleZoom);
/**
* Remove elem references so gardbage collector can clean this up
*/
Object.keys(this).forEach(function(key) {
delete this[key];
}.bind(this));
}.bind(this);
}
Zoom.prototype.previewZoom = function() {
var additionalZoom = zoom(this.srcCoords, this.destCoords, this.config.rotate);
var resultantZoom = cascade(additionalZoom, this.activeZoom);
/**
* Prevent panning if scaling is greater than min/max
*/
if (this.checkPan(resultantZoom) === false) { return; }
/**
* Prevent panning if reaching the boundary of the image
*/
resultantZoom = this.checkBoundaries(resultantZoom);
this.resultantZoom = resultantZoom;
this.repaint();
};
Zoom.prototype.setZoom = function(newZoom) {
this.resultantZoom = newZoom;
this.repaint();
};
Zoom.prototype.finalize = function() {
this.activeZoom = this.resultantZoom;
};
Zoom.prototype.repaint = function() {
this.elem.style.transform = this.resultantZoom.css();
};
/**
* Toggle zoom-out/zoom-on
*
* @param {Boolean} useOriginalIdentity If triggering reset manually and want to reset scale to 1
*/
Zoom.prototype.reset = function(useOriginalIdentity) {
var newIdentity = identity;
if (this.wnd.requestAnimationFrame) {
this.isAnimationRunning = true;
var Z = this.activeZoom;
var startTime = null;
/**
* Enable doubletap to zoom image
*/
if (this.config.maxScale) {
if (!useOriginalIdentity && Z.A[0][0] === 1 && Z.A[1][1] === 1) {
var windowSize = {
height: (document.documentElement.clientHeight || window.innerHeight),
width: (document.documentElement.clientWidth || window.innerWidth)
};
var max = this.config.maxScale;
var middleX = ((this.pixelSize.width * this.config.maxScale) - windowSize.width) / 2;
middleX = middleX + (this.elem.offsetLeft);
middleX = middleX * -1;
var middleY = ((windowSize.height / 2) - (this.pixelSize.height / 2));
middleY = middleY + (((this.pixelSize.height * this.config.maxScale) - windowSize.height) / 2);
middleY = middleY * -1;
newIdentity = new Transform([[max, 0], [0, max]], [middleX, middleY]);
}
}
var me = this;
var step = function(time) {
if (!startTime) {
startTime = time;
}
var progress = (time - startTime)/100;
if (progress >= 1) {
me.setZoom(newIdentity);
/**
* Grace period to prevent animation glitches
*/
setTimeout(function() {
me.isAnimationRunning = false;
}, 100);
} else {
me.setZoom(Transform.avg(Z, newIdentity, progress));
me.wnd.requestAnimationFrame(step);
}
};
this.wnd.requestAnimationFrame(step);
} else {
this.setZoom(newIdentity);
}
};
Zoom.prototype.checkPan = function(resultantZoom) {
var proceed = true;
var A = resultantZoom.A;
/**
* If scale is less than `minScale`
*/
var minScale = this.config.minScale;
if (minScale) {
if (A[0][0] <= minScale && A[1][1] <= minScale) {
proceed = false;
this.finalize();
this.reset(true);
}
}
/**
* If scale is more than `maxScale`
*/
var maxScale = this.config.maxScale;
if (maxScale && (A[0][0] > maxScale && A[1][1] > maxScale)) {
proceed = false;
}
return proceed;
};
Zoom.prototype.checkBoundaries = function(resultantZoom) {
var A = resultantZoom.A;
var b = resultantZoom.b;
var boundaries = this.config.boundaries;
if (boundaries === true) {
var width = this.pixelSize.width * A[0][0];
var height = this.pixelSize.height * A[1][1];
var windowSize = {
height: (document.documentElement.clientHeight || window.innerHeight),
width: (document.documentElement.clientWidth || window.innerWidth)
};
var xLeft, xRight;
var yTop, yBottom;
/**
* Portrait viewport
*/
if (windowSize.height > windowSize.width) {
/**
* Prevent image going further right
*/
if (b[0] >= 0) {
b[0] = 0;
}
/**
* Prevent image going further left
*/
if (Math.abs(b[0]) >= (width - windowSize.width)) {
b[0] = (width - windowSize.width) * -1;
}
/**
* Prevent image going further up
*/
var yTopFormula = ((windowSize.height - this.pixelSize.height) / 2) * -1;
if (b[1] <= yTopFormula) {
yTop = yTopFormula;
}
/**
* Prevent image going further down
*/
var yBottomFormula = ((windowSize.height - this.pixelSize.height) / 2) - (height - this.pixelSize.height);
if (b[1] > yBottomFormula) {
yBottom = yBottomFormula;
}
if (height < windowSize.height) {
if (yTop && !yBottom) {
b[1] = yTop;
} else if (yBottom && !yTop) {
b[1] = yBottom;
}
} else {
if (yTop && !yBottom) {
b[1] = yBottomFormula;
} else if (yBottom && !yTop) {
b[1] = yTopFormula;
}
}
} else {
/**
* Landscape viewport
*/
/**
* X Axis
*/
/**
* Prevent image going further right
*/
var xRightFormula = this.elem.offsetLeft * -1;
if (b[0] >= xRightFormula) {
xRight = xRightFormula;
}
/**
* Prevent image going further left
*/
var xLeftFormula = ((windowSize.width - this.pixelSize.width) / 2) - (width - this.pixelSize.width);
if (b[0] <= xLeftFormula) {
xLeft = xLeftFormula;
}
/**
* Prevent glitching on images whose width is smaller than the viewport
*/
var widerImage = (width > windowSize.width);
if (xRight && !xLeft) {
b[0] = (widerImage) ? xRight : xLeftFormula;
} else if (xLeft && !xRight) {
b[0] = (widerImage) ? xLeft : xRightFormula;
}
/**
* Y Axis
*/
if (b[1] >= 0) {
yTop = 0;
} else if (Math.abs(b[1]) >= (height - windowSize.height)) {
yBottom = (height - windowSize.height) * -1;
}
if (yTop >= 0 && !yBottom) {
b[1] = yTop;
} else if (yBottom && !yTop) {
b[1] = yBottom;
}
}
resultantZoom.A = A;
resultantZoom.b = b;
}
return resultantZoom;
};
window.Zoom = Zoom;
})();