forked from docsifyjs/docsify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoom-image.js
304 lines (239 loc) · 8.4 KB
/
zoom-image.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
this.D = this.D || {};
(function () {
'use strict';
var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
function createCommonjsModule(fn, module) {
return module = { exports: {} }, fn(module, module.exports), module.exports;
}
var zoomImage = createCommonjsModule(function (module, exports) {
(function (global, factory) {
module.exports = factory();
}(commonjsGlobal, (function () { 'use strict';
var windowWidth = function () { return document.documentElement.clientWidth; };
var windowHeight = function () { return document.documentElement.clientHeight; };
var elemOffset = function (elem) {
var rect = elem.getBoundingClientRect();
var docElem = document.documentElement;
var win = window;
return {
top: rect.top + win.pageYOffset - docElem.clientTop,
left: rect.left + win.pageXOffset - docElem.clientLeft
}
};
var once = function (elem, type, handler) {
var fn = function (e) {
e.target.removeEventListener(type, fn);
handler();
};
elem.addEventListener(type, fn);
};
var Size = function Size(w, h) {
this.w = w;
this.h = h;
};
var ZoomImage = function ZoomImage(img, offset) {
this.img = img;
this.preservedTransform = img.style.transform;
this.wrap = null;
this.overlay = null;
this.offset = offset;
};
ZoomImage.prototype.forceRepaint = function forceRepaint () {
var _ = this.img.offsetWidth;
return
};
ZoomImage.prototype.zoom = function zoom () {
var size = new Size(this.img.naturalWidth, this.img.naturalHeight);
this.wrap = document.createElement('div');
this.wrap.classList.add('zoom-img-wrap');
this.img.parentNode.insertBefore(this.wrap, this.img);
this.wrap.appendChild(this.img);
this.img.classList.add('zoom-img');
this.img.setAttribute('data-action', 'zoom-out');
this.overlay = document.createElement('div');
this.overlay.classList.add('zoom-overlay');
document.body.appendChild(this.overlay);
this.forceRepaint();
var scale = this.calculateScale(size);
this.forceRepaint();
this.animate(scale);
document.body.classList.add('zoom-overlay-open');
};
ZoomImage.prototype.calculateScale = function calculateScale (size) {
var maxScaleFactor = size.w / this.img.width;
var viewportWidth = (windowWidth() - this.offset);
var viewportHeight = (windowHeight() - this.offset);
var imageAspectRatio = size.w / size.h;
var viewportAspectRatio = viewportWidth / viewportHeight;
if (size.w < viewportWidth && size.h < viewportHeight) {
return maxScaleFactor
} else if (imageAspectRatio < viewportAspectRatio) {
return (viewportHeight / size.h) * maxScaleFactor
}
return (viewportWidth / size.w) * maxScaleFactor
};
ZoomImage.prototype.animate = function animate (scale) {
var imageOffset = elemOffset(this.img);
var scrollTop = window.pageYOffset;
var viewportX = (windowWidth() / 2);
var viewportY = scrollTop + (windowHeight() / 2);
var imageCenterX = imageOffset.left + (this.img.width / 2);
var imageCenterY = imageOffset.top + (this.img.height / 2);
var tx = viewportX - imageCenterX;
var ty = viewportY - imageCenterY;
var tz = 0;
var imgTr = "scale(" + scale + ")";
var wrapTr = "translate3d(" + tx + "px, " + ty + "px, " + tz + "px)";
this.img.style.transform = imgTr;
this.wrap.style.transform = wrapTr;
};
ZoomImage.prototype.dispose = function dispose () {
if (this.wrap === null || this.wrap.parentNode === null) {
return
}
this.img.classList.remove('zoom-img');
this.img.setAttribute('data-action', 'zoom');
this.wrap.parentNode.insertBefore(this.img, this.wrap);
this.wrap.parentNode.removeChild(this.wrap);
document.body.removeChild(this.overlay);
document.body.classList.remove('zoom-overlay-transitioning');
};
ZoomImage.prototype.close = function close () {
var this$1 = this;
document.body.classList.add('zoom-overlay-transitioning');
this.img.style.transform = this.preservedTransform;
if (this.img.style.length === 0) {
this.img.removeAttribute('style');
}
this.wrap.style.transform = 'none';
once(this.img, 'transitionend', function () {
this$1.dispose();
// remove class should happen after dispose. Otherwise,
// a new click event could fire and create a duplicate ZoomImage for
// the same <img> element.
document.body.classList.remove('zoom-overlay-open');
});
};
/**
* Pure JavaScript implementation of zoom.js.
*
* Original preamble:
* zoom.js - It's the best way to zoom an image
* @version v0.0.2
* @link https://github.com/fat/zoom.js
* @license MIT
*
* Needs a related CSS file to work. See the README at
* https://github.com/nishanths/zoom.js for more info.
*
* This is a fork of the original zoom.js implementation by @fat.
* Copyrights for the original project are held by @fat. All other copyright
* for changes in the fork are held by Nishanth Shanmugham.
*
* Copyright (c) 2013 @fat
* The MIT License. Copyright © 2016 Nishanth Shanmugham.
*/
var current = null;
var offset = 80;
var initialScrollPos = -1;
var initialTouchPos = -1;
function handleScroll() {
if (initialScrollPos === -1) {
initialScrollPos = window.pageYOffset;
}
var deltaY = Math.abs(initialScrollPos - window.pageYOffset);
if (deltaY >= 40) {
closeCurrent();
}
}
function handleKeyup(e) {
if (e.keyCode === 27) {
closeCurrent();
}
}
function handleTouchStart(e) {
var t = e.touches[0];
if (t === null) {
return
}
initialTouchPos = t.pageY;
e.target.addEventListener('touchmove', handleTouchMove);
}
function handleTouchMove(e) {
var t = e.touches[0];
if (t === null) {
return
}
if (Math.abs(t.pageY - initialTouchPos) > 10) {
closeCurrent();
e.target.removeEventListener('touchmove', handleTouchMove);
}
}
function handleClick() {
closeCurrent();
}
function addCloseListeners() {
document.addEventListener('scroll', handleScroll);
document.addEventListener('keyup', handleKeyup);
document.addEventListener('touchstart', handleTouchStart);
document.addEventListener('click', handleClick, true);
}
function removeCloseListeners() {
document.removeEventListener('scroll', handleScroll);
document.removeEventListener('keyup', handleKeyup);
document.removeEventListener('touchstart', handleTouchStart);
document.removeEventListener('click', handleClick, true);
}
function closeCurrent(force) {
if (current === null) {
return
}
if (force) {
current.dispose();
} else {
current.close();
}
removeCloseListeners();
current = null;
}
function prepareZoom(e) {
if (document.body.classList.contains('zoom-overlay-open')) {
return
}
if (e.metaKey || e.ctrlKey) {
window.open((e.target.getAttribute('data-original') || e.target.src), '_blank');
return
}
if (e.target.width >= windowWidth() - offset) {
return
}
closeCurrent(true);
current = new ZoomImage(e.target, offset);
current.zoom();
addCloseListeners();
}
var zoom = function (el) {
el.setAttribute('data-action', 'zoom');
el.addEventListener('click', prepareZoom);
return function () { return el.removeEventListener('click', prepareZoom); }
};
return zoom;
})));
});
var style = "img[data-action=\"zoom\"] {\n cursor: pointer;\n cursor: -webkit-zoom-in;\n cursor: zoom-in;\n}\n.zoom-img,\n.zoom-img-wrap {\n position: relative;\n z-index: 666;\n transition: -webkit-transform 300ms cubic-bezier(.2,0,.2,1);\n transition: transform 300ms cubic-bezier(.2,0,.2,1);\n transition: transform 300ms cubic-bezier(.2,0,.2,1), -webkit-transform 300ms cubic-bezier(.2,0,.2,1);\n}\n.zoom-overlay {\n z-index: 420;\n background: #fff;\n position: fixed;\n top: 0;\n left: 0;\n right: 0;\n bottom: 0;\n pointer-events: none;\n opacity: 0;\n transition: opacity 300ms;\n}\n.zoom-overlay-open .zoom-overlay {\n opacity: 1;\n}\n.zoom-overlay-open {\n cursor: pointer;\n cursor: -webkit-zoom-out;\n cursor: zoom-out;\n}\n.zoom-overlay-transitioning {\n cursor: default;\n}\n.zoom-overlay-open.zoom-overlay-transitioning .zoom-overlay{\n opacity: 0;\n}\n";
function install (hook) {
var dom = Docsify.dom;
var destroys;
// add style
dom.appendTo(dom.head, dom.create('style', style));
hook.doneEach(function (_) {
var images = dom.findAll('img:not(.emoji)');
if (Array.isArray(destroys) && destroys.length) {
destroys.forEach(function (o) { return o(); });
destroys = [];
}
destroys = images.map(zoomImage);
});
}
$docsify.plugins = [].concat(install, $docsify.plugins);
}());