forked from jackmoore/wheelzoom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wheelzoom.js
307 lines (255 loc) · 7.19 KB
/
wheelzoom.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
/*!
Wheelzoom 1.1.1
license: MIT
https://luperi.github.io/wheelzoom/
*/
// create CustomEvent to override window.Event in unsupported browsers
(function () {
function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}
CustomEvent.prototype = window.Event.prototype;
window.CustomEvent = CustomEvent;
})();
// create cross browser triggerEvent function
window.triggerEvent = function(target, eventName, params) {
if (params) {
var e = new CustomEvent(eventName, {
'detail': params
});
} else {
try {
var e = new Event(eventName);
} catch (err) {
var e = new CustomEvent(eventName);
}
}
return target.dispatchEvent(e, params);
};
window.wheelzoom = (function(){
var defaults = {
zoom: 0.10,
maxZoom: -1
};
var canvas = document.createElement('canvas');
var main = function(img, options){
if (!img || !img.nodeName || img.nodeName !== 'IMG') { return; }
var settings = {};
var width;
var height;
var bgWidth;
var bgHeight;
var bgPosX;
var bgPosY;
var previousEvent;
var cachedDataUrl;
var initBgPosX;
var initBgPosY;
function setSrcToBackground(img) {
img.style.backgroundImage = 'url("'+img.src+'")';
img.style.backgroundRepeat = 'no-repeat';
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
cachedDataUrl = canvas.toDataURL();
img.src = cachedDataUrl;
}
function updateBgStyle() {
if (bgPosX > 0) {
bgPosX = 0;
} else if (bgPosX < width - bgWidth) {
bgPosX = width - bgWidth;
}
if (bgPosY > 0) {
bgPosY = 0;
} else if (bgPosY < height - bgHeight) {
bgPosY = height - bgHeight;
}
img.style.backgroundSize = bgWidth+'px '+bgHeight+'px';
img.style.backgroundPosition = bgPosX+'px '+bgPosY+'px';
}
function reset() {
bgWidth = width;
bgHeight = height;
bgPosX = bgPosY = 0;
updateBgStyle();
}
img.doZoomIn = function(propagate) {
if (typeof propagate === 'undefined') {
propagate = false;
}
doZoom(-100, propagate);
}
img.doZoomOut = function(propagate) {
if (typeof propagate === 'undefined') {
propagate = false;
}
doZoom(100, propagate);
}
function doZoom (deltaY, propagate) {
if (typeof propagate === 'undefined') {
propagate = false;
}
// zoom always at the center of the image
var offsetX = img.width/2;
var offsetY = img.height/2;
// Record the offset between the bg edge and the center of the image:
var bgCenterX = offsetX - bgPosX;
var bgCenterY = offsetY - bgPosY;
// Use the previous offset to get the percent offset between the bg edge and the center of the image:
var bgRatioX = bgCenterX/bgWidth;
var bgRatioY = bgCenterY/bgHeight;
// Update the bg size:
if (deltaY < 0) {
if (settings.maxZoom == -1 || (bgWidth + bgWidth*settings.zoom) / width <= settings.maxZoom) {
bgWidth += bgWidth*settings.zoom;
bgHeight += bgHeight*settings.zoom;
}
} else {
bgWidth -= bgWidth*settings.zoom;
bgHeight -= bgHeight*settings.zoom;
}
// Take the percent offset and apply it to the new size:
bgPosX = offsetX - (bgWidth * bgRatioX);
bgPosY = offsetY - (bgHeight * bgRatioY);
if (propagate) {
if (deltaY < 0) {
// setTimeout to handle lot of events fired
setTimeout(function() {
triggerEvent(img, 'wheelzoom.in', {
zoom: bgWidth/width,
bgPosX: bgPosX,
bgPosY: bgPosY
});
}, 10);
} else {
// setTimeout to handle lot of events fired
setTimeout(function() {
triggerEvent(img, 'wheelzoom.out', {
zoom: bgWidth/width,
bgPosX: bgPosX,
bgPosY: bgPosY
});
}, 10);
}
}
// Prevent zooming out beyond the starting size
if (bgWidth <= width || bgHeight <= height) {
triggerEvent(img, 'wheelzoom.reset');
} else {
updateBgStyle();
}
}
function onwheel(e) {
var deltaY = 0;
e.preventDefault();
if (e.deltaY) { // FireFox 17+ (IE9+, Chrome 31+?)
deltaY = e.deltaY;
} else if (e.wheelDelta) {
deltaY = -e.wheelDelta;
}
if (deltaY < 0) {
img.doZoomIn(true);
} else {
img.doZoomOut(true);
}
}
img.doDrag = function (x, y) {
bgPosX = x;
bgPosY = y;
updateBgStyle();
}
function drag(e) {
e.preventDefault();
var xShift = e.pageX - previousEvent.pageX;
var yShift = e.pageY - previousEvent.pageY;
var x = bgPosX + xShift;
var y = bgPosY + yShift;
img.doDrag(x, y);
triggerEvent(img, 'wheelzoom.drag', {
bgPosX: bgPosX,
bgPosY: bgPosY,
xShift: xShift,
yShift: yShift
});
previousEvent = e;
updateBgStyle();
}
function removeDrag() {
triggerEvent(img, 'wheelzoom.dragend', {
x: bgPosX - initBgPosX,
y: bgPosY - initBgPosY
});
document.removeEventListener('mouseup', removeDrag);
document.removeEventListener('mousemove', drag);
}
// Make the background draggable
function draggable(e) {
triggerEvent(img, 'wheelzoom.dragstart');
e.preventDefault();
previousEvent = e;
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', removeDrag);
}
function load() {
if (img.src === cachedDataUrl) return;
var computedStyle = window.getComputedStyle(img, null);
width = parseInt(computedStyle.width, 10);
height = parseInt(computedStyle.height, 10);
bgWidth = width;
bgHeight = height;
bgPosX = bgPosY = initBgPosX = initBgPosY = 0;
setSrcToBackground(img);
img.style.backgroundSize = width+'px '+height+'px';
img.style.backgroundPosition = '0 0';
img.addEventListener('wheelzoom.reset', reset);
img.addEventListener('wheelzoom.destroy', destroy);
img.addEventListener('wheel', onwheel);
img.addEventListener('mousedown', draggable);
}
var destroy = function (originalProperties) {
img.removeEventListener('wheelzoom.destroy', destroy);
img.removeEventListener('wheelzoom.reset', reset);
img.removeEventListener('mouseup', removeDrag);
img.removeEventListener('mousemove', drag);
img.removeEventListener('mousedown', draggable);
img.removeEventListener('wheel', onwheel);
img.style.backgroundImage = originalProperties.backgroundImage;
img.style.backgroundRepeat = originalProperties.backgroundRepeat;
img.src = originalProperties.src;
}.bind(null, {
backgroundImage: img.style.backgroundImage,
backgroundRepeat: img.style.backgroundRepeat,
src: img.src
});
options = options || {};
Object.keys(defaults).forEach(function(key){
settings[key] = typeof options[key] !== 'undefined' ? options[key] : defaults[key];
});
var t = setInterval(function(){
if (img.complete) {
load();
}
clearInterval(t);
}, 100);
};
// Do nothing in IE8
if (typeof window.getComputedStyle !== 'function') {
return function(elements) {
return elements;
};
} else {
return function(elements, options) {
if (elements && elements.length) {
for (var i=0;i<elements.length;i++) {
main(elements[i], options);
}
} else if (elements && elements.nodeName) {
main(elements, options);
}
return elements;
};
}
}());