-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathloupe.js
359 lines (301 loc) · 8.52 KB
/
loupe.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
/**
Loupe 2.0.1
https://github.com/redoPop/loupe
*/
(function (root) {
'use strict';
/**
A new loupe instance.
@constructor
@param {HTMLElement}
@example
var loupe = new Loupe(document.getElementById('image'));
*/
function Loupe(el) {
this.init(el);
}
// Adapted from Modernizr's pointerevents feature detect
var canPoint = document.createElement('x');
canPoint.style.cssText = 'pointer-events:none';
canPoint = canPoint.style.pointerEvents === 'none';
// Keyword variables for tighter minification
var _offset = 'offset';
var _mousemove = 'mousemove';
var _mouseout = 'mouseout';
var _pointerdown = 'pointerdown';
var _touchstart = 'touchstart';
var _wheel = 'wheel';
// Helper method to normalize event.offset(X|Y)
function eventOffset(event, axis) {
return event[_offset + axis] === undefined ?
event['layer' + axis] - event.target[_offset + (axis === 'X' ? 'Left' : 'Top')] :
event[_offset + axis];
}
// Helper method to safely parse an int
function intify(number) {
return parseInt(number, 10);
}
// Helper method to add & remove CSS classes
function setClass(el, add, remove) {
var string = el.className;
remove = remove || '';
el.className =
(
string.replace(remove, '') + ' ' +
add
)
.replace(/^\s*([\S\s]*)\b\s*$/, '$1');
}
Loupe.prototype = {
/**
The properties of the `_` object are likely to change between
versions of Loupe; do _not_ rely on it!
@namespace
@private
*/
_: {},
/**
CSS class names used by the loupe object.
@namespace
@static
*/
classes: {
/**
The main magnifying lens.
@member {string}
@default
*/
lens: 'loupe',
/**
Applied when the lens is in use and visible.
@member {string}
@default
*/
on: 'loupe--active',
/**
Applied when the lens is deactivated after use.
@member {string}
@default
*/
off: 'loupe--inactive'
},
/**
True while the loupe is active.
@type {boolean}
@default
*/
on: false,
/**
Zoom level (compared w/source img)
@type {number}
@default
*/
zoom: 2,
/**
Minimum and maximum zoom levels!
@type {Array}
@default
*/
range: [1.2, 7],
/**
Called by the contructor to initialize the loupe instance.
@param {HTMLElement}
*/
init: function (el) {
var _this = this;
// If the browser doesn't support pointer-events,
// halt loupe's initialization!
if (!canPoint) {
return;
}
// Create local versions of unshared objects
_this._ = {};
_this.range = _this.range.slice(0);
_this.el = el;
_this.render();
_this.listen();
},
/**
Show the loupe.
*/
show: function () {
if (!this.on) {
setClass(this.lens, this.classes.on, this.classes.off);
this.on = true;
}
},
/**
Hide the loupe.
*/
hide: function () {
setClass(this.lens, this.classes.off, this.classes.on);
this.on = false;
},
/**
Renders the loupe element.
*/
render: function () {
var _this = this;
_this.lens = document.createElement('div');
_this.lens.className += ' ' + _this.classes.lens;
document.body.appendChild(_this.lens);
},
/**
Returns URL to use as a magnified image.
Finds the closest parent link from `el` and returns the value
of its `href` attribute, else returns the `src` attribute of
`el` itself.
@param {HTMLImageElement} el Source or root image element
*/
image: function (imageEl) {
var _this = this;
if (_this._.l === imageEl) {
return _this._.s;
}
_this._.l = imageEl;
var newS;
var el = imageEl;
var els = [];
while (el && !newS) {
els.unshift(el);
el = el.parentNode;
if (el && el.nodeName === 'A') {
newS = el.href;
}
}
if (!newS) {
newS = imageEl.src;
}
_this._.s = newS;
return newS;
},
/**
Refresh the loupe position and focus.
Also used as a mousemove handler.
@param {MouseEvent} [event] - mousemove event.
*/
refresh: function () {
var _this = this;
var style = _this.lens.style;
var event = arguments[0] || _this._.e;
if (event && event.target) {
style.backgroundImage = 'url("' + _this.image(event.target) + '")';
}
// Ignore the very first this.move call: it could be a
// simulated pointer event!
if (_this._.e) {
var elComputedStyle = getComputedStyle(event.target);
var loComputedStyle = getComputedStyle(_this.lens);
_this.show();
// Change the loupe position
style.transform = style.webkitTransform = 'translate(' +
event.pageX + 'px,' + event.pageY + 'px)';
// Adjust zoom level and background size
_this.lens.style.backgroundSize =
intify(elComputedStyle.width) * _this.zoom + 'px ' +
intify(elComputedStyle.height) * _this.zoom + 'px';
// Change the focal point of the loupe image
_this.lens.style.backgroundPosition =
(0 - eventOffset(event, 'X') * _this.zoom +
(intify(loComputedStyle.width) / 2)) + 'px ' +
(0 - eventOffset(event, 'Y') * _this.zoom +
(intify(loComputedStyle.height) / 2 | 0)) + 'px';
}
_this._.e = event;
},
/**
バルス!
Removes all created elements and event listeners. After
calling this method, the loupe object can be destroyed
or garbage collected, or even re-initialized by calling
the .init method with a different element.
*/
destroy: function () {
var _this = this;
var elRemoveListener = _this.el.removeEventListener.bind(_this.el);
// Remove this.lens
if (_this.lens) {
_this.lens.parentNode.removeChild(_this.lens);
_this.lens = null;
}
// If this.listen was called when the loupe was set up,
// remove its event listeners during destruction.
if (_this._[_mousemove]) {
elRemoveListener(_touchstart, _this._[_touchstart]);
elRemoveListener(_pointerdown, _this._[_pointerdown]);
elRemoveListener(_mousemove, _this._[_mousemove]);
elRemoveListener(_mouseout, _this._[_mouseout]);
elRemoveListener(_wheel, _this._[_wheel]);
}
// Clear _ in case the user wants to re-initialize.
_this._ = {};
},
/**
Sets up event listeners.
*/
listen: function () {
var isTouched;
var _this = this;
var elAddListener = _this.el.addEventListener.bind(_this.el);
_this._[_touchstart] = function () {
isTouched = true;
};
// IE 11 uses pointer events instead of touch* events
_this._[_pointerdown] = function (event) {
if (event.pointerType === 'touch') {
isTouched = true;
}
};
_this._[_mousemove] = function (event) {
if (!isTouched) {
_this.refresh.call(_this, event);
}
isTouched = false;
};
_this._[_mouseout] = _this.hide.bind(_this);
// Add event listners describing normal x, y movement
elAddListener(_touchstart, _this._[_touchstart]);
elAddListener(_pointerdown, _this._[_pointerdown]);
elAddListener(_mousemove, _this._[_mousemove]);
elAddListener(_mouseout, _this._[_mouseout]);
// Add event listener describing z (mousewheel) movement
_this._[_wheel] = _this.wheel.bind(_this);
elAddListener(_wheel, _this._[_wheel]);
},
/**
Wheel event handler to trigger zooming behavior.
@param {WheelEvent} event - wheel event.
*/
wheel: function (event) {
var _this = this;
var zoom;
if (_this.on) {
zoom = _this.zoom + event.deltaY * 0.1;
zoom = (
zoom < _this.range[0] ?
_this.range[0] :
(
zoom > _this.range[1] ?
_this.range[1] :
zoom
)
);
_this.zoom = zoom;
_this.refresh(event);
event.preventDefault();
}
}
};
// UMD (based on returnExports.js template)
// https://github.com/umdjs/umd
/* globals define, module */
if (typeof define === 'function' && define.amd) {
define([], function () {
return Loupe;
});
} else if (typeof module === 'object' && module.exports) {
module.exports = Loupe;
} else {
root.Loupe = Loupe;
}
}(this));