zoom.js
4.1 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
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
_initZoom: function () {
this.scrollerStyle[utils.style.transformOrigin] = '0 0';
},
_zoomStart: function (e) {
var c1 = Math.abs( e.touches[0].pageX - e.touches[1].pageX ),
c2 = Math.abs( e.touches[0].pageY - e.touches[1].pageY );
this.touchesDistanceStart = Math.sqrt(c1 * c1 + c2 * c2);
this.startScale = this.scale;
this.originX = Math.abs(e.touches[0].pageX + e.touches[1].pageX) / 2 + this.wrapperOffset.left - this.x;
this.originY = Math.abs(e.touches[0].pageY + e.touches[1].pageY) / 2 + this.wrapperOffset.top - this.y;
this._execEvent('zoomStart');
},
_zoom: function (e) {
if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
return;
}
if ( this.options.preventDefault ) {
e.preventDefault();
}
var c1 = Math.abs( e.touches[0].pageX - e.touches[1].pageX ),
c2 = Math.abs( e.touches[0].pageY - e.touches[1].pageY ),
distance = Math.sqrt( c1 * c1 + c2 * c2 ),
scale = 1 / this.touchesDistanceStart * distance * this.startScale,
lastScale,
x, y;
this.scaled = true;
if ( scale < this.options.zoomMin ) {
scale = 0.5 * this.options.zoomMin * Math.pow(2.0, scale / this.options.zoomMin);
} else if ( scale > this.options.zoomMax ) {
scale = 2.0 * this.options.zoomMax * Math.pow(0.5, this.options.zoomMax / scale);
}
lastScale = scale / this.startScale;
x = this.originX - this.originX * lastScale + this.startX;
y = this.originY - this.originY * lastScale + this.startY;
this.scale = scale;
this.scrollTo(x, y, 0);
},
_zoomEnd: function (e) {
if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
return;
}
if ( this.options.preventDefault ) {
e.preventDefault();
}
var newX, newY,
lastScale;
this.isInTransition = 0;
this.initiated = 0;
if ( this.scale > this.options.zoomMax ) {
this.scale = this.options.zoomMax;
} else if ( this.scale < this.options.zoomMin ) {
this.scale = this.options.zoomMin;
}
// Update boundaries
this.refresh();
lastScale = this.scale / this.startScale;
newX = this.originX - this.originX * lastScale + this.startX;
newY = this.originY - this.originY * lastScale + this.startY;
if ( newX > 0 ) {
newX = 0;
} else if ( newX < this.maxScrollX ) {
newX = this.maxScrollX;
}
if ( newY > 0 ) {
newY = 0;
} else if ( newY < this.maxScrollY ) {
newY = this.maxScrollY;
}
if ( this.x != newX || this.y != newY ) {
this.scrollTo(newX, newY, this.options.bounceTime);
}
this.scaled = false;
this._execEvent('zoomEnd');
},
zoom: function (scale, x, y, time) {
if ( scale < this.options.zoomMin ) {
scale = this.options.zoomMin;
} else if ( scale > this.options.zoomMax ) {
scale = this.options.zoomMax;
}
if ( scale == this.scale ) {
return;
}
var relScale = scale / this.scale;
x = x === undefined ? this.wrapperWidth / 2 : x;
y = y === undefined ? this.wrapperHeight / 2 : y;
time = time === undefined ? 300 : time;
x = x + this.wrapperOffset.left - this.x;
y = y + this.wrapperOffset.top - this.y;
x = x - x * relScale + this.x;
y = y - y * relScale + this.y;
this.scale = scale;
this.refresh(); // update boundaries
if ( x > 0 ) {
x = 0;
} else if ( x < this.maxScrollX ) {
x = this.maxScrollX;
}
if ( y > 0 ) {
y = 0;
} else if ( y < this.maxScrollY ) {
y = this.maxScrollY;
}
this.scrollTo(x, y, time);
},
_wheelZoom: function (e) {
var wheelDeltaY,
deltaScale,
that = this;
// Execute the zoomEnd event after 400ms the wheel stopped scrolling
clearTimeout(this.wheelTimeout);
this.wheelTimeout = setTimeout(function () {
that._execEvent('zoomEnd');
}, 400);
if ( 'deltaX' in e ) {
wheelDeltaY = -e.deltaY / Math.abs(e.deltaY);
} else if ('wheelDeltaX' in e) {
wheelDeltaY = e.wheelDeltaY / Math.abs(e.wheelDeltaY);
} else if('wheelDelta' in e) {
wheelDeltaY = e.wheelDelta / Math.abs(e.wheelDelta);
} else if ('detail' in e) {
wheelDeltaY = -e.detail / Math.abs(e.wheelDelta);
} else {
return;
}
deltaScale = this.scale + wheelDeltaY / 5;
this.zoom(deltaScale, e.pageX, e.pageY, 0);
},