Skip to content

Commit 7f6ee32

Browse files
committed
update panzoom library
1 parent 2b89c02 commit 7f6ee32

File tree

1 file changed

+47
-33
lines changed

1 file changed

+47
-33
lines changed

js/plugins/jquery.panzoom.js

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**
22
* @license jquery.panzoom.js v3.2.2
3-
* Updated: Sat Aug 27 2016
3+
* Updated: Wed Jun 28 2017
44
* Add pan and zoom functionality to any element
55
* Copyright (c) timmy willison
66
* Released under the MIT license
77
* https://github.com/timmywil/jquery.panzoom/blob/master/MIT-License.txt
88
*/
99

10-
(function(global, factory) {
10+
(function(global, factory) {
1111
// AMD
1212
if (typeof define === 'function' && define.amd) {
1313
define([ 'jquery' ], function(jQuery) {
@@ -297,12 +297,15 @@
297297
which: 1,
298298

299299
// The increment at which to zoom
300-
// adds/subtracts to the scale each time zoomIn/Out is called
300+
// Should be a number greater than 0
301301
increment: 0.3,
302302

303-
// Turns on exponential zooming
304-
// If false, zooming will be incremented linearly
305-
exponential: true,
303+
// When no scale is passed, this option tells
304+
// the `zoom` method to increment
305+
// the scale *linearly* based on the increment option.
306+
// This often ends up looking like very little happened at larger zoom levels.
307+
// The default is to multiply/divide the scale based on the increment.
308+
linearZoom: false,
306309

307310
// Pan only when the scale is greater than minScale
308311
panOnlyWhenZoomed: false,
@@ -509,6 +512,15 @@
509512
return matrix || [ 1, 0, 0, 1, 0, 0 ];
510513
},
511514

515+
/**
516+
* Get the current scale.
517+
* @param {String} [transform] matrix-formatted transform value
518+
* @returns {Number} Current scale relative to the initial scale (height / width = 1)
519+
*/
520+
getScale: function(matrix) {
521+
return Math.sqrt(Math.pow(matrix[0], 2) + Math.pow(matrix[1], 2));
522+
},
523+
512524
/**
513525
* Given a matrix object, quickly set the current matrix of the element
514526
* @param {Array|String} matrix
@@ -526,7 +538,7 @@
526538
if (typeof matrix === 'string') {
527539
matrix = this.getMatrix(matrix);
528540
}
529-
var scale = +matrix[0];
541+
var scale = this.getScale(matrix);
530542
var contain = typeof options.contain !== 'undefined' ? options.contain : this.options.contain;
531543

532544
// Apply containment
@@ -690,31 +702,29 @@
690702
if (options.disableZoom) { return; }
691703
var animate = false;
692704
var matrix = options.matrix || this.getMatrix();
693-
var startScale = +matrix[0];
705+
var surfaceM = new Matrix(matrix);
706+
var startScale = this.getScale(matrix);
694707

695708
// Calculate zoom based on increment
696709
if (typeof scale !== 'number') {
697-
// Just use a number a little greater than 1
698-
// Below 1 can use normal increments
699-
if (options.exponential && startScale - options.increment >= 1) {
700-
scale = Math[scale ? 'sqrt' : 'pow'](startScale, 2);
710+
if (options.linearZoom) {
711+
scale = 1 + (options.increment * (scale ? -1 : 1)) / startScale;
701712
} else {
702-
scale = startScale + (options.increment * (scale ? -1 : 1));
713+
scale = scale ? (1 / (1 + options.increment)) : (1 + options.increment);
703714
}
704715
animate = true;
716+
} else {
717+
scale = 1 / startScale;
705718
}
706719

707720
// Constrain scale
708-
if (scale > options.maxScale) {
709-
scale = options.maxScale;
710-
} else if (scale < options.minScale) {
711-
scale = options.minScale;
712-
}
721+
scale = Math.max(Math.min(scale, options.maxScale / startScale), options.minScale / startScale);
722+
var m = surfaceM.x(new Matrix(scale, 0, 0, 0, (typeof options.dValue === 'number' ? options.dValue / startScale : scale), 0));
713723

714724
// Calculate focal point based on scale
715725
var focal = options.focal;
716726
if (focal && !options.disablePan) {
717-
// Adapted from code by Florian Günther
727+
// Adapted from code by Florian Günther
718728
// https://github.com/florianguenther/zui53
719729
this.resetDimensions();
720730
var dims = options.dims = this.dimensions;
@@ -729,21 +739,21 @@
729739
}
730740

731741
var clientV = new Vector(clientX, clientY, 1);
732-
var surfaceM = new Matrix(matrix);
733742
// Supply an offset manually if necessary
734743
var o = this.parentOffset || this.$parent.offset();
735744
var offsetM = new Matrix(1, 0, o.left - this.$doc.scrollLeft(), 0, 1, o.top - this.$doc.scrollTop());
736745
var surfaceV = surfaceM.inverse().x(offsetM.inverse().x(clientV));
737-
var scaleBy = scale / matrix[0];
738-
surfaceM = surfaceM.x(new Matrix([scaleBy, 0, 0, scaleBy, 0, 0]));
746+
surfaceM = surfaceM.x(new Matrix([scale, 0, 0, scale, 0, 0]));
739747
clientV = offsetM.x(surfaceM.x(surfaceV));
740748
matrix[4] = +matrix[4] + (clientX - clientV.e(0));
741749
matrix[5] = +matrix[5] + (clientY - clientV.e(1));
742750
}
743751

744752
// Set the scale
745-
matrix[0] = scale;
746-
matrix[3] = typeof options.dValue === 'number' ? options.dValue : scale;
753+
matrix[0] = m.e(0);
754+
matrix[1] = m.e(3);
755+
matrix[2] = m.e(1);
756+
matrix[3] = m.e(4);
747757

748758
// Calling zoom may still pan the element
749759
this.setMatrix(matrix, {
@@ -754,7 +764,7 @@
754764

755765
// Trigger zoom event
756766
if (!options.silent) {
757-
this._trigger('zoom', matrix[0], options);
767+
this._trigger('zoom', scale, options);
758768
}
759769
},
760770

@@ -960,7 +970,7 @@
960970
if (!options.disablePan || !options.disableZoom) {
961971
events[ str_start ] = function(e) {
962972
var touches;
963-
if (e.type === 'touchstart' ?
973+
if (/touchstart|pointerdown/.test(e.type) ? // fix double events pointer/touch on Chrome >=55 #303
964974
// Touch
965975
(touches = e.touches || e.originalEvent.touches) &&
966976
((touches.length === 1 && !options.disablePan) || touches.length === 2) :
@@ -1128,6 +1138,12 @@
11281138
if (this.panning) {
11291139
return;
11301140
}
1141+
1142+
var type = event.type;
1143+
if (window.PointerEvent && (type === 'touchstart')) { // fix double events pointer/touch on Chrome >=55 #303
1144+
// return false;
1145+
}
1146+
11311147
var moveEvent, endEvent,
11321148
startDistance, startScale, startMiddle,
11331149
startPageX, startPageY, touch;
@@ -1139,7 +1155,6 @@
11391155
var origPageX = +original[4];
11401156
var origPageY = +original[5];
11411157
var panOptions = { matrix: matrix, animate: 'skip' };
1142-
var type = event.type;
11431158

11441159
// Use proper events
11451160
if (type === 'pointerdown') {
@@ -1163,9 +1178,6 @@
11631178
// Remove any transitions happening
11641179
this.transition(true);
11651180

1166-
// Indicate that we are currently panning
1167-
this.panning = true;
1168-
11691181
// Trigger start event
11701182
this._trigger('start', event, touches);
11711183

@@ -1176,7 +1188,7 @@
11761188
return;
11771189
}
11781190
startDistance = self._getDistance(touches);
1179-
startScale = +matrix[0];
1191+
startScale = self.getScale(matrix);
11801192
startMiddle = self._getMiddle(touches);
11811193
return;
11821194
}
@@ -1199,7 +1211,7 @@
11991211

12001212
var move = function(e) {
12011213
var coords;
1202-
e.preventDefault();
1214+
e.stopPropagation(); // chrome passive event warning https://www.chromestatus.com/features/5093566007214080 #328
12031215
touches = e.touches || e.originalEvent.touches;
12041216
setStart(e, touches);
12051217

@@ -1210,7 +1222,6 @@
12101222
var middle = self._getMiddle(touches);
12111223
var diff = self._getDistance(touches) - startDistance;
12121224

1213-
// Set zoom
12141225
self.zoom(diff * (options.increment / 100) + startScale, {
12151226
focal: middle,
12161227
matrix: matrix,
@@ -1233,6 +1244,9 @@
12331244
coords = e;
12341245
}
12351246

1247+
// Indicate that we are currently panning
1248+
this.panning = true;
1249+
12361250
self.pan(
12371251
origPageX + coords.pageX - startPageX,
12381252
origPageY + coords.pageY - startPageY,

0 commit comments

Comments
 (0)