@ntlab/ntjs-assets 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3253 @@
1
+ /*!
2
+ * Cropper.js v1.5.13
3
+ * https://fengyuanchen.github.io/cropperjs
4
+ *
5
+ * Copyright 2015-present Chen Fengyuan
6
+ * Released under the MIT license
7
+ *
8
+ * Date: 2022-11-20T05:30:46.114Z
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ function ownKeys(object, enumerableOnly) {
14
+ var keys = Object.keys(object);
15
+ if (Object.getOwnPropertySymbols) {
16
+ var symbols = Object.getOwnPropertySymbols(object);
17
+ enumerableOnly && (symbols = symbols.filter(function (sym) {
18
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
19
+ })), keys.push.apply(keys, symbols);
20
+ }
21
+ return keys;
22
+ }
23
+ function _objectSpread2(target) {
24
+ for (var i = 1; i < arguments.length; i++) {
25
+ var source = null != arguments[i] ? arguments[i] : {};
26
+ i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
27
+ _defineProperty(target, key, source[key]);
28
+ }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
29
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
30
+ });
31
+ }
32
+ return target;
33
+ }
34
+ function _typeof(obj) {
35
+ "@babel/helpers - typeof";
36
+
37
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) {
38
+ return typeof obj;
39
+ } : function (obj) {
40
+ return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
41
+ }, _typeof(obj);
42
+ }
43
+ function _classCallCheck(instance, Constructor) {
44
+ if (!(instance instanceof Constructor)) {
45
+ throw new TypeError("Cannot call a class as a function");
46
+ }
47
+ }
48
+ function _defineProperties(target, props) {
49
+ for (var i = 0; i < props.length; i++) {
50
+ var descriptor = props[i];
51
+ descriptor.enumerable = descriptor.enumerable || false;
52
+ descriptor.configurable = true;
53
+ if ("value" in descriptor) descriptor.writable = true;
54
+ Object.defineProperty(target, descriptor.key, descriptor);
55
+ }
56
+ }
57
+ function _createClass(Constructor, protoProps, staticProps) {
58
+ if (protoProps) _defineProperties(Constructor.prototype, protoProps);
59
+ if (staticProps) _defineProperties(Constructor, staticProps);
60
+ Object.defineProperty(Constructor, "prototype", {
61
+ writable: false
62
+ });
63
+ return Constructor;
64
+ }
65
+ function _defineProperty(obj, key, value) {
66
+ if (key in obj) {
67
+ Object.defineProperty(obj, key, {
68
+ value: value,
69
+ enumerable: true,
70
+ configurable: true,
71
+ writable: true
72
+ });
73
+ } else {
74
+ obj[key] = value;
75
+ }
76
+ return obj;
77
+ }
78
+ function _toConsumableArray(arr) {
79
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
80
+ }
81
+ function _arrayWithoutHoles(arr) {
82
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
83
+ }
84
+ function _iterableToArray(iter) {
85
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
86
+ }
87
+ function _unsupportedIterableToArray(o, minLen) {
88
+ if (!o) return;
89
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
90
+ var n = Object.prototype.toString.call(o).slice(8, -1);
91
+ if (n === "Object" && o.constructor) n = o.constructor.name;
92
+ if (n === "Map" || n === "Set") return Array.from(o);
93
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
94
+ }
95
+ function _arrayLikeToArray(arr, len) {
96
+ if (len == null || len > arr.length) len = arr.length;
97
+ for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
98
+ return arr2;
99
+ }
100
+ function _nonIterableSpread() {
101
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
102
+ }
103
+
104
+ var IS_BROWSER = typeof window !== 'undefined' && typeof window.document !== 'undefined';
105
+ var WINDOW = IS_BROWSER ? window : {};
106
+ var IS_TOUCH_DEVICE = IS_BROWSER && WINDOW.document.documentElement ? 'ontouchstart' in WINDOW.document.documentElement : false;
107
+ var HAS_POINTER_EVENT = IS_BROWSER ? 'PointerEvent' in WINDOW : false;
108
+ var NAMESPACE = 'cropper';
109
+
110
+ // Actions
111
+ var ACTION_ALL = 'all';
112
+ var ACTION_CROP = 'crop';
113
+ var ACTION_MOVE = 'move';
114
+ var ACTION_ZOOM = 'zoom';
115
+ var ACTION_EAST = 'e';
116
+ var ACTION_WEST = 'w';
117
+ var ACTION_SOUTH = 's';
118
+ var ACTION_NORTH = 'n';
119
+ var ACTION_NORTH_EAST = 'ne';
120
+ var ACTION_NORTH_WEST = 'nw';
121
+ var ACTION_SOUTH_EAST = 'se';
122
+ var ACTION_SOUTH_WEST = 'sw';
123
+
124
+ // Classes
125
+ var CLASS_CROP = "".concat(NAMESPACE, "-crop");
126
+ var CLASS_DISABLED = "".concat(NAMESPACE, "-disabled");
127
+ var CLASS_HIDDEN = "".concat(NAMESPACE, "-hidden");
128
+ var CLASS_HIDE = "".concat(NAMESPACE, "-hide");
129
+ var CLASS_INVISIBLE = "".concat(NAMESPACE, "-invisible");
130
+ var CLASS_MODAL = "".concat(NAMESPACE, "-modal");
131
+ var CLASS_MOVE = "".concat(NAMESPACE, "-move");
132
+
133
+ // Data keys
134
+ var DATA_ACTION = "".concat(NAMESPACE, "Action");
135
+ var DATA_PREVIEW = "".concat(NAMESPACE, "Preview");
136
+
137
+ // Drag modes
138
+ var DRAG_MODE_CROP = 'crop';
139
+ var DRAG_MODE_MOVE = 'move';
140
+ var DRAG_MODE_NONE = 'none';
141
+
142
+ // Events
143
+ var EVENT_CROP = 'crop';
144
+ var EVENT_CROP_END = 'cropend';
145
+ var EVENT_CROP_MOVE = 'cropmove';
146
+ var EVENT_CROP_START = 'cropstart';
147
+ var EVENT_DBLCLICK = 'dblclick';
148
+ var EVENT_TOUCH_START = IS_TOUCH_DEVICE ? 'touchstart' : 'mousedown';
149
+ var EVENT_TOUCH_MOVE = IS_TOUCH_DEVICE ? 'touchmove' : 'mousemove';
150
+ var EVENT_TOUCH_END = IS_TOUCH_DEVICE ? 'touchend touchcancel' : 'mouseup';
151
+ var EVENT_POINTER_DOWN = HAS_POINTER_EVENT ? 'pointerdown' : EVENT_TOUCH_START;
152
+ var EVENT_POINTER_MOVE = HAS_POINTER_EVENT ? 'pointermove' : EVENT_TOUCH_MOVE;
153
+ var EVENT_POINTER_UP = HAS_POINTER_EVENT ? 'pointerup pointercancel' : EVENT_TOUCH_END;
154
+ var EVENT_READY = 'ready';
155
+ var EVENT_RESIZE = 'resize';
156
+ var EVENT_WHEEL = 'wheel';
157
+ var EVENT_ZOOM = 'zoom';
158
+
159
+ // Mime types
160
+ var MIME_TYPE_JPEG = 'image/jpeg';
161
+
162
+ // RegExps
163
+ var REGEXP_ACTIONS = /^e|w|s|n|se|sw|ne|nw|all|crop|move|zoom$/;
164
+ var REGEXP_DATA_URL = /^data:/;
165
+ var REGEXP_DATA_URL_JPEG = /^data:image\/jpeg;base64,/;
166
+ var REGEXP_TAG_NAME = /^img|canvas$/i;
167
+
168
+ // Misc
169
+ // Inspired by the default width and height of a canvas element.
170
+ var MIN_CONTAINER_WIDTH = 200;
171
+ var MIN_CONTAINER_HEIGHT = 100;
172
+
173
+ var DEFAULTS = {
174
+ // Define the view mode of the cropper
175
+ viewMode: 0,
176
+ // 0, 1, 2, 3
177
+
178
+ // Define the dragging mode of the cropper
179
+ dragMode: DRAG_MODE_CROP,
180
+ // 'crop', 'move' or 'none'
181
+
182
+ // Define the initial aspect ratio of the crop box
183
+ initialAspectRatio: NaN,
184
+ // Define the aspect ratio of the crop box
185
+ aspectRatio: NaN,
186
+ // An object with the previous cropping result data
187
+ data: null,
188
+ // A selector for adding extra containers to preview
189
+ preview: '',
190
+ // Re-render the cropper when resize the window
191
+ responsive: true,
192
+ // Restore the cropped area after resize the window
193
+ restore: true,
194
+ // Check if the current image is a cross-origin image
195
+ checkCrossOrigin: true,
196
+ // Check the current image's Exif Orientation information
197
+ checkOrientation: true,
198
+ // Show the black modal
199
+ modal: true,
200
+ // Show the dashed lines for guiding
201
+ guides: true,
202
+ // Show the center indicator for guiding
203
+ center: true,
204
+ // Show the white modal to highlight the crop box
205
+ highlight: true,
206
+ // Show the grid background
207
+ background: true,
208
+ // Enable to crop the image automatically when initialize
209
+ autoCrop: true,
210
+ // Define the percentage of automatic cropping area when initializes
211
+ autoCropArea: 0.8,
212
+ // Enable to move the image
213
+ movable: true,
214
+ // Enable to rotate the image
215
+ rotatable: true,
216
+ // Enable to scale the image
217
+ scalable: true,
218
+ // Enable to zoom the image
219
+ zoomable: true,
220
+ // Enable to zoom the image by dragging touch
221
+ zoomOnTouch: true,
222
+ // Enable to zoom the image by wheeling mouse
223
+ zoomOnWheel: true,
224
+ // Define zoom ratio when zoom the image by wheeling mouse
225
+ wheelZoomRatio: 0.1,
226
+ // Enable to move the crop box
227
+ cropBoxMovable: true,
228
+ // Enable to resize the crop box
229
+ cropBoxResizable: true,
230
+ // Toggle drag mode between "crop" and "move" when click twice on the cropper
231
+ toggleDragModeOnDblclick: true,
232
+ // Size limitation
233
+ minCanvasWidth: 0,
234
+ minCanvasHeight: 0,
235
+ minCropBoxWidth: 0,
236
+ minCropBoxHeight: 0,
237
+ minContainerWidth: MIN_CONTAINER_WIDTH,
238
+ minContainerHeight: MIN_CONTAINER_HEIGHT,
239
+ // Shortcuts of events
240
+ ready: null,
241
+ cropstart: null,
242
+ cropmove: null,
243
+ cropend: null,
244
+ crop: null,
245
+ zoom: null
246
+ };
247
+
248
+ var TEMPLATE = '<div class="cropper-container" touch-action="none">' + '<div class="cropper-wrap-box">' + '<div class="cropper-canvas"></div>' + '</div>' + '<div class="cropper-drag-box"></div>' + '<div class="cropper-crop-box">' + '<span class="cropper-view-box"></span>' + '<span class="cropper-dashed dashed-h"></span>' + '<span class="cropper-dashed dashed-v"></span>' + '<span class="cropper-center"></span>' + '<span class="cropper-face"></span>' + '<span class="cropper-line line-e" data-cropper-action="e"></span>' + '<span class="cropper-line line-n" data-cropper-action="n"></span>' + '<span class="cropper-line line-w" data-cropper-action="w"></span>' + '<span class="cropper-line line-s" data-cropper-action="s"></span>' + '<span class="cropper-point point-e" data-cropper-action="e"></span>' + '<span class="cropper-point point-n" data-cropper-action="n"></span>' + '<span class="cropper-point point-w" data-cropper-action="w"></span>' + '<span class="cropper-point point-s" data-cropper-action="s"></span>' + '<span class="cropper-point point-ne" data-cropper-action="ne"></span>' + '<span class="cropper-point point-nw" data-cropper-action="nw"></span>' + '<span class="cropper-point point-sw" data-cropper-action="sw"></span>' + '<span class="cropper-point point-se" data-cropper-action="se"></span>' + '</div>' + '</div>';
249
+
250
+ /**
251
+ * Check if the given value is not a number.
252
+ */
253
+ var isNaN = Number.isNaN || WINDOW.isNaN;
254
+
255
+ /**
256
+ * Check if the given value is a number.
257
+ * @param {*} value - The value to check.
258
+ * @returns {boolean} Returns `true` if the given value is a number, else `false`.
259
+ */
260
+ function isNumber(value) {
261
+ return typeof value === 'number' && !isNaN(value);
262
+ }
263
+
264
+ /**
265
+ * Check if the given value is a positive number.
266
+ * @param {*} value - The value to check.
267
+ * @returns {boolean} Returns `true` if the given value is a positive number, else `false`.
268
+ */
269
+ var isPositiveNumber = function isPositiveNumber(value) {
270
+ return value > 0 && value < Infinity;
271
+ };
272
+
273
+ /**
274
+ * Check if the given value is undefined.
275
+ * @param {*} value - The value to check.
276
+ * @returns {boolean} Returns `true` if the given value is undefined, else `false`.
277
+ */
278
+ function isUndefined(value) {
279
+ return typeof value === 'undefined';
280
+ }
281
+
282
+ /**
283
+ * Check if the given value is an object.
284
+ * @param {*} value - The value to check.
285
+ * @returns {boolean} Returns `true` if the given value is an object, else `false`.
286
+ */
287
+ function isObject(value) {
288
+ return _typeof(value) === 'object' && value !== null;
289
+ }
290
+ var hasOwnProperty = Object.prototype.hasOwnProperty;
291
+
292
+ /**
293
+ * Check if the given value is a plain object.
294
+ * @param {*} value - The value to check.
295
+ * @returns {boolean} Returns `true` if the given value is a plain object, else `false`.
296
+ */
297
+ function isPlainObject(value) {
298
+ if (!isObject(value)) {
299
+ return false;
300
+ }
301
+ try {
302
+ var _constructor = value.constructor;
303
+ var prototype = _constructor.prototype;
304
+ return _constructor && prototype && hasOwnProperty.call(prototype, 'isPrototypeOf');
305
+ } catch (error) {
306
+ return false;
307
+ }
308
+ }
309
+
310
+ /**
311
+ * Check if the given value is a function.
312
+ * @param {*} value - The value to check.
313
+ * @returns {boolean} Returns `true` if the given value is a function, else `false`.
314
+ */
315
+ function isFunction(value) {
316
+ return typeof value === 'function';
317
+ }
318
+ var slice = Array.prototype.slice;
319
+
320
+ /**
321
+ * Convert array-like or iterable object to an array.
322
+ * @param {*} value - The value to convert.
323
+ * @returns {Array} Returns a new array.
324
+ */
325
+ function toArray(value) {
326
+ return Array.from ? Array.from(value) : slice.call(value);
327
+ }
328
+
329
+ /**
330
+ * Iterate the given data.
331
+ * @param {*} data - The data to iterate.
332
+ * @param {Function} callback - The process function for each element.
333
+ * @returns {*} The original data.
334
+ */
335
+ function forEach(data, callback) {
336
+ if (data && isFunction(callback)) {
337
+ if (Array.isArray(data) || isNumber(data.length) /* array-like */) {
338
+ toArray(data).forEach(function (value, key) {
339
+ callback.call(data, value, key, data);
340
+ });
341
+ } else if (isObject(data)) {
342
+ Object.keys(data).forEach(function (key) {
343
+ callback.call(data, data[key], key, data);
344
+ });
345
+ }
346
+ }
347
+ return data;
348
+ }
349
+
350
+ /**
351
+ * Extend the given object.
352
+ * @param {*} target - The target object to extend.
353
+ * @param {*} args - The rest objects for merging to the target object.
354
+ * @returns {Object} The extended object.
355
+ */
356
+ var assign = Object.assign || function assign(target) {
357
+ for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
358
+ args[_key - 1] = arguments[_key];
359
+ }
360
+ if (isObject(target) && args.length > 0) {
361
+ args.forEach(function (arg) {
362
+ if (isObject(arg)) {
363
+ Object.keys(arg).forEach(function (key) {
364
+ target[key] = arg[key];
365
+ });
366
+ }
367
+ });
368
+ }
369
+ return target;
370
+ };
371
+ var REGEXP_DECIMALS = /\.\d*(?:0|9){12}\d*$/;
372
+
373
+ /**
374
+ * Normalize decimal number.
375
+ * Check out {@link https://0.30000000000000004.com/}
376
+ * @param {number} value - The value to normalize.
377
+ * @param {number} [times=100000000000] - The times for normalizing.
378
+ * @returns {number} Returns the normalized number.
379
+ */
380
+ function normalizeDecimalNumber(value) {
381
+ var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100000000000;
382
+ return REGEXP_DECIMALS.test(value) ? Math.round(value * times) / times : value;
383
+ }
384
+ var REGEXP_SUFFIX = /^width|height|left|top|marginLeft|marginTop$/;
385
+
386
+ /**
387
+ * Apply styles to the given element.
388
+ * @param {Element} element - The target element.
389
+ * @param {Object} styles - The styles for applying.
390
+ */
391
+ function setStyle(element, styles) {
392
+ var style = element.style;
393
+ forEach(styles, function (value, property) {
394
+ if (REGEXP_SUFFIX.test(property) && isNumber(value)) {
395
+ value = "".concat(value, "px");
396
+ }
397
+ style[property] = value;
398
+ });
399
+ }
400
+
401
+ /**
402
+ * Check if the given element has a special class.
403
+ * @param {Element} element - The element to check.
404
+ * @param {string} value - The class to search.
405
+ * @returns {boolean} Returns `true` if the special class was found.
406
+ */
407
+ function hasClass(element, value) {
408
+ return element.classList ? element.classList.contains(value) : element.className.indexOf(value) > -1;
409
+ }
410
+
411
+ /**
412
+ * Add classes to the given element.
413
+ * @param {Element} element - The target element.
414
+ * @param {string} value - The classes to be added.
415
+ */
416
+ function addClass(element, value) {
417
+ if (!value) {
418
+ return;
419
+ }
420
+ if (isNumber(element.length)) {
421
+ forEach(element, function (elem) {
422
+ addClass(elem, value);
423
+ });
424
+ return;
425
+ }
426
+ if (element.classList) {
427
+ element.classList.add(value);
428
+ return;
429
+ }
430
+ var className = element.className.trim();
431
+ if (!className) {
432
+ element.className = value;
433
+ } else if (className.indexOf(value) < 0) {
434
+ element.className = "".concat(className, " ").concat(value);
435
+ }
436
+ }
437
+
438
+ /**
439
+ * Remove classes from the given element.
440
+ * @param {Element} element - The target element.
441
+ * @param {string} value - The classes to be removed.
442
+ */
443
+ function removeClass(element, value) {
444
+ if (!value) {
445
+ return;
446
+ }
447
+ if (isNumber(element.length)) {
448
+ forEach(element, function (elem) {
449
+ removeClass(elem, value);
450
+ });
451
+ return;
452
+ }
453
+ if (element.classList) {
454
+ element.classList.remove(value);
455
+ return;
456
+ }
457
+ if (element.className.indexOf(value) >= 0) {
458
+ element.className = element.className.replace(value, '');
459
+ }
460
+ }
461
+
462
+ /**
463
+ * Add or remove classes from the given element.
464
+ * @param {Element} element - The target element.
465
+ * @param {string} value - The classes to be toggled.
466
+ * @param {boolean} added - Add only.
467
+ */
468
+ function toggleClass(element, value, added) {
469
+ if (!value) {
470
+ return;
471
+ }
472
+ if (isNumber(element.length)) {
473
+ forEach(element, function (elem) {
474
+ toggleClass(elem, value, added);
475
+ });
476
+ return;
477
+ }
478
+
479
+ // IE10-11 doesn't support the second parameter of `classList.toggle`
480
+ if (added) {
481
+ addClass(element, value);
482
+ } else {
483
+ removeClass(element, value);
484
+ }
485
+ }
486
+ var REGEXP_CAMEL_CASE = /([a-z\d])([A-Z])/g;
487
+
488
+ /**
489
+ * Transform the given string from camelCase to kebab-case
490
+ * @param {string} value - The value to transform.
491
+ * @returns {string} The transformed value.
492
+ */
493
+ function toParamCase(value) {
494
+ return value.replace(REGEXP_CAMEL_CASE, '$1-$2').toLowerCase();
495
+ }
496
+
497
+ /**
498
+ * Get data from the given element.
499
+ * @param {Element} element - The target element.
500
+ * @param {string} name - The data key to get.
501
+ * @returns {string} The data value.
502
+ */
503
+ function getData(element, name) {
504
+ if (isObject(element[name])) {
505
+ return element[name];
506
+ }
507
+ if (element.dataset) {
508
+ return element.dataset[name];
509
+ }
510
+ return element.getAttribute("data-".concat(toParamCase(name)));
511
+ }
512
+
513
+ /**
514
+ * Set data to the given element.
515
+ * @param {Element} element - The target element.
516
+ * @param {string} name - The data key to set.
517
+ * @param {string} data - The data value.
518
+ */
519
+ function setData(element, name, data) {
520
+ if (isObject(data)) {
521
+ element[name] = data;
522
+ } else if (element.dataset) {
523
+ element.dataset[name] = data;
524
+ } else {
525
+ element.setAttribute("data-".concat(toParamCase(name)), data);
526
+ }
527
+ }
528
+
529
+ /**
530
+ * Remove data from the given element.
531
+ * @param {Element} element - The target element.
532
+ * @param {string} name - The data key to remove.
533
+ */
534
+ function removeData(element, name) {
535
+ if (isObject(element[name])) {
536
+ try {
537
+ delete element[name];
538
+ } catch (error) {
539
+ element[name] = undefined;
540
+ }
541
+ } else if (element.dataset) {
542
+ // #128 Safari not allows to delete dataset property
543
+ try {
544
+ delete element.dataset[name];
545
+ } catch (error) {
546
+ element.dataset[name] = undefined;
547
+ }
548
+ } else {
549
+ element.removeAttribute("data-".concat(toParamCase(name)));
550
+ }
551
+ }
552
+ var REGEXP_SPACES = /\s\s*/;
553
+ var onceSupported = function () {
554
+ var supported = false;
555
+ if (IS_BROWSER) {
556
+ var once = false;
557
+ var listener = function listener() {};
558
+ var options = Object.defineProperty({}, 'once', {
559
+ get: function get() {
560
+ supported = true;
561
+ return once;
562
+ },
563
+ /**
564
+ * This setter can fix a `TypeError` in strict mode
565
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Getter_only}
566
+ * @param {boolean} value - The value to set
567
+ */
568
+ set: function set(value) {
569
+ once = value;
570
+ }
571
+ });
572
+ WINDOW.addEventListener('test', listener, options);
573
+ WINDOW.removeEventListener('test', listener, options);
574
+ }
575
+ return supported;
576
+ }();
577
+
578
+ /**
579
+ * Remove event listener from the target element.
580
+ * @param {Element} element - The event target.
581
+ * @param {string} type - The event type(s).
582
+ * @param {Function} listener - The event listener.
583
+ * @param {Object} options - The event options.
584
+ */
585
+ function removeListener(element, type, listener) {
586
+ var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
587
+ var handler = listener;
588
+ type.trim().split(REGEXP_SPACES).forEach(function (event) {
589
+ if (!onceSupported) {
590
+ var listeners = element.listeners;
591
+ if (listeners && listeners[event] && listeners[event][listener]) {
592
+ handler = listeners[event][listener];
593
+ delete listeners[event][listener];
594
+ if (Object.keys(listeners[event]).length === 0) {
595
+ delete listeners[event];
596
+ }
597
+ if (Object.keys(listeners).length === 0) {
598
+ delete element.listeners;
599
+ }
600
+ }
601
+ }
602
+ element.removeEventListener(event, handler, options);
603
+ });
604
+ }
605
+
606
+ /**
607
+ * Add event listener to the target element.
608
+ * @param {Element} element - The event target.
609
+ * @param {string} type - The event type(s).
610
+ * @param {Function} listener - The event listener.
611
+ * @param {Object} options - The event options.
612
+ */
613
+ function addListener(element, type, listener) {
614
+ var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
615
+ var _handler = listener;
616
+ type.trim().split(REGEXP_SPACES).forEach(function (event) {
617
+ if (options.once && !onceSupported) {
618
+ var _element$listeners = element.listeners,
619
+ listeners = _element$listeners === void 0 ? {} : _element$listeners;
620
+ _handler = function handler() {
621
+ delete listeners[event][listener];
622
+ element.removeEventListener(event, _handler, options);
623
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
624
+ args[_key2] = arguments[_key2];
625
+ }
626
+ listener.apply(element, args);
627
+ };
628
+ if (!listeners[event]) {
629
+ listeners[event] = {};
630
+ }
631
+ if (listeners[event][listener]) {
632
+ element.removeEventListener(event, listeners[event][listener], options);
633
+ }
634
+ listeners[event][listener] = _handler;
635
+ element.listeners = listeners;
636
+ }
637
+ element.addEventListener(event, _handler, options);
638
+ });
639
+ }
640
+
641
+ /**
642
+ * Dispatch event on the target element.
643
+ * @param {Element} element - The event target.
644
+ * @param {string} type - The event type(s).
645
+ * @param {Object} data - The additional event data.
646
+ * @returns {boolean} Indicate if the event is default prevented or not.
647
+ */
648
+ function dispatchEvent(element, type, data) {
649
+ var event;
650
+
651
+ // Event and CustomEvent on IE9-11 are global objects, not constructors
652
+ if (isFunction(Event) && isFunction(CustomEvent)) {
653
+ event = new CustomEvent(type, {
654
+ detail: data,
655
+ bubbles: true,
656
+ cancelable: true
657
+ });
658
+ } else {
659
+ event = document.createEvent('CustomEvent');
660
+ event.initCustomEvent(type, true, true, data);
661
+ }
662
+ return element.dispatchEvent(event);
663
+ }
664
+
665
+ /**
666
+ * Get the offset base on the document.
667
+ * @param {Element} element - The target element.
668
+ * @returns {Object} The offset data.
669
+ */
670
+ function getOffset(element) {
671
+ var box = element.getBoundingClientRect();
672
+ return {
673
+ left: box.left + (window.pageXOffset - document.documentElement.clientLeft),
674
+ top: box.top + (window.pageYOffset - document.documentElement.clientTop)
675
+ };
676
+ }
677
+ var location = WINDOW.location;
678
+ var REGEXP_ORIGINS = /^(\w+:)\/\/([^:/?#]*):?(\d*)/i;
679
+
680
+ /**
681
+ * Check if the given URL is a cross origin URL.
682
+ * @param {string} url - The target URL.
683
+ * @returns {boolean} Returns `true` if the given URL is a cross origin URL, else `false`.
684
+ */
685
+ function isCrossOriginURL(url) {
686
+ var parts = url.match(REGEXP_ORIGINS);
687
+ return parts !== null && (parts[1] !== location.protocol || parts[2] !== location.hostname || parts[3] !== location.port);
688
+ }
689
+
690
+ /**
691
+ * Add timestamp to the given URL.
692
+ * @param {string} url - The target URL.
693
+ * @returns {string} The result URL.
694
+ */
695
+ function addTimestamp(url) {
696
+ var timestamp = "timestamp=".concat(new Date().getTime());
697
+ return url + (url.indexOf('?') === -1 ? '?' : '&') + timestamp;
698
+ }
699
+
700
+ /**
701
+ * Get transforms base on the given object.
702
+ * @param {Object} obj - The target object.
703
+ * @returns {string} A string contains transform values.
704
+ */
705
+ function getTransforms(_ref) {
706
+ var rotate = _ref.rotate,
707
+ scaleX = _ref.scaleX,
708
+ scaleY = _ref.scaleY,
709
+ translateX = _ref.translateX,
710
+ translateY = _ref.translateY;
711
+ var values = [];
712
+ if (isNumber(translateX) && translateX !== 0) {
713
+ values.push("translateX(".concat(translateX, "px)"));
714
+ }
715
+ if (isNumber(translateY) && translateY !== 0) {
716
+ values.push("translateY(".concat(translateY, "px)"));
717
+ }
718
+
719
+ // Rotate should come first before scale to match orientation transform
720
+ if (isNumber(rotate) && rotate !== 0) {
721
+ values.push("rotate(".concat(rotate, "deg)"));
722
+ }
723
+ if (isNumber(scaleX) && scaleX !== 1) {
724
+ values.push("scaleX(".concat(scaleX, ")"));
725
+ }
726
+ if (isNumber(scaleY) && scaleY !== 1) {
727
+ values.push("scaleY(".concat(scaleY, ")"));
728
+ }
729
+ var transform = values.length ? values.join(' ') : 'none';
730
+ return {
731
+ WebkitTransform: transform,
732
+ msTransform: transform,
733
+ transform: transform
734
+ };
735
+ }
736
+
737
+ /**
738
+ * Get the max ratio of a group of pointers.
739
+ * @param {string} pointers - The target pointers.
740
+ * @returns {number} The result ratio.
741
+ */
742
+ function getMaxZoomRatio(pointers) {
743
+ var pointers2 = _objectSpread2({}, pointers);
744
+ var maxRatio = 0;
745
+ forEach(pointers, function (pointer, pointerId) {
746
+ delete pointers2[pointerId];
747
+ forEach(pointers2, function (pointer2) {
748
+ var x1 = Math.abs(pointer.startX - pointer2.startX);
749
+ var y1 = Math.abs(pointer.startY - pointer2.startY);
750
+ var x2 = Math.abs(pointer.endX - pointer2.endX);
751
+ var y2 = Math.abs(pointer.endY - pointer2.endY);
752
+ var z1 = Math.sqrt(x1 * x1 + y1 * y1);
753
+ var z2 = Math.sqrt(x2 * x2 + y2 * y2);
754
+ var ratio = (z2 - z1) / z1;
755
+ if (Math.abs(ratio) > Math.abs(maxRatio)) {
756
+ maxRatio = ratio;
757
+ }
758
+ });
759
+ });
760
+ return maxRatio;
761
+ }
762
+
763
+ /**
764
+ * Get a pointer from an event object.
765
+ * @param {Object} event - The target event object.
766
+ * @param {boolean} endOnly - Indicates if only returns the end point coordinate or not.
767
+ * @returns {Object} The result pointer contains start and/or end point coordinates.
768
+ */
769
+ function getPointer(_ref2, endOnly) {
770
+ var pageX = _ref2.pageX,
771
+ pageY = _ref2.pageY;
772
+ var end = {
773
+ endX: pageX,
774
+ endY: pageY
775
+ };
776
+ return endOnly ? end : _objectSpread2({
777
+ startX: pageX,
778
+ startY: pageY
779
+ }, end);
780
+ }
781
+
782
+ /**
783
+ * Get the center point coordinate of a group of pointers.
784
+ * @param {Object} pointers - The target pointers.
785
+ * @returns {Object} The center point coordinate.
786
+ */
787
+ function getPointersCenter(pointers) {
788
+ var pageX = 0;
789
+ var pageY = 0;
790
+ var count = 0;
791
+ forEach(pointers, function (_ref3) {
792
+ var startX = _ref3.startX,
793
+ startY = _ref3.startY;
794
+ pageX += startX;
795
+ pageY += startY;
796
+ count += 1;
797
+ });
798
+ pageX /= count;
799
+ pageY /= count;
800
+ return {
801
+ pageX: pageX,
802
+ pageY: pageY
803
+ };
804
+ }
805
+
806
+ /**
807
+ * Get the max sizes in a rectangle under the given aspect ratio.
808
+ * @param {Object} data - The original sizes.
809
+ * @param {string} [type='contain'] - The adjust type.
810
+ * @returns {Object} The result sizes.
811
+ */
812
+ function getAdjustedSizes(_ref4) {
813
+ var aspectRatio = _ref4.aspectRatio,
814
+ height = _ref4.height,
815
+ width = _ref4.width;
816
+ var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'contain';
817
+ var isValidWidth = isPositiveNumber(width);
818
+ var isValidHeight = isPositiveNumber(height);
819
+ if (isValidWidth && isValidHeight) {
820
+ var adjustedWidth = height * aspectRatio;
821
+ if (type === 'contain' && adjustedWidth > width || type === 'cover' && adjustedWidth < width) {
822
+ height = width / aspectRatio;
823
+ } else {
824
+ width = height * aspectRatio;
825
+ }
826
+ } else if (isValidWidth) {
827
+ height = width / aspectRatio;
828
+ } else if (isValidHeight) {
829
+ width = height * aspectRatio;
830
+ }
831
+ return {
832
+ width: width,
833
+ height: height
834
+ };
835
+ }
836
+
837
+ /**
838
+ * Get the new sizes of a rectangle after rotated.
839
+ * @param {Object} data - The original sizes.
840
+ * @returns {Object} The result sizes.
841
+ */
842
+ function getRotatedSizes(_ref5) {
843
+ var width = _ref5.width,
844
+ height = _ref5.height,
845
+ degree = _ref5.degree;
846
+ degree = Math.abs(degree) % 180;
847
+ if (degree === 90) {
848
+ return {
849
+ width: height,
850
+ height: width
851
+ };
852
+ }
853
+ var arc = degree % 90 * Math.PI / 180;
854
+ var sinArc = Math.sin(arc);
855
+ var cosArc = Math.cos(arc);
856
+ var newWidth = width * cosArc + height * sinArc;
857
+ var newHeight = width * sinArc + height * cosArc;
858
+ return degree > 90 ? {
859
+ width: newHeight,
860
+ height: newWidth
861
+ } : {
862
+ width: newWidth,
863
+ height: newHeight
864
+ };
865
+ }
866
+
867
+ /**
868
+ * Get a canvas which drew the given image.
869
+ * @param {HTMLImageElement} image - The image for drawing.
870
+ * @param {Object} imageData - The image data.
871
+ * @param {Object} canvasData - The canvas data.
872
+ * @param {Object} options - The options.
873
+ * @returns {HTMLCanvasElement} The result canvas.
874
+ */
875
+ function getSourceCanvas(image, _ref6, _ref7, _ref8) {
876
+ var imageAspectRatio = _ref6.aspectRatio,
877
+ imageNaturalWidth = _ref6.naturalWidth,
878
+ imageNaturalHeight = _ref6.naturalHeight,
879
+ _ref6$rotate = _ref6.rotate,
880
+ rotate = _ref6$rotate === void 0 ? 0 : _ref6$rotate,
881
+ _ref6$scaleX = _ref6.scaleX,
882
+ scaleX = _ref6$scaleX === void 0 ? 1 : _ref6$scaleX,
883
+ _ref6$scaleY = _ref6.scaleY,
884
+ scaleY = _ref6$scaleY === void 0 ? 1 : _ref6$scaleY;
885
+ var aspectRatio = _ref7.aspectRatio,
886
+ naturalWidth = _ref7.naturalWidth,
887
+ naturalHeight = _ref7.naturalHeight;
888
+ var _ref8$fillColor = _ref8.fillColor,
889
+ fillColor = _ref8$fillColor === void 0 ? 'transparent' : _ref8$fillColor,
890
+ _ref8$imageSmoothingE = _ref8.imageSmoothingEnabled,
891
+ imageSmoothingEnabled = _ref8$imageSmoothingE === void 0 ? true : _ref8$imageSmoothingE,
892
+ _ref8$imageSmoothingQ = _ref8.imageSmoothingQuality,
893
+ imageSmoothingQuality = _ref8$imageSmoothingQ === void 0 ? 'low' : _ref8$imageSmoothingQ,
894
+ _ref8$maxWidth = _ref8.maxWidth,
895
+ maxWidth = _ref8$maxWidth === void 0 ? Infinity : _ref8$maxWidth,
896
+ _ref8$maxHeight = _ref8.maxHeight,
897
+ maxHeight = _ref8$maxHeight === void 0 ? Infinity : _ref8$maxHeight,
898
+ _ref8$minWidth = _ref8.minWidth,
899
+ minWidth = _ref8$minWidth === void 0 ? 0 : _ref8$minWidth,
900
+ _ref8$minHeight = _ref8.minHeight,
901
+ minHeight = _ref8$minHeight === void 0 ? 0 : _ref8$minHeight;
902
+ var canvas = document.createElement('canvas');
903
+ var context = canvas.getContext('2d');
904
+ var maxSizes = getAdjustedSizes({
905
+ aspectRatio: aspectRatio,
906
+ width: maxWidth,
907
+ height: maxHeight
908
+ });
909
+ var minSizes = getAdjustedSizes({
910
+ aspectRatio: aspectRatio,
911
+ width: minWidth,
912
+ height: minHeight
913
+ }, 'cover');
914
+ var width = Math.min(maxSizes.width, Math.max(minSizes.width, naturalWidth));
915
+ var height = Math.min(maxSizes.height, Math.max(minSizes.height, naturalHeight));
916
+
917
+ // Note: should always use image's natural sizes for drawing as
918
+ // imageData.naturalWidth === canvasData.naturalHeight when rotate % 180 === 90
919
+ var destMaxSizes = getAdjustedSizes({
920
+ aspectRatio: imageAspectRatio,
921
+ width: maxWidth,
922
+ height: maxHeight
923
+ });
924
+ var destMinSizes = getAdjustedSizes({
925
+ aspectRatio: imageAspectRatio,
926
+ width: minWidth,
927
+ height: minHeight
928
+ }, 'cover');
929
+ var destWidth = Math.min(destMaxSizes.width, Math.max(destMinSizes.width, imageNaturalWidth));
930
+ var destHeight = Math.min(destMaxSizes.height, Math.max(destMinSizes.height, imageNaturalHeight));
931
+ var params = [-destWidth / 2, -destHeight / 2, destWidth, destHeight];
932
+ canvas.width = normalizeDecimalNumber(width);
933
+ canvas.height = normalizeDecimalNumber(height);
934
+ context.fillStyle = fillColor;
935
+ context.fillRect(0, 0, width, height);
936
+ context.save();
937
+ context.translate(width / 2, height / 2);
938
+ context.rotate(rotate * Math.PI / 180);
939
+ context.scale(scaleX, scaleY);
940
+ context.imageSmoothingEnabled = imageSmoothingEnabled;
941
+ context.imageSmoothingQuality = imageSmoothingQuality;
942
+ context.drawImage.apply(context, [image].concat(_toConsumableArray(params.map(function (param) {
943
+ return Math.floor(normalizeDecimalNumber(param));
944
+ }))));
945
+ context.restore();
946
+ return canvas;
947
+ }
948
+ var fromCharCode = String.fromCharCode;
949
+
950
+ /**
951
+ * Get string from char code in data view.
952
+ * @param {DataView} dataView - The data view for read.
953
+ * @param {number} start - The start index.
954
+ * @param {number} length - The read length.
955
+ * @returns {string} The read result.
956
+ */
957
+ function getStringFromCharCode(dataView, start, length) {
958
+ var str = '';
959
+ length += start;
960
+ for (var i = start; i < length; i += 1) {
961
+ str += fromCharCode(dataView.getUint8(i));
962
+ }
963
+ return str;
964
+ }
965
+ var REGEXP_DATA_URL_HEAD = /^data:.*,/;
966
+
967
+ /**
968
+ * Transform Data URL to array buffer.
969
+ * @param {string} dataURL - The Data URL to transform.
970
+ * @returns {ArrayBuffer} The result array buffer.
971
+ */
972
+ function dataURLToArrayBuffer(dataURL) {
973
+ var base64 = dataURL.replace(REGEXP_DATA_URL_HEAD, '');
974
+ var binary = atob(base64);
975
+ var arrayBuffer = new ArrayBuffer(binary.length);
976
+ var uint8 = new Uint8Array(arrayBuffer);
977
+ forEach(uint8, function (value, i) {
978
+ uint8[i] = binary.charCodeAt(i);
979
+ });
980
+ return arrayBuffer;
981
+ }
982
+
983
+ /**
984
+ * Transform array buffer to Data URL.
985
+ * @param {ArrayBuffer} arrayBuffer - The array buffer to transform.
986
+ * @param {string} mimeType - The mime type of the Data URL.
987
+ * @returns {string} The result Data URL.
988
+ */
989
+ function arrayBufferToDataURL(arrayBuffer, mimeType) {
990
+ var chunks = [];
991
+
992
+ // Chunk Typed Array for better performance (#435)
993
+ var chunkSize = 8192;
994
+ var uint8 = new Uint8Array(arrayBuffer);
995
+ while (uint8.length > 0) {
996
+ // XXX: Babel's `toConsumableArray` helper will throw error in IE or Safari 9
997
+ // eslint-disable-next-line prefer-spread
998
+ chunks.push(fromCharCode.apply(null, toArray(uint8.subarray(0, chunkSize))));
999
+ uint8 = uint8.subarray(chunkSize);
1000
+ }
1001
+ return "data:".concat(mimeType, ";base64,").concat(btoa(chunks.join('')));
1002
+ }
1003
+
1004
+ /**
1005
+ * Get orientation value from given array buffer.
1006
+ * @param {ArrayBuffer} arrayBuffer - The array buffer to read.
1007
+ * @returns {number} The read orientation value.
1008
+ */
1009
+ function resetAndGetOrientation(arrayBuffer) {
1010
+ var dataView = new DataView(arrayBuffer);
1011
+ var orientation;
1012
+
1013
+ // Ignores range error when the image does not have correct Exif information
1014
+ try {
1015
+ var littleEndian;
1016
+ var app1Start;
1017
+ var ifdStart;
1018
+
1019
+ // Only handle JPEG image (start by 0xFFD8)
1020
+ if (dataView.getUint8(0) === 0xFF && dataView.getUint8(1) === 0xD8) {
1021
+ var length = dataView.byteLength;
1022
+ var offset = 2;
1023
+ while (offset + 1 < length) {
1024
+ if (dataView.getUint8(offset) === 0xFF && dataView.getUint8(offset + 1) === 0xE1) {
1025
+ app1Start = offset;
1026
+ break;
1027
+ }
1028
+ offset += 1;
1029
+ }
1030
+ }
1031
+ if (app1Start) {
1032
+ var exifIDCode = app1Start + 4;
1033
+ var tiffOffset = app1Start + 10;
1034
+ if (getStringFromCharCode(dataView, exifIDCode, 4) === 'Exif') {
1035
+ var endianness = dataView.getUint16(tiffOffset);
1036
+ littleEndian = endianness === 0x4949;
1037
+ if (littleEndian || endianness === 0x4D4D /* bigEndian */) {
1038
+ if (dataView.getUint16(tiffOffset + 2, littleEndian) === 0x002A) {
1039
+ var firstIFDOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
1040
+ if (firstIFDOffset >= 0x00000008) {
1041
+ ifdStart = tiffOffset + firstIFDOffset;
1042
+ }
1043
+ }
1044
+ }
1045
+ }
1046
+ }
1047
+ if (ifdStart) {
1048
+ var _length = dataView.getUint16(ifdStart, littleEndian);
1049
+ var _offset;
1050
+ var i;
1051
+ for (i = 0; i < _length; i += 1) {
1052
+ _offset = ifdStart + i * 12 + 2;
1053
+ if (dataView.getUint16(_offset, littleEndian) === 0x0112 /* Orientation */) {
1054
+ // 8 is the offset of the current tag's value
1055
+ _offset += 8;
1056
+
1057
+ // Get the original orientation value
1058
+ orientation = dataView.getUint16(_offset, littleEndian);
1059
+
1060
+ // Override the orientation with its default value
1061
+ dataView.setUint16(_offset, 1, littleEndian);
1062
+ break;
1063
+ }
1064
+ }
1065
+ }
1066
+ } catch (error) {
1067
+ orientation = 1;
1068
+ }
1069
+ return orientation;
1070
+ }
1071
+
1072
+ /**
1073
+ * Parse Exif Orientation value.
1074
+ * @param {number} orientation - The orientation to parse.
1075
+ * @returns {Object} The parsed result.
1076
+ */
1077
+ function parseOrientation(orientation) {
1078
+ var rotate = 0;
1079
+ var scaleX = 1;
1080
+ var scaleY = 1;
1081
+ switch (orientation) {
1082
+ // Flip horizontal
1083
+ case 2:
1084
+ scaleX = -1;
1085
+ break;
1086
+
1087
+ // Rotate left 180°
1088
+ case 3:
1089
+ rotate = -180;
1090
+ break;
1091
+
1092
+ // Flip vertical
1093
+ case 4:
1094
+ scaleY = -1;
1095
+ break;
1096
+
1097
+ // Flip vertical and rotate right 90°
1098
+ case 5:
1099
+ rotate = 90;
1100
+ scaleY = -1;
1101
+ break;
1102
+
1103
+ // Rotate right 90°
1104
+ case 6:
1105
+ rotate = 90;
1106
+ break;
1107
+
1108
+ // Flip horizontal and rotate right 90°
1109
+ case 7:
1110
+ rotate = 90;
1111
+ scaleX = -1;
1112
+ break;
1113
+
1114
+ // Rotate left 90°
1115
+ case 8:
1116
+ rotate = -90;
1117
+ break;
1118
+ }
1119
+ return {
1120
+ rotate: rotate,
1121
+ scaleX: scaleX,
1122
+ scaleY: scaleY
1123
+ };
1124
+ }
1125
+
1126
+ var render = {
1127
+ render: function render() {
1128
+ this.initContainer();
1129
+ this.initCanvas();
1130
+ this.initCropBox();
1131
+ this.renderCanvas();
1132
+ if (this.cropped) {
1133
+ this.renderCropBox();
1134
+ }
1135
+ },
1136
+ initContainer: function initContainer() {
1137
+ var element = this.element,
1138
+ options = this.options,
1139
+ container = this.container,
1140
+ cropper = this.cropper;
1141
+ var minWidth = Number(options.minContainerWidth);
1142
+ var minHeight = Number(options.minContainerHeight);
1143
+ addClass(cropper, CLASS_HIDDEN);
1144
+ removeClass(element, CLASS_HIDDEN);
1145
+ var containerData = {
1146
+ width: Math.max(container.offsetWidth, minWidth >= 0 ? minWidth : MIN_CONTAINER_WIDTH),
1147
+ height: Math.max(container.offsetHeight, minHeight >= 0 ? minHeight : MIN_CONTAINER_HEIGHT)
1148
+ };
1149
+ this.containerData = containerData;
1150
+ setStyle(cropper, {
1151
+ width: containerData.width,
1152
+ height: containerData.height
1153
+ });
1154
+ addClass(element, CLASS_HIDDEN);
1155
+ removeClass(cropper, CLASS_HIDDEN);
1156
+ },
1157
+ // Canvas (image wrapper)
1158
+ initCanvas: function initCanvas() {
1159
+ var containerData = this.containerData,
1160
+ imageData = this.imageData;
1161
+ var viewMode = this.options.viewMode;
1162
+ var rotated = Math.abs(imageData.rotate) % 180 === 90;
1163
+ var naturalWidth = rotated ? imageData.naturalHeight : imageData.naturalWidth;
1164
+ var naturalHeight = rotated ? imageData.naturalWidth : imageData.naturalHeight;
1165
+ var aspectRatio = naturalWidth / naturalHeight;
1166
+ var canvasWidth = containerData.width;
1167
+ var canvasHeight = containerData.height;
1168
+ if (containerData.height * aspectRatio > containerData.width) {
1169
+ if (viewMode === 3) {
1170
+ canvasWidth = containerData.height * aspectRatio;
1171
+ } else {
1172
+ canvasHeight = containerData.width / aspectRatio;
1173
+ }
1174
+ } else if (viewMode === 3) {
1175
+ canvasHeight = containerData.width / aspectRatio;
1176
+ } else {
1177
+ canvasWidth = containerData.height * aspectRatio;
1178
+ }
1179
+ var canvasData = {
1180
+ aspectRatio: aspectRatio,
1181
+ naturalWidth: naturalWidth,
1182
+ naturalHeight: naturalHeight,
1183
+ width: canvasWidth,
1184
+ height: canvasHeight
1185
+ };
1186
+ this.canvasData = canvasData;
1187
+ this.limited = viewMode === 1 || viewMode === 2;
1188
+ this.limitCanvas(true, true);
1189
+ canvasData.width = Math.min(Math.max(canvasData.width, canvasData.minWidth), canvasData.maxWidth);
1190
+ canvasData.height = Math.min(Math.max(canvasData.height, canvasData.minHeight), canvasData.maxHeight);
1191
+ canvasData.left = (containerData.width - canvasData.width) / 2;
1192
+ canvasData.top = (containerData.height - canvasData.height) / 2;
1193
+ canvasData.oldLeft = canvasData.left;
1194
+ canvasData.oldTop = canvasData.top;
1195
+ this.initialCanvasData = assign({}, canvasData);
1196
+ },
1197
+ limitCanvas: function limitCanvas(sizeLimited, positionLimited) {
1198
+ var options = this.options,
1199
+ containerData = this.containerData,
1200
+ canvasData = this.canvasData,
1201
+ cropBoxData = this.cropBoxData;
1202
+ var viewMode = options.viewMode;
1203
+ var aspectRatio = canvasData.aspectRatio;
1204
+ var cropped = this.cropped && cropBoxData;
1205
+ if (sizeLimited) {
1206
+ var minCanvasWidth = Number(options.minCanvasWidth) || 0;
1207
+ var minCanvasHeight = Number(options.minCanvasHeight) || 0;
1208
+ if (viewMode > 1) {
1209
+ minCanvasWidth = Math.max(minCanvasWidth, containerData.width);
1210
+ minCanvasHeight = Math.max(minCanvasHeight, containerData.height);
1211
+ if (viewMode === 3) {
1212
+ if (minCanvasHeight * aspectRatio > minCanvasWidth) {
1213
+ minCanvasWidth = minCanvasHeight * aspectRatio;
1214
+ } else {
1215
+ minCanvasHeight = minCanvasWidth / aspectRatio;
1216
+ }
1217
+ }
1218
+ } else if (viewMode > 0) {
1219
+ if (minCanvasWidth) {
1220
+ minCanvasWidth = Math.max(minCanvasWidth, cropped ? cropBoxData.width : 0);
1221
+ } else if (minCanvasHeight) {
1222
+ minCanvasHeight = Math.max(minCanvasHeight, cropped ? cropBoxData.height : 0);
1223
+ } else if (cropped) {
1224
+ minCanvasWidth = cropBoxData.width;
1225
+ minCanvasHeight = cropBoxData.height;
1226
+ if (minCanvasHeight * aspectRatio > minCanvasWidth) {
1227
+ minCanvasWidth = minCanvasHeight * aspectRatio;
1228
+ } else {
1229
+ minCanvasHeight = minCanvasWidth / aspectRatio;
1230
+ }
1231
+ }
1232
+ }
1233
+ var _getAdjustedSizes = getAdjustedSizes({
1234
+ aspectRatio: aspectRatio,
1235
+ width: minCanvasWidth,
1236
+ height: minCanvasHeight
1237
+ });
1238
+ minCanvasWidth = _getAdjustedSizes.width;
1239
+ minCanvasHeight = _getAdjustedSizes.height;
1240
+ canvasData.minWidth = minCanvasWidth;
1241
+ canvasData.minHeight = minCanvasHeight;
1242
+ canvasData.maxWidth = Infinity;
1243
+ canvasData.maxHeight = Infinity;
1244
+ }
1245
+ if (positionLimited) {
1246
+ if (viewMode > (cropped ? 0 : 1)) {
1247
+ var newCanvasLeft = containerData.width - canvasData.width;
1248
+ var newCanvasTop = containerData.height - canvasData.height;
1249
+ canvasData.minLeft = Math.min(0, newCanvasLeft);
1250
+ canvasData.minTop = Math.min(0, newCanvasTop);
1251
+ canvasData.maxLeft = Math.max(0, newCanvasLeft);
1252
+ canvasData.maxTop = Math.max(0, newCanvasTop);
1253
+ if (cropped && this.limited) {
1254
+ canvasData.minLeft = Math.min(cropBoxData.left, cropBoxData.left + (cropBoxData.width - canvasData.width));
1255
+ canvasData.minTop = Math.min(cropBoxData.top, cropBoxData.top + (cropBoxData.height - canvasData.height));
1256
+ canvasData.maxLeft = cropBoxData.left;
1257
+ canvasData.maxTop = cropBoxData.top;
1258
+ if (viewMode === 2) {
1259
+ if (canvasData.width >= containerData.width) {
1260
+ canvasData.minLeft = Math.min(0, newCanvasLeft);
1261
+ canvasData.maxLeft = Math.max(0, newCanvasLeft);
1262
+ }
1263
+ if (canvasData.height >= containerData.height) {
1264
+ canvasData.minTop = Math.min(0, newCanvasTop);
1265
+ canvasData.maxTop = Math.max(0, newCanvasTop);
1266
+ }
1267
+ }
1268
+ }
1269
+ } else {
1270
+ canvasData.minLeft = -canvasData.width;
1271
+ canvasData.minTop = -canvasData.height;
1272
+ canvasData.maxLeft = containerData.width;
1273
+ canvasData.maxTop = containerData.height;
1274
+ }
1275
+ }
1276
+ },
1277
+ renderCanvas: function renderCanvas(changed, transformed) {
1278
+ var canvasData = this.canvasData,
1279
+ imageData = this.imageData;
1280
+ if (transformed) {
1281
+ var _getRotatedSizes = getRotatedSizes({
1282
+ width: imageData.naturalWidth * Math.abs(imageData.scaleX || 1),
1283
+ height: imageData.naturalHeight * Math.abs(imageData.scaleY || 1),
1284
+ degree: imageData.rotate || 0
1285
+ }),
1286
+ naturalWidth = _getRotatedSizes.width,
1287
+ naturalHeight = _getRotatedSizes.height;
1288
+ var width = canvasData.width * (naturalWidth / canvasData.naturalWidth);
1289
+ var height = canvasData.height * (naturalHeight / canvasData.naturalHeight);
1290
+ canvasData.left -= (width - canvasData.width) / 2;
1291
+ canvasData.top -= (height - canvasData.height) / 2;
1292
+ canvasData.width = width;
1293
+ canvasData.height = height;
1294
+ canvasData.aspectRatio = naturalWidth / naturalHeight;
1295
+ canvasData.naturalWidth = naturalWidth;
1296
+ canvasData.naturalHeight = naturalHeight;
1297
+ this.limitCanvas(true, false);
1298
+ }
1299
+ if (canvasData.width > canvasData.maxWidth || canvasData.width < canvasData.minWidth) {
1300
+ canvasData.left = canvasData.oldLeft;
1301
+ }
1302
+ if (canvasData.height > canvasData.maxHeight || canvasData.height < canvasData.minHeight) {
1303
+ canvasData.top = canvasData.oldTop;
1304
+ }
1305
+ canvasData.width = Math.min(Math.max(canvasData.width, canvasData.minWidth), canvasData.maxWidth);
1306
+ canvasData.height = Math.min(Math.max(canvasData.height, canvasData.minHeight), canvasData.maxHeight);
1307
+ this.limitCanvas(false, true);
1308
+ canvasData.left = Math.min(Math.max(canvasData.left, canvasData.minLeft), canvasData.maxLeft);
1309
+ canvasData.top = Math.min(Math.max(canvasData.top, canvasData.minTop), canvasData.maxTop);
1310
+ canvasData.oldLeft = canvasData.left;
1311
+ canvasData.oldTop = canvasData.top;
1312
+ setStyle(this.canvas, assign({
1313
+ width: canvasData.width,
1314
+ height: canvasData.height
1315
+ }, getTransforms({
1316
+ translateX: canvasData.left,
1317
+ translateY: canvasData.top
1318
+ })));
1319
+ this.renderImage(changed);
1320
+ if (this.cropped && this.limited) {
1321
+ this.limitCropBox(true, true);
1322
+ }
1323
+ },
1324
+ renderImage: function renderImage(changed) {
1325
+ var canvasData = this.canvasData,
1326
+ imageData = this.imageData;
1327
+ var width = imageData.naturalWidth * (canvasData.width / canvasData.naturalWidth);
1328
+ var height = imageData.naturalHeight * (canvasData.height / canvasData.naturalHeight);
1329
+ assign(imageData, {
1330
+ width: width,
1331
+ height: height,
1332
+ left: (canvasData.width - width) / 2,
1333
+ top: (canvasData.height - height) / 2
1334
+ });
1335
+ setStyle(this.image, assign({
1336
+ width: imageData.width,
1337
+ height: imageData.height
1338
+ }, getTransforms(assign({
1339
+ translateX: imageData.left,
1340
+ translateY: imageData.top
1341
+ }, imageData))));
1342
+ if (changed) {
1343
+ this.output();
1344
+ }
1345
+ },
1346
+ initCropBox: function initCropBox() {
1347
+ var options = this.options,
1348
+ canvasData = this.canvasData;
1349
+ var aspectRatio = options.aspectRatio || options.initialAspectRatio;
1350
+ var autoCropArea = Number(options.autoCropArea) || 0.8;
1351
+ var cropBoxData = {
1352
+ width: canvasData.width,
1353
+ height: canvasData.height
1354
+ };
1355
+ if (aspectRatio) {
1356
+ if (canvasData.height * aspectRatio > canvasData.width) {
1357
+ cropBoxData.height = cropBoxData.width / aspectRatio;
1358
+ } else {
1359
+ cropBoxData.width = cropBoxData.height * aspectRatio;
1360
+ }
1361
+ }
1362
+ this.cropBoxData = cropBoxData;
1363
+ this.limitCropBox(true, true);
1364
+
1365
+ // Initialize auto crop area
1366
+ cropBoxData.width = Math.min(Math.max(cropBoxData.width, cropBoxData.minWidth), cropBoxData.maxWidth);
1367
+ cropBoxData.height = Math.min(Math.max(cropBoxData.height, cropBoxData.minHeight), cropBoxData.maxHeight);
1368
+
1369
+ // The width/height of auto crop area must large than "minWidth/Height"
1370
+ cropBoxData.width = Math.max(cropBoxData.minWidth, cropBoxData.width * autoCropArea);
1371
+ cropBoxData.height = Math.max(cropBoxData.minHeight, cropBoxData.height * autoCropArea);
1372
+ cropBoxData.left = canvasData.left + (canvasData.width - cropBoxData.width) / 2;
1373
+ cropBoxData.top = canvasData.top + (canvasData.height - cropBoxData.height) / 2;
1374
+ cropBoxData.oldLeft = cropBoxData.left;
1375
+ cropBoxData.oldTop = cropBoxData.top;
1376
+ this.initialCropBoxData = assign({}, cropBoxData);
1377
+ },
1378
+ limitCropBox: function limitCropBox(sizeLimited, positionLimited) {
1379
+ var options = this.options,
1380
+ containerData = this.containerData,
1381
+ canvasData = this.canvasData,
1382
+ cropBoxData = this.cropBoxData,
1383
+ limited = this.limited;
1384
+ var aspectRatio = options.aspectRatio;
1385
+ if (sizeLimited) {
1386
+ var minCropBoxWidth = Number(options.minCropBoxWidth) || 0;
1387
+ var minCropBoxHeight = Number(options.minCropBoxHeight) || 0;
1388
+ var maxCropBoxWidth = limited ? Math.min(containerData.width, canvasData.width, canvasData.width + canvasData.left, containerData.width - canvasData.left) : containerData.width;
1389
+ var maxCropBoxHeight = limited ? Math.min(containerData.height, canvasData.height, canvasData.height + canvasData.top, containerData.height - canvasData.top) : containerData.height;
1390
+
1391
+ // The min/maxCropBoxWidth/Height must be less than container's width/height
1392
+ minCropBoxWidth = Math.min(minCropBoxWidth, containerData.width);
1393
+ minCropBoxHeight = Math.min(minCropBoxHeight, containerData.height);
1394
+ if (aspectRatio) {
1395
+ if (minCropBoxWidth && minCropBoxHeight) {
1396
+ if (minCropBoxHeight * aspectRatio > minCropBoxWidth) {
1397
+ minCropBoxHeight = minCropBoxWidth / aspectRatio;
1398
+ } else {
1399
+ minCropBoxWidth = minCropBoxHeight * aspectRatio;
1400
+ }
1401
+ } else if (minCropBoxWidth) {
1402
+ minCropBoxHeight = minCropBoxWidth / aspectRatio;
1403
+ } else if (minCropBoxHeight) {
1404
+ minCropBoxWidth = minCropBoxHeight * aspectRatio;
1405
+ }
1406
+ if (maxCropBoxHeight * aspectRatio > maxCropBoxWidth) {
1407
+ maxCropBoxHeight = maxCropBoxWidth / aspectRatio;
1408
+ } else {
1409
+ maxCropBoxWidth = maxCropBoxHeight * aspectRatio;
1410
+ }
1411
+ }
1412
+
1413
+ // The minWidth/Height must be less than maxWidth/Height
1414
+ cropBoxData.minWidth = Math.min(minCropBoxWidth, maxCropBoxWidth);
1415
+ cropBoxData.minHeight = Math.min(minCropBoxHeight, maxCropBoxHeight);
1416
+ cropBoxData.maxWidth = maxCropBoxWidth;
1417
+ cropBoxData.maxHeight = maxCropBoxHeight;
1418
+ }
1419
+ if (positionLimited) {
1420
+ if (limited) {
1421
+ cropBoxData.minLeft = Math.max(0, canvasData.left);
1422
+ cropBoxData.minTop = Math.max(0, canvasData.top);
1423
+ cropBoxData.maxLeft = Math.min(containerData.width, canvasData.left + canvasData.width) - cropBoxData.width;
1424
+ cropBoxData.maxTop = Math.min(containerData.height, canvasData.top + canvasData.height) - cropBoxData.height;
1425
+ } else {
1426
+ cropBoxData.minLeft = 0;
1427
+ cropBoxData.minTop = 0;
1428
+ cropBoxData.maxLeft = containerData.width - cropBoxData.width;
1429
+ cropBoxData.maxTop = containerData.height - cropBoxData.height;
1430
+ }
1431
+ }
1432
+ },
1433
+ renderCropBox: function renderCropBox() {
1434
+ var options = this.options,
1435
+ containerData = this.containerData,
1436
+ cropBoxData = this.cropBoxData;
1437
+ if (cropBoxData.width > cropBoxData.maxWidth || cropBoxData.width < cropBoxData.minWidth) {
1438
+ cropBoxData.left = cropBoxData.oldLeft;
1439
+ }
1440
+ if (cropBoxData.height > cropBoxData.maxHeight || cropBoxData.height < cropBoxData.minHeight) {
1441
+ cropBoxData.top = cropBoxData.oldTop;
1442
+ }
1443
+ cropBoxData.width = Math.min(Math.max(cropBoxData.width, cropBoxData.minWidth), cropBoxData.maxWidth);
1444
+ cropBoxData.height = Math.min(Math.max(cropBoxData.height, cropBoxData.minHeight), cropBoxData.maxHeight);
1445
+ this.limitCropBox(false, true);
1446
+ cropBoxData.left = Math.min(Math.max(cropBoxData.left, cropBoxData.minLeft), cropBoxData.maxLeft);
1447
+ cropBoxData.top = Math.min(Math.max(cropBoxData.top, cropBoxData.minTop), cropBoxData.maxTop);
1448
+ cropBoxData.oldLeft = cropBoxData.left;
1449
+ cropBoxData.oldTop = cropBoxData.top;
1450
+ if (options.movable && options.cropBoxMovable) {
1451
+ // Turn to move the canvas when the crop box is equal to the container
1452
+ setData(this.face, DATA_ACTION, cropBoxData.width >= containerData.width && cropBoxData.height >= containerData.height ? ACTION_MOVE : ACTION_ALL);
1453
+ }
1454
+ setStyle(this.cropBox, assign({
1455
+ width: cropBoxData.width,
1456
+ height: cropBoxData.height
1457
+ }, getTransforms({
1458
+ translateX: cropBoxData.left,
1459
+ translateY: cropBoxData.top
1460
+ })));
1461
+ if (this.cropped && this.limited) {
1462
+ this.limitCanvas(true, true);
1463
+ }
1464
+ if (!this.disabled) {
1465
+ this.output();
1466
+ }
1467
+ },
1468
+ output: function output() {
1469
+ this.preview();
1470
+ dispatchEvent(this.element, EVENT_CROP, this.getData());
1471
+ }
1472
+ };
1473
+
1474
+ var preview = {
1475
+ initPreview: function initPreview() {
1476
+ var element = this.element,
1477
+ crossOrigin = this.crossOrigin;
1478
+ var preview = this.options.preview;
1479
+ var url = crossOrigin ? this.crossOriginUrl : this.url;
1480
+ var alt = element.alt || 'The image to preview';
1481
+ var image = document.createElement('img');
1482
+ if (crossOrigin) {
1483
+ image.crossOrigin = crossOrigin;
1484
+ }
1485
+ image.src = url;
1486
+ image.alt = alt;
1487
+ this.viewBox.appendChild(image);
1488
+ this.viewBoxImage = image;
1489
+ if (!preview) {
1490
+ return;
1491
+ }
1492
+ var previews = preview;
1493
+ if (typeof preview === 'string') {
1494
+ previews = element.ownerDocument.querySelectorAll(preview);
1495
+ } else if (preview.querySelector) {
1496
+ previews = [preview];
1497
+ }
1498
+ this.previews = previews;
1499
+ forEach(previews, function (el) {
1500
+ var img = document.createElement('img');
1501
+
1502
+ // Save the original size for recover
1503
+ setData(el, DATA_PREVIEW, {
1504
+ width: el.offsetWidth,
1505
+ height: el.offsetHeight,
1506
+ html: el.innerHTML
1507
+ });
1508
+ if (crossOrigin) {
1509
+ img.crossOrigin = crossOrigin;
1510
+ }
1511
+ img.src = url;
1512
+ img.alt = alt;
1513
+
1514
+ /**
1515
+ * Override img element styles
1516
+ * Add `display:block` to avoid margin top issue
1517
+ * Add `height:auto` to override `height` attribute on IE8
1518
+ * (Occur only when margin-top <= -height)
1519
+ */
1520
+ img.style.cssText = 'display:block;' + 'width:100%;' + 'height:auto;' + 'min-width:0!important;' + 'min-height:0!important;' + 'max-width:none!important;' + 'max-height:none!important;' + 'image-orientation:0deg!important;"';
1521
+ el.innerHTML = '';
1522
+ el.appendChild(img);
1523
+ });
1524
+ },
1525
+ resetPreview: function resetPreview() {
1526
+ forEach(this.previews, function (element) {
1527
+ var data = getData(element, DATA_PREVIEW);
1528
+ setStyle(element, {
1529
+ width: data.width,
1530
+ height: data.height
1531
+ });
1532
+ element.innerHTML = data.html;
1533
+ removeData(element, DATA_PREVIEW);
1534
+ });
1535
+ },
1536
+ preview: function preview() {
1537
+ var imageData = this.imageData,
1538
+ canvasData = this.canvasData,
1539
+ cropBoxData = this.cropBoxData;
1540
+ var cropBoxWidth = cropBoxData.width,
1541
+ cropBoxHeight = cropBoxData.height;
1542
+ var width = imageData.width,
1543
+ height = imageData.height;
1544
+ var left = cropBoxData.left - canvasData.left - imageData.left;
1545
+ var top = cropBoxData.top - canvasData.top - imageData.top;
1546
+ if (!this.cropped || this.disabled) {
1547
+ return;
1548
+ }
1549
+ setStyle(this.viewBoxImage, assign({
1550
+ width: width,
1551
+ height: height
1552
+ }, getTransforms(assign({
1553
+ translateX: -left,
1554
+ translateY: -top
1555
+ }, imageData))));
1556
+ forEach(this.previews, function (element) {
1557
+ var data = getData(element, DATA_PREVIEW);
1558
+ var originalWidth = data.width;
1559
+ var originalHeight = data.height;
1560
+ var newWidth = originalWidth;
1561
+ var newHeight = originalHeight;
1562
+ var ratio = 1;
1563
+ if (cropBoxWidth) {
1564
+ ratio = originalWidth / cropBoxWidth;
1565
+ newHeight = cropBoxHeight * ratio;
1566
+ }
1567
+ if (cropBoxHeight && newHeight > originalHeight) {
1568
+ ratio = originalHeight / cropBoxHeight;
1569
+ newWidth = cropBoxWidth * ratio;
1570
+ newHeight = originalHeight;
1571
+ }
1572
+ setStyle(element, {
1573
+ width: newWidth,
1574
+ height: newHeight
1575
+ });
1576
+ setStyle(element.getElementsByTagName('img')[0], assign({
1577
+ width: width * ratio,
1578
+ height: height * ratio
1579
+ }, getTransforms(assign({
1580
+ translateX: -left * ratio,
1581
+ translateY: -top * ratio
1582
+ }, imageData))));
1583
+ });
1584
+ }
1585
+ };
1586
+
1587
+ var events = {
1588
+ bind: function bind() {
1589
+ var element = this.element,
1590
+ options = this.options,
1591
+ cropper = this.cropper;
1592
+ if (isFunction(options.cropstart)) {
1593
+ addListener(element, EVENT_CROP_START, options.cropstart);
1594
+ }
1595
+ if (isFunction(options.cropmove)) {
1596
+ addListener(element, EVENT_CROP_MOVE, options.cropmove);
1597
+ }
1598
+ if (isFunction(options.cropend)) {
1599
+ addListener(element, EVENT_CROP_END, options.cropend);
1600
+ }
1601
+ if (isFunction(options.crop)) {
1602
+ addListener(element, EVENT_CROP, options.crop);
1603
+ }
1604
+ if (isFunction(options.zoom)) {
1605
+ addListener(element, EVENT_ZOOM, options.zoom);
1606
+ }
1607
+ addListener(cropper, EVENT_POINTER_DOWN, this.onCropStart = this.cropStart.bind(this));
1608
+ if (options.zoomable && options.zoomOnWheel) {
1609
+ addListener(cropper, EVENT_WHEEL, this.onWheel = this.wheel.bind(this), {
1610
+ passive: false,
1611
+ capture: true
1612
+ });
1613
+ }
1614
+ if (options.toggleDragModeOnDblclick) {
1615
+ addListener(cropper, EVENT_DBLCLICK, this.onDblclick = this.dblclick.bind(this));
1616
+ }
1617
+ addListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onCropMove = this.cropMove.bind(this));
1618
+ addListener(element.ownerDocument, EVENT_POINTER_UP, this.onCropEnd = this.cropEnd.bind(this));
1619
+ if (options.responsive) {
1620
+ addListener(window, EVENT_RESIZE, this.onResize = this.resize.bind(this));
1621
+ }
1622
+ },
1623
+ unbind: function unbind() {
1624
+ var element = this.element,
1625
+ options = this.options,
1626
+ cropper = this.cropper;
1627
+ if (isFunction(options.cropstart)) {
1628
+ removeListener(element, EVENT_CROP_START, options.cropstart);
1629
+ }
1630
+ if (isFunction(options.cropmove)) {
1631
+ removeListener(element, EVENT_CROP_MOVE, options.cropmove);
1632
+ }
1633
+ if (isFunction(options.cropend)) {
1634
+ removeListener(element, EVENT_CROP_END, options.cropend);
1635
+ }
1636
+ if (isFunction(options.crop)) {
1637
+ removeListener(element, EVENT_CROP, options.crop);
1638
+ }
1639
+ if (isFunction(options.zoom)) {
1640
+ removeListener(element, EVENT_ZOOM, options.zoom);
1641
+ }
1642
+ removeListener(cropper, EVENT_POINTER_DOWN, this.onCropStart);
1643
+ if (options.zoomable && options.zoomOnWheel) {
1644
+ removeListener(cropper, EVENT_WHEEL, this.onWheel, {
1645
+ passive: false,
1646
+ capture: true
1647
+ });
1648
+ }
1649
+ if (options.toggleDragModeOnDblclick) {
1650
+ removeListener(cropper, EVENT_DBLCLICK, this.onDblclick);
1651
+ }
1652
+ removeListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onCropMove);
1653
+ removeListener(element.ownerDocument, EVENT_POINTER_UP, this.onCropEnd);
1654
+ if (options.responsive) {
1655
+ removeListener(window, EVENT_RESIZE, this.onResize);
1656
+ }
1657
+ }
1658
+ };
1659
+
1660
+ var handlers = {
1661
+ resize: function resize() {
1662
+ if (this.disabled) {
1663
+ return;
1664
+ }
1665
+ var options = this.options,
1666
+ container = this.container,
1667
+ containerData = this.containerData;
1668
+ var ratioX = container.offsetWidth / containerData.width;
1669
+ var ratioY = container.offsetHeight / containerData.height;
1670
+ var ratio = Math.abs(ratioX - 1) > Math.abs(ratioY - 1) ? ratioX : ratioY;
1671
+
1672
+ // Resize when width changed or height changed
1673
+ if (ratio !== 1) {
1674
+ var canvasData;
1675
+ var cropBoxData;
1676
+ if (options.restore) {
1677
+ canvasData = this.getCanvasData();
1678
+ cropBoxData = this.getCropBoxData();
1679
+ }
1680
+ this.render();
1681
+ if (options.restore) {
1682
+ this.setCanvasData(forEach(canvasData, function (n, i) {
1683
+ canvasData[i] = n * ratio;
1684
+ }));
1685
+ this.setCropBoxData(forEach(cropBoxData, function (n, i) {
1686
+ cropBoxData[i] = n * ratio;
1687
+ }));
1688
+ }
1689
+ }
1690
+ },
1691
+ dblclick: function dblclick() {
1692
+ if (this.disabled || this.options.dragMode === DRAG_MODE_NONE) {
1693
+ return;
1694
+ }
1695
+ this.setDragMode(hasClass(this.dragBox, CLASS_CROP) ? DRAG_MODE_MOVE : DRAG_MODE_CROP);
1696
+ },
1697
+ wheel: function wheel(event) {
1698
+ var _this = this;
1699
+ var ratio = Number(this.options.wheelZoomRatio) || 0.1;
1700
+ var delta = 1;
1701
+ if (this.disabled) {
1702
+ return;
1703
+ }
1704
+ event.preventDefault();
1705
+
1706
+ // Limit wheel speed to prevent zoom too fast (#21)
1707
+ if (this.wheeling) {
1708
+ return;
1709
+ }
1710
+ this.wheeling = true;
1711
+ setTimeout(function () {
1712
+ _this.wheeling = false;
1713
+ }, 50);
1714
+ if (event.deltaY) {
1715
+ delta = event.deltaY > 0 ? 1 : -1;
1716
+ } else if (event.wheelDelta) {
1717
+ delta = -event.wheelDelta / 120;
1718
+ } else if (event.detail) {
1719
+ delta = event.detail > 0 ? 1 : -1;
1720
+ }
1721
+ this.zoom(-delta * ratio, event);
1722
+ },
1723
+ cropStart: function cropStart(event) {
1724
+ var buttons = event.buttons,
1725
+ button = event.button;
1726
+ if (this.disabled
1727
+
1728
+ // Handle mouse event and pointer event and ignore touch event
1729
+ || (event.type === 'mousedown' || event.type === 'pointerdown' && event.pointerType === 'mouse') && (
1730
+ // No primary button (Usually the left button)
1731
+ isNumber(buttons) && buttons !== 1 || isNumber(button) && button !== 0
1732
+
1733
+ // Open context menu
1734
+ || event.ctrlKey)) {
1735
+ return;
1736
+ }
1737
+ var options = this.options,
1738
+ pointers = this.pointers;
1739
+ var action;
1740
+ if (event.changedTouches) {
1741
+ // Handle touch event
1742
+ forEach(event.changedTouches, function (touch) {
1743
+ pointers[touch.identifier] = getPointer(touch);
1744
+ });
1745
+ } else {
1746
+ // Handle mouse event and pointer event
1747
+ pointers[event.pointerId || 0] = getPointer(event);
1748
+ }
1749
+ if (Object.keys(pointers).length > 1 && options.zoomable && options.zoomOnTouch) {
1750
+ action = ACTION_ZOOM;
1751
+ } else {
1752
+ action = getData(event.target, DATA_ACTION);
1753
+ }
1754
+ if (!REGEXP_ACTIONS.test(action)) {
1755
+ return;
1756
+ }
1757
+ if (dispatchEvent(this.element, EVENT_CROP_START, {
1758
+ originalEvent: event,
1759
+ action: action
1760
+ }) === false) {
1761
+ return;
1762
+ }
1763
+
1764
+ // This line is required for preventing page zooming in iOS browsers
1765
+ event.preventDefault();
1766
+ this.action = action;
1767
+ this.cropping = false;
1768
+ if (action === ACTION_CROP) {
1769
+ this.cropping = true;
1770
+ addClass(this.dragBox, CLASS_MODAL);
1771
+ }
1772
+ },
1773
+ cropMove: function cropMove(event) {
1774
+ var action = this.action;
1775
+ if (this.disabled || !action) {
1776
+ return;
1777
+ }
1778
+ var pointers = this.pointers;
1779
+ event.preventDefault();
1780
+ if (dispatchEvent(this.element, EVENT_CROP_MOVE, {
1781
+ originalEvent: event,
1782
+ action: action
1783
+ }) === false) {
1784
+ return;
1785
+ }
1786
+ if (event.changedTouches) {
1787
+ forEach(event.changedTouches, function (touch) {
1788
+ // The first parameter should not be undefined (#432)
1789
+ assign(pointers[touch.identifier] || {}, getPointer(touch, true));
1790
+ });
1791
+ } else {
1792
+ assign(pointers[event.pointerId || 0] || {}, getPointer(event, true));
1793
+ }
1794
+ this.change(event);
1795
+ },
1796
+ cropEnd: function cropEnd(event) {
1797
+ if (this.disabled) {
1798
+ return;
1799
+ }
1800
+ var action = this.action,
1801
+ pointers = this.pointers;
1802
+ if (event.changedTouches) {
1803
+ forEach(event.changedTouches, function (touch) {
1804
+ delete pointers[touch.identifier];
1805
+ });
1806
+ } else {
1807
+ delete pointers[event.pointerId || 0];
1808
+ }
1809
+ if (!action) {
1810
+ return;
1811
+ }
1812
+ event.preventDefault();
1813
+ if (!Object.keys(pointers).length) {
1814
+ this.action = '';
1815
+ }
1816
+ if (this.cropping) {
1817
+ this.cropping = false;
1818
+ toggleClass(this.dragBox, CLASS_MODAL, this.cropped && this.options.modal);
1819
+ }
1820
+ dispatchEvent(this.element, EVENT_CROP_END, {
1821
+ originalEvent: event,
1822
+ action: action
1823
+ });
1824
+ }
1825
+ };
1826
+
1827
+ var change = {
1828
+ change: function change(event) {
1829
+ var options = this.options,
1830
+ canvasData = this.canvasData,
1831
+ containerData = this.containerData,
1832
+ cropBoxData = this.cropBoxData,
1833
+ pointers = this.pointers;
1834
+ var action = this.action;
1835
+ var aspectRatio = options.aspectRatio;
1836
+ var left = cropBoxData.left,
1837
+ top = cropBoxData.top,
1838
+ width = cropBoxData.width,
1839
+ height = cropBoxData.height;
1840
+ var right = left + width;
1841
+ var bottom = top + height;
1842
+ var minLeft = 0;
1843
+ var minTop = 0;
1844
+ var maxWidth = containerData.width;
1845
+ var maxHeight = containerData.height;
1846
+ var renderable = true;
1847
+ var offset;
1848
+
1849
+ // Locking aspect ratio in "free mode" by holding shift key
1850
+ if (!aspectRatio && event.shiftKey) {
1851
+ aspectRatio = width && height ? width / height : 1;
1852
+ }
1853
+ if (this.limited) {
1854
+ minLeft = cropBoxData.minLeft;
1855
+ minTop = cropBoxData.minTop;
1856
+ maxWidth = minLeft + Math.min(containerData.width, canvasData.width, canvasData.left + canvasData.width);
1857
+ maxHeight = minTop + Math.min(containerData.height, canvasData.height, canvasData.top + canvasData.height);
1858
+ }
1859
+ var pointer = pointers[Object.keys(pointers)[0]];
1860
+ var range = {
1861
+ x: pointer.endX - pointer.startX,
1862
+ y: pointer.endY - pointer.startY
1863
+ };
1864
+ var check = function check(side) {
1865
+ switch (side) {
1866
+ case ACTION_EAST:
1867
+ if (right + range.x > maxWidth) {
1868
+ range.x = maxWidth - right;
1869
+ }
1870
+ break;
1871
+ case ACTION_WEST:
1872
+ if (left + range.x < minLeft) {
1873
+ range.x = minLeft - left;
1874
+ }
1875
+ break;
1876
+ case ACTION_NORTH:
1877
+ if (top + range.y < minTop) {
1878
+ range.y = minTop - top;
1879
+ }
1880
+ break;
1881
+ case ACTION_SOUTH:
1882
+ if (bottom + range.y > maxHeight) {
1883
+ range.y = maxHeight - bottom;
1884
+ }
1885
+ break;
1886
+ }
1887
+ };
1888
+ switch (action) {
1889
+ // Move crop box
1890
+ case ACTION_ALL:
1891
+ left += range.x;
1892
+ top += range.y;
1893
+ break;
1894
+
1895
+ // Resize crop box
1896
+ case ACTION_EAST:
1897
+ if (range.x >= 0 && (right >= maxWidth || aspectRatio && (top <= minTop || bottom >= maxHeight))) {
1898
+ renderable = false;
1899
+ break;
1900
+ }
1901
+ check(ACTION_EAST);
1902
+ width += range.x;
1903
+ if (width < 0) {
1904
+ action = ACTION_WEST;
1905
+ width = -width;
1906
+ left -= width;
1907
+ }
1908
+ if (aspectRatio) {
1909
+ height = width / aspectRatio;
1910
+ top += (cropBoxData.height - height) / 2;
1911
+ }
1912
+ break;
1913
+ case ACTION_NORTH:
1914
+ if (range.y <= 0 && (top <= minTop || aspectRatio && (left <= minLeft || right >= maxWidth))) {
1915
+ renderable = false;
1916
+ break;
1917
+ }
1918
+ check(ACTION_NORTH);
1919
+ height -= range.y;
1920
+ top += range.y;
1921
+ if (height < 0) {
1922
+ action = ACTION_SOUTH;
1923
+ height = -height;
1924
+ top -= height;
1925
+ }
1926
+ if (aspectRatio) {
1927
+ width = height * aspectRatio;
1928
+ left += (cropBoxData.width - width) / 2;
1929
+ }
1930
+ break;
1931
+ case ACTION_WEST:
1932
+ if (range.x <= 0 && (left <= minLeft || aspectRatio && (top <= minTop || bottom >= maxHeight))) {
1933
+ renderable = false;
1934
+ break;
1935
+ }
1936
+ check(ACTION_WEST);
1937
+ width -= range.x;
1938
+ left += range.x;
1939
+ if (width < 0) {
1940
+ action = ACTION_EAST;
1941
+ width = -width;
1942
+ left -= width;
1943
+ }
1944
+ if (aspectRatio) {
1945
+ height = width / aspectRatio;
1946
+ top += (cropBoxData.height - height) / 2;
1947
+ }
1948
+ break;
1949
+ case ACTION_SOUTH:
1950
+ if (range.y >= 0 && (bottom >= maxHeight || aspectRatio && (left <= minLeft || right >= maxWidth))) {
1951
+ renderable = false;
1952
+ break;
1953
+ }
1954
+ check(ACTION_SOUTH);
1955
+ height += range.y;
1956
+ if (height < 0) {
1957
+ action = ACTION_NORTH;
1958
+ height = -height;
1959
+ top -= height;
1960
+ }
1961
+ if (aspectRatio) {
1962
+ width = height * aspectRatio;
1963
+ left += (cropBoxData.width - width) / 2;
1964
+ }
1965
+ break;
1966
+ case ACTION_NORTH_EAST:
1967
+ if (aspectRatio) {
1968
+ if (range.y <= 0 && (top <= minTop || right >= maxWidth)) {
1969
+ renderable = false;
1970
+ break;
1971
+ }
1972
+ check(ACTION_NORTH);
1973
+ height -= range.y;
1974
+ top += range.y;
1975
+ width = height * aspectRatio;
1976
+ } else {
1977
+ check(ACTION_NORTH);
1978
+ check(ACTION_EAST);
1979
+ if (range.x >= 0) {
1980
+ if (right < maxWidth) {
1981
+ width += range.x;
1982
+ } else if (range.y <= 0 && top <= minTop) {
1983
+ renderable = false;
1984
+ }
1985
+ } else {
1986
+ width += range.x;
1987
+ }
1988
+ if (range.y <= 0) {
1989
+ if (top > minTop) {
1990
+ height -= range.y;
1991
+ top += range.y;
1992
+ }
1993
+ } else {
1994
+ height -= range.y;
1995
+ top += range.y;
1996
+ }
1997
+ }
1998
+ if (width < 0 && height < 0) {
1999
+ action = ACTION_SOUTH_WEST;
2000
+ height = -height;
2001
+ width = -width;
2002
+ top -= height;
2003
+ left -= width;
2004
+ } else if (width < 0) {
2005
+ action = ACTION_NORTH_WEST;
2006
+ width = -width;
2007
+ left -= width;
2008
+ } else if (height < 0) {
2009
+ action = ACTION_SOUTH_EAST;
2010
+ height = -height;
2011
+ top -= height;
2012
+ }
2013
+ break;
2014
+ case ACTION_NORTH_WEST:
2015
+ if (aspectRatio) {
2016
+ if (range.y <= 0 && (top <= minTop || left <= minLeft)) {
2017
+ renderable = false;
2018
+ break;
2019
+ }
2020
+ check(ACTION_NORTH);
2021
+ height -= range.y;
2022
+ top += range.y;
2023
+ width = height * aspectRatio;
2024
+ left += cropBoxData.width - width;
2025
+ } else {
2026
+ check(ACTION_NORTH);
2027
+ check(ACTION_WEST);
2028
+ if (range.x <= 0) {
2029
+ if (left > minLeft) {
2030
+ width -= range.x;
2031
+ left += range.x;
2032
+ } else if (range.y <= 0 && top <= minTop) {
2033
+ renderable = false;
2034
+ }
2035
+ } else {
2036
+ width -= range.x;
2037
+ left += range.x;
2038
+ }
2039
+ if (range.y <= 0) {
2040
+ if (top > minTop) {
2041
+ height -= range.y;
2042
+ top += range.y;
2043
+ }
2044
+ } else {
2045
+ height -= range.y;
2046
+ top += range.y;
2047
+ }
2048
+ }
2049
+ if (width < 0 && height < 0) {
2050
+ action = ACTION_SOUTH_EAST;
2051
+ height = -height;
2052
+ width = -width;
2053
+ top -= height;
2054
+ left -= width;
2055
+ } else if (width < 0) {
2056
+ action = ACTION_NORTH_EAST;
2057
+ width = -width;
2058
+ left -= width;
2059
+ } else if (height < 0) {
2060
+ action = ACTION_SOUTH_WEST;
2061
+ height = -height;
2062
+ top -= height;
2063
+ }
2064
+ break;
2065
+ case ACTION_SOUTH_WEST:
2066
+ if (aspectRatio) {
2067
+ if (range.x <= 0 && (left <= minLeft || bottom >= maxHeight)) {
2068
+ renderable = false;
2069
+ break;
2070
+ }
2071
+ check(ACTION_WEST);
2072
+ width -= range.x;
2073
+ left += range.x;
2074
+ height = width / aspectRatio;
2075
+ } else {
2076
+ check(ACTION_SOUTH);
2077
+ check(ACTION_WEST);
2078
+ if (range.x <= 0) {
2079
+ if (left > minLeft) {
2080
+ width -= range.x;
2081
+ left += range.x;
2082
+ } else if (range.y >= 0 && bottom >= maxHeight) {
2083
+ renderable = false;
2084
+ }
2085
+ } else {
2086
+ width -= range.x;
2087
+ left += range.x;
2088
+ }
2089
+ if (range.y >= 0) {
2090
+ if (bottom < maxHeight) {
2091
+ height += range.y;
2092
+ }
2093
+ } else {
2094
+ height += range.y;
2095
+ }
2096
+ }
2097
+ if (width < 0 && height < 0) {
2098
+ action = ACTION_NORTH_EAST;
2099
+ height = -height;
2100
+ width = -width;
2101
+ top -= height;
2102
+ left -= width;
2103
+ } else if (width < 0) {
2104
+ action = ACTION_SOUTH_EAST;
2105
+ width = -width;
2106
+ left -= width;
2107
+ } else if (height < 0) {
2108
+ action = ACTION_NORTH_WEST;
2109
+ height = -height;
2110
+ top -= height;
2111
+ }
2112
+ break;
2113
+ case ACTION_SOUTH_EAST:
2114
+ if (aspectRatio) {
2115
+ if (range.x >= 0 && (right >= maxWidth || bottom >= maxHeight)) {
2116
+ renderable = false;
2117
+ break;
2118
+ }
2119
+ check(ACTION_EAST);
2120
+ width += range.x;
2121
+ height = width / aspectRatio;
2122
+ } else {
2123
+ check(ACTION_SOUTH);
2124
+ check(ACTION_EAST);
2125
+ if (range.x >= 0) {
2126
+ if (right < maxWidth) {
2127
+ width += range.x;
2128
+ } else if (range.y >= 0 && bottom >= maxHeight) {
2129
+ renderable = false;
2130
+ }
2131
+ } else {
2132
+ width += range.x;
2133
+ }
2134
+ if (range.y >= 0) {
2135
+ if (bottom < maxHeight) {
2136
+ height += range.y;
2137
+ }
2138
+ } else {
2139
+ height += range.y;
2140
+ }
2141
+ }
2142
+ if (width < 0 && height < 0) {
2143
+ action = ACTION_NORTH_WEST;
2144
+ height = -height;
2145
+ width = -width;
2146
+ top -= height;
2147
+ left -= width;
2148
+ } else if (width < 0) {
2149
+ action = ACTION_SOUTH_WEST;
2150
+ width = -width;
2151
+ left -= width;
2152
+ } else if (height < 0) {
2153
+ action = ACTION_NORTH_EAST;
2154
+ height = -height;
2155
+ top -= height;
2156
+ }
2157
+ break;
2158
+
2159
+ // Move canvas
2160
+ case ACTION_MOVE:
2161
+ this.move(range.x, range.y);
2162
+ renderable = false;
2163
+ break;
2164
+
2165
+ // Zoom canvas
2166
+ case ACTION_ZOOM:
2167
+ this.zoom(getMaxZoomRatio(pointers), event);
2168
+ renderable = false;
2169
+ break;
2170
+
2171
+ // Create crop box
2172
+ case ACTION_CROP:
2173
+ if (!range.x || !range.y) {
2174
+ renderable = false;
2175
+ break;
2176
+ }
2177
+ offset = getOffset(this.cropper);
2178
+ left = pointer.startX - offset.left;
2179
+ top = pointer.startY - offset.top;
2180
+ width = cropBoxData.minWidth;
2181
+ height = cropBoxData.minHeight;
2182
+ if (range.x > 0) {
2183
+ action = range.y > 0 ? ACTION_SOUTH_EAST : ACTION_NORTH_EAST;
2184
+ } else if (range.x < 0) {
2185
+ left -= width;
2186
+ action = range.y > 0 ? ACTION_SOUTH_WEST : ACTION_NORTH_WEST;
2187
+ }
2188
+ if (range.y < 0) {
2189
+ top -= height;
2190
+ }
2191
+
2192
+ // Show the crop box if is hidden
2193
+ if (!this.cropped) {
2194
+ removeClass(this.cropBox, CLASS_HIDDEN);
2195
+ this.cropped = true;
2196
+ if (this.limited) {
2197
+ this.limitCropBox(true, true);
2198
+ }
2199
+ }
2200
+ break;
2201
+ }
2202
+ if (renderable) {
2203
+ cropBoxData.width = width;
2204
+ cropBoxData.height = height;
2205
+ cropBoxData.left = left;
2206
+ cropBoxData.top = top;
2207
+ this.action = action;
2208
+ this.renderCropBox();
2209
+ }
2210
+
2211
+ // Override
2212
+ forEach(pointers, function (p) {
2213
+ p.startX = p.endX;
2214
+ p.startY = p.endY;
2215
+ });
2216
+ }
2217
+ };
2218
+
2219
+ var methods = {
2220
+ // Show the crop box manually
2221
+ crop: function crop() {
2222
+ if (this.ready && !this.cropped && !this.disabled) {
2223
+ this.cropped = true;
2224
+ this.limitCropBox(true, true);
2225
+ if (this.options.modal) {
2226
+ addClass(this.dragBox, CLASS_MODAL);
2227
+ }
2228
+ removeClass(this.cropBox, CLASS_HIDDEN);
2229
+ this.setCropBoxData(this.initialCropBoxData);
2230
+ }
2231
+ return this;
2232
+ },
2233
+ // Reset the image and crop box to their initial states
2234
+ reset: function reset() {
2235
+ if (this.ready && !this.disabled) {
2236
+ this.imageData = assign({}, this.initialImageData);
2237
+ this.canvasData = assign({}, this.initialCanvasData);
2238
+ this.cropBoxData = assign({}, this.initialCropBoxData);
2239
+ this.renderCanvas();
2240
+ if (this.cropped) {
2241
+ this.renderCropBox();
2242
+ }
2243
+ }
2244
+ return this;
2245
+ },
2246
+ // Clear the crop box
2247
+ clear: function clear() {
2248
+ if (this.cropped && !this.disabled) {
2249
+ assign(this.cropBoxData, {
2250
+ left: 0,
2251
+ top: 0,
2252
+ width: 0,
2253
+ height: 0
2254
+ });
2255
+ this.cropped = false;
2256
+ this.renderCropBox();
2257
+ this.limitCanvas(true, true);
2258
+
2259
+ // Render canvas after crop box rendered
2260
+ this.renderCanvas();
2261
+ removeClass(this.dragBox, CLASS_MODAL);
2262
+ addClass(this.cropBox, CLASS_HIDDEN);
2263
+ }
2264
+ return this;
2265
+ },
2266
+ /**
2267
+ * Replace the image's src and rebuild the cropper
2268
+ * @param {string} url - The new URL.
2269
+ * @param {boolean} [hasSameSize] - Indicate if the new image has the same size as the old one.
2270
+ * @returns {Cropper} this
2271
+ */
2272
+ replace: function replace(url) {
2273
+ var hasSameSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
2274
+ if (!this.disabled && url) {
2275
+ if (this.isImg) {
2276
+ this.element.src = url;
2277
+ }
2278
+ if (hasSameSize) {
2279
+ this.url = url;
2280
+ this.image.src = url;
2281
+ if (this.ready) {
2282
+ this.viewBoxImage.src = url;
2283
+ forEach(this.previews, function (element) {
2284
+ element.getElementsByTagName('img')[0].src = url;
2285
+ });
2286
+ }
2287
+ } else {
2288
+ if (this.isImg) {
2289
+ this.replaced = true;
2290
+ }
2291
+ this.options.data = null;
2292
+ this.uncreate();
2293
+ this.load(url);
2294
+ }
2295
+ }
2296
+ return this;
2297
+ },
2298
+ // Enable (unfreeze) the cropper
2299
+ enable: function enable() {
2300
+ if (this.ready && this.disabled) {
2301
+ this.disabled = false;
2302
+ removeClass(this.cropper, CLASS_DISABLED);
2303
+ }
2304
+ return this;
2305
+ },
2306
+ // Disable (freeze) the cropper
2307
+ disable: function disable() {
2308
+ if (this.ready && !this.disabled) {
2309
+ this.disabled = true;
2310
+ addClass(this.cropper, CLASS_DISABLED);
2311
+ }
2312
+ return this;
2313
+ },
2314
+ /**
2315
+ * Destroy the cropper and remove the instance from the image
2316
+ * @returns {Cropper} this
2317
+ */
2318
+ destroy: function destroy() {
2319
+ var element = this.element;
2320
+ if (!element[NAMESPACE]) {
2321
+ return this;
2322
+ }
2323
+ element[NAMESPACE] = undefined;
2324
+ if (this.isImg && this.replaced) {
2325
+ element.src = this.originalUrl;
2326
+ }
2327
+ this.uncreate();
2328
+ return this;
2329
+ },
2330
+ /**
2331
+ * Move the canvas with relative offsets
2332
+ * @param {number} offsetX - The relative offset distance on the x-axis.
2333
+ * @param {number} [offsetY=offsetX] - The relative offset distance on the y-axis.
2334
+ * @returns {Cropper} this
2335
+ */
2336
+ move: function move(offsetX) {
2337
+ var offsetY = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : offsetX;
2338
+ var _this$canvasData = this.canvasData,
2339
+ left = _this$canvasData.left,
2340
+ top = _this$canvasData.top;
2341
+ return this.moveTo(isUndefined(offsetX) ? offsetX : left + Number(offsetX), isUndefined(offsetY) ? offsetY : top + Number(offsetY));
2342
+ },
2343
+ /**
2344
+ * Move the canvas to an absolute point
2345
+ * @param {number} x - The x-axis coordinate.
2346
+ * @param {number} [y=x] - The y-axis coordinate.
2347
+ * @returns {Cropper} this
2348
+ */
2349
+ moveTo: function moveTo(x) {
2350
+ var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : x;
2351
+ var canvasData = this.canvasData;
2352
+ var changed = false;
2353
+ x = Number(x);
2354
+ y = Number(y);
2355
+ if (this.ready && !this.disabled && this.options.movable) {
2356
+ if (isNumber(x)) {
2357
+ canvasData.left = x;
2358
+ changed = true;
2359
+ }
2360
+ if (isNumber(y)) {
2361
+ canvasData.top = y;
2362
+ changed = true;
2363
+ }
2364
+ if (changed) {
2365
+ this.renderCanvas(true);
2366
+ }
2367
+ }
2368
+ return this;
2369
+ },
2370
+ /**
2371
+ * Zoom the canvas with a relative ratio
2372
+ * @param {number} ratio - The target ratio.
2373
+ * @param {Event} _originalEvent - The original event if any.
2374
+ * @returns {Cropper} this
2375
+ */
2376
+ zoom: function zoom(ratio, _originalEvent) {
2377
+ var canvasData = this.canvasData;
2378
+ ratio = Number(ratio);
2379
+ if (ratio < 0) {
2380
+ ratio = 1 / (1 - ratio);
2381
+ } else {
2382
+ ratio = 1 + ratio;
2383
+ }
2384
+ return this.zoomTo(canvasData.width * ratio / canvasData.naturalWidth, null, _originalEvent);
2385
+ },
2386
+ /**
2387
+ * Zoom the canvas to an absolute ratio
2388
+ * @param {number} ratio - The target ratio.
2389
+ * @param {Object} pivot - The zoom pivot point coordinate.
2390
+ * @param {Event} _originalEvent - The original event if any.
2391
+ * @returns {Cropper} this
2392
+ */
2393
+ zoomTo: function zoomTo(ratio, pivot, _originalEvent) {
2394
+ var options = this.options,
2395
+ canvasData = this.canvasData;
2396
+ var width = canvasData.width,
2397
+ height = canvasData.height,
2398
+ naturalWidth = canvasData.naturalWidth,
2399
+ naturalHeight = canvasData.naturalHeight;
2400
+ ratio = Number(ratio);
2401
+ if (ratio >= 0 && this.ready && !this.disabled && options.zoomable) {
2402
+ var newWidth = naturalWidth * ratio;
2403
+ var newHeight = naturalHeight * ratio;
2404
+ if (dispatchEvent(this.element, EVENT_ZOOM, {
2405
+ ratio: ratio,
2406
+ oldRatio: width / naturalWidth,
2407
+ originalEvent: _originalEvent
2408
+ }) === false) {
2409
+ return this;
2410
+ }
2411
+ if (_originalEvent) {
2412
+ var pointers = this.pointers;
2413
+ var offset = getOffset(this.cropper);
2414
+ var center = pointers && Object.keys(pointers).length ? getPointersCenter(pointers) : {
2415
+ pageX: _originalEvent.pageX,
2416
+ pageY: _originalEvent.pageY
2417
+ };
2418
+
2419
+ // Zoom from the triggering point of the event
2420
+ canvasData.left -= (newWidth - width) * ((center.pageX - offset.left - canvasData.left) / width);
2421
+ canvasData.top -= (newHeight - height) * ((center.pageY - offset.top - canvasData.top) / height);
2422
+ } else if (isPlainObject(pivot) && isNumber(pivot.x) && isNumber(pivot.y)) {
2423
+ canvasData.left -= (newWidth - width) * ((pivot.x - canvasData.left) / width);
2424
+ canvasData.top -= (newHeight - height) * ((pivot.y - canvasData.top) / height);
2425
+ } else {
2426
+ // Zoom from the center of the canvas
2427
+ canvasData.left -= (newWidth - width) / 2;
2428
+ canvasData.top -= (newHeight - height) / 2;
2429
+ }
2430
+ canvasData.width = newWidth;
2431
+ canvasData.height = newHeight;
2432
+ this.renderCanvas(true);
2433
+ }
2434
+ return this;
2435
+ },
2436
+ /**
2437
+ * Rotate the canvas with a relative degree
2438
+ * @param {number} degree - The rotate degree.
2439
+ * @returns {Cropper} this
2440
+ */
2441
+ rotate: function rotate(degree) {
2442
+ return this.rotateTo((this.imageData.rotate || 0) + Number(degree));
2443
+ },
2444
+ /**
2445
+ * Rotate the canvas to an absolute degree
2446
+ * @param {number} degree - The rotate degree.
2447
+ * @returns {Cropper} this
2448
+ */
2449
+ rotateTo: function rotateTo(degree) {
2450
+ degree = Number(degree);
2451
+ if (isNumber(degree) && this.ready && !this.disabled && this.options.rotatable) {
2452
+ this.imageData.rotate = degree % 360;
2453
+ this.renderCanvas(true, true);
2454
+ }
2455
+ return this;
2456
+ },
2457
+ /**
2458
+ * Scale the image on the x-axis.
2459
+ * @param {number} scaleX - The scale ratio on the x-axis.
2460
+ * @returns {Cropper} this
2461
+ */
2462
+ scaleX: function scaleX(_scaleX) {
2463
+ var scaleY = this.imageData.scaleY;
2464
+ return this.scale(_scaleX, isNumber(scaleY) ? scaleY : 1);
2465
+ },
2466
+ /**
2467
+ * Scale the image on the y-axis.
2468
+ * @param {number} scaleY - The scale ratio on the y-axis.
2469
+ * @returns {Cropper} this
2470
+ */
2471
+ scaleY: function scaleY(_scaleY) {
2472
+ var scaleX = this.imageData.scaleX;
2473
+ return this.scale(isNumber(scaleX) ? scaleX : 1, _scaleY);
2474
+ },
2475
+ /**
2476
+ * Scale the image
2477
+ * @param {number} scaleX - The scale ratio on the x-axis.
2478
+ * @param {number} [scaleY=scaleX] - The scale ratio on the y-axis.
2479
+ * @returns {Cropper} this
2480
+ */
2481
+ scale: function scale(scaleX) {
2482
+ var scaleY = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : scaleX;
2483
+ var imageData = this.imageData;
2484
+ var transformed = false;
2485
+ scaleX = Number(scaleX);
2486
+ scaleY = Number(scaleY);
2487
+ if (this.ready && !this.disabled && this.options.scalable) {
2488
+ if (isNumber(scaleX)) {
2489
+ imageData.scaleX = scaleX;
2490
+ transformed = true;
2491
+ }
2492
+ if (isNumber(scaleY)) {
2493
+ imageData.scaleY = scaleY;
2494
+ transformed = true;
2495
+ }
2496
+ if (transformed) {
2497
+ this.renderCanvas(true, true);
2498
+ }
2499
+ }
2500
+ return this;
2501
+ },
2502
+ /**
2503
+ * Get the cropped area position and size data (base on the original image)
2504
+ * @param {boolean} [rounded=false] - Indicate if round the data values or not.
2505
+ * @returns {Object} The result cropped data.
2506
+ */
2507
+ getData: function getData() {
2508
+ var rounded = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
2509
+ var options = this.options,
2510
+ imageData = this.imageData,
2511
+ canvasData = this.canvasData,
2512
+ cropBoxData = this.cropBoxData;
2513
+ var data;
2514
+ if (this.ready && this.cropped) {
2515
+ data = {
2516
+ x: cropBoxData.left - canvasData.left,
2517
+ y: cropBoxData.top - canvasData.top,
2518
+ width: cropBoxData.width,
2519
+ height: cropBoxData.height
2520
+ };
2521
+ var ratio = imageData.width / imageData.naturalWidth;
2522
+ forEach(data, function (n, i) {
2523
+ data[i] = n / ratio;
2524
+ });
2525
+ if (rounded) {
2526
+ // In case rounding off leads to extra 1px in right or bottom border
2527
+ // we should round the top-left corner and the dimension (#343).
2528
+ var bottom = Math.round(data.y + data.height);
2529
+ var right = Math.round(data.x + data.width);
2530
+ data.x = Math.round(data.x);
2531
+ data.y = Math.round(data.y);
2532
+ data.width = right - data.x;
2533
+ data.height = bottom - data.y;
2534
+ }
2535
+ } else {
2536
+ data = {
2537
+ x: 0,
2538
+ y: 0,
2539
+ width: 0,
2540
+ height: 0
2541
+ };
2542
+ }
2543
+ if (options.rotatable) {
2544
+ data.rotate = imageData.rotate || 0;
2545
+ }
2546
+ if (options.scalable) {
2547
+ data.scaleX = imageData.scaleX || 1;
2548
+ data.scaleY = imageData.scaleY || 1;
2549
+ }
2550
+ return data;
2551
+ },
2552
+ /**
2553
+ * Set the cropped area position and size with new data
2554
+ * @param {Object} data - The new data.
2555
+ * @returns {Cropper} this
2556
+ */
2557
+ setData: function setData(data) {
2558
+ var options = this.options,
2559
+ imageData = this.imageData,
2560
+ canvasData = this.canvasData;
2561
+ var cropBoxData = {};
2562
+ if (this.ready && !this.disabled && isPlainObject(data)) {
2563
+ var transformed = false;
2564
+ if (options.rotatable) {
2565
+ if (isNumber(data.rotate) && data.rotate !== imageData.rotate) {
2566
+ imageData.rotate = data.rotate;
2567
+ transformed = true;
2568
+ }
2569
+ }
2570
+ if (options.scalable) {
2571
+ if (isNumber(data.scaleX) && data.scaleX !== imageData.scaleX) {
2572
+ imageData.scaleX = data.scaleX;
2573
+ transformed = true;
2574
+ }
2575
+ if (isNumber(data.scaleY) && data.scaleY !== imageData.scaleY) {
2576
+ imageData.scaleY = data.scaleY;
2577
+ transformed = true;
2578
+ }
2579
+ }
2580
+ if (transformed) {
2581
+ this.renderCanvas(true, true);
2582
+ }
2583
+ var ratio = imageData.width / imageData.naturalWidth;
2584
+ if (isNumber(data.x)) {
2585
+ cropBoxData.left = data.x * ratio + canvasData.left;
2586
+ }
2587
+ if (isNumber(data.y)) {
2588
+ cropBoxData.top = data.y * ratio + canvasData.top;
2589
+ }
2590
+ if (isNumber(data.width)) {
2591
+ cropBoxData.width = data.width * ratio;
2592
+ }
2593
+ if (isNumber(data.height)) {
2594
+ cropBoxData.height = data.height * ratio;
2595
+ }
2596
+ this.setCropBoxData(cropBoxData);
2597
+ }
2598
+ return this;
2599
+ },
2600
+ /**
2601
+ * Get the container size data.
2602
+ * @returns {Object} The result container data.
2603
+ */
2604
+ getContainerData: function getContainerData() {
2605
+ return this.ready ? assign({}, this.containerData) : {};
2606
+ },
2607
+ /**
2608
+ * Get the image position and size data.
2609
+ * @returns {Object} The result image data.
2610
+ */
2611
+ getImageData: function getImageData() {
2612
+ return this.sized ? assign({}, this.imageData) : {};
2613
+ },
2614
+ /**
2615
+ * Get the canvas position and size data.
2616
+ * @returns {Object} The result canvas data.
2617
+ */
2618
+ getCanvasData: function getCanvasData() {
2619
+ var canvasData = this.canvasData;
2620
+ var data = {};
2621
+ if (this.ready) {
2622
+ forEach(['left', 'top', 'width', 'height', 'naturalWidth', 'naturalHeight'], function (n) {
2623
+ data[n] = canvasData[n];
2624
+ });
2625
+ }
2626
+ return data;
2627
+ },
2628
+ /**
2629
+ * Set the canvas position and size with new data.
2630
+ * @param {Object} data - The new canvas data.
2631
+ * @returns {Cropper} this
2632
+ */
2633
+ setCanvasData: function setCanvasData(data) {
2634
+ var canvasData = this.canvasData;
2635
+ var aspectRatio = canvasData.aspectRatio;
2636
+ if (this.ready && !this.disabled && isPlainObject(data)) {
2637
+ if (isNumber(data.left)) {
2638
+ canvasData.left = data.left;
2639
+ }
2640
+ if (isNumber(data.top)) {
2641
+ canvasData.top = data.top;
2642
+ }
2643
+ if (isNumber(data.width)) {
2644
+ canvasData.width = data.width;
2645
+ canvasData.height = data.width / aspectRatio;
2646
+ } else if (isNumber(data.height)) {
2647
+ canvasData.height = data.height;
2648
+ canvasData.width = data.height * aspectRatio;
2649
+ }
2650
+ this.renderCanvas(true);
2651
+ }
2652
+ return this;
2653
+ },
2654
+ /**
2655
+ * Get the crop box position and size data.
2656
+ * @returns {Object} The result crop box data.
2657
+ */
2658
+ getCropBoxData: function getCropBoxData() {
2659
+ var cropBoxData = this.cropBoxData;
2660
+ var data;
2661
+ if (this.ready && this.cropped) {
2662
+ data = {
2663
+ left: cropBoxData.left,
2664
+ top: cropBoxData.top,
2665
+ width: cropBoxData.width,
2666
+ height: cropBoxData.height
2667
+ };
2668
+ }
2669
+ return data || {};
2670
+ },
2671
+ /**
2672
+ * Set the crop box position and size with new data.
2673
+ * @param {Object} data - The new crop box data.
2674
+ * @returns {Cropper} this
2675
+ */
2676
+ setCropBoxData: function setCropBoxData(data) {
2677
+ var cropBoxData = this.cropBoxData;
2678
+ var aspectRatio = this.options.aspectRatio;
2679
+ var widthChanged;
2680
+ var heightChanged;
2681
+ if (this.ready && this.cropped && !this.disabled && isPlainObject(data)) {
2682
+ if (isNumber(data.left)) {
2683
+ cropBoxData.left = data.left;
2684
+ }
2685
+ if (isNumber(data.top)) {
2686
+ cropBoxData.top = data.top;
2687
+ }
2688
+ if (isNumber(data.width) && data.width !== cropBoxData.width) {
2689
+ widthChanged = true;
2690
+ cropBoxData.width = data.width;
2691
+ }
2692
+ if (isNumber(data.height) && data.height !== cropBoxData.height) {
2693
+ heightChanged = true;
2694
+ cropBoxData.height = data.height;
2695
+ }
2696
+ if (aspectRatio) {
2697
+ if (widthChanged) {
2698
+ cropBoxData.height = cropBoxData.width / aspectRatio;
2699
+ } else if (heightChanged) {
2700
+ cropBoxData.width = cropBoxData.height * aspectRatio;
2701
+ }
2702
+ }
2703
+ this.renderCropBox();
2704
+ }
2705
+ return this;
2706
+ },
2707
+ /**
2708
+ * Get a canvas drawn the cropped image.
2709
+ * @param {Object} [options={}] - The config options.
2710
+ * @returns {HTMLCanvasElement} - The result canvas.
2711
+ */
2712
+ getCroppedCanvas: function getCroppedCanvas() {
2713
+ var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
2714
+ if (!this.ready || !window.HTMLCanvasElement) {
2715
+ return null;
2716
+ }
2717
+ var canvasData = this.canvasData;
2718
+ var source = getSourceCanvas(this.image, this.imageData, canvasData, options);
2719
+
2720
+ // Returns the source canvas if it is not cropped.
2721
+ if (!this.cropped) {
2722
+ return source;
2723
+ }
2724
+ var _this$getData = this.getData(),
2725
+ initialX = _this$getData.x,
2726
+ initialY = _this$getData.y,
2727
+ initialWidth = _this$getData.width,
2728
+ initialHeight = _this$getData.height;
2729
+ var ratio = source.width / Math.floor(canvasData.naturalWidth);
2730
+ if (ratio !== 1) {
2731
+ initialX *= ratio;
2732
+ initialY *= ratio;
2733
+ initialWidth *= ratio;
2734
+ initialHeight *= ratio;
2735
+ }
2736
+ var aspectRatio = initialWidth / initialHeight;
2737
+ var maxSizes = getAdjustedSizes({
2738
+ aspectRatio: aspectRatio,
2739
+ width: options.maxWidth || Infinity,
2740
+ height: options.maxHeight || Infinity
2741
+ });
2742
+ var minSizes = getAdjustedSizes({
2743
+ aspectRatio: aspectRatio,
2744
+ width: options.minWidth || 0,
2745
+ height: options.minHeight || 0
2746
+ }, 'cover');
2747
+ var _getAdjustedSizes = getAdjustedSizes({
2748
+ aspectRatio: aspectRatio,
2749
+ width: options.width || (ratio !== 1 ? source.width : initialWidth),
2750
+ height: options.height || (ratio !== 1 ? source.height : initialHeight)
2751
+ }),
2752
+ width = _getAdjustedSizes.width,
2753
+ height = _getAdjustedSizes.height;
2754
+ width = Math.min(maxSizes.width, Math.max(minSizes.width, width));
2755
+ height = Math.min(maxSizes.height, Math.max(minSizes.height, height));
2756
+ var canvas = document.createElement('canvas');
2757
+ var context = canvas.getContext('2d');
2758
+ canvas.width = normalizeDecimalNumber(width);
2759
+ canvas.height = normalizeDecimalNumber(height);
2760
+ context.fillStyle = options.fillColor || 'transparent';
2761
+ context.fillRect(0, 0, width, height);
2762
+ var _options$imageSmoothi = options.imageSmoothingEnabled,
2763
+ imageSmoothingEnabled = _options$imageSmoothi === void 0 ? true : _options$imageSmoothi,
2764
+ imageSmoothingQuality = options.imageSmoothingQuality;
2765
+ context.imageSmoothingEnabled = imageSmoothingEnabled;
2766
+ if (imageSmoothingQuality) {
2767
+ context.imageSmoothingQuality = imageSmoothingQuality;
2768
+ }
2769
+
2770
+ // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D.drawImage
2771
+ var sourceWidth = source.width;
2772
+ var sourceHeight = source.height;
2773
+
2774
+ // Source canvas parameters
2775
+ var srcX = initialX;
2776
+ var srcY = initialY;
2777
+ var srcWidth;
2778
+ var srcHeight;
2779
+
2780
+ // Destination canvas parameters
2781
+ var dstX;
2782
+ var dstY;
2783
+ var dstWidth;
2784
+ var dstHeight;
2785
+ if (srcX <= -initialWidth || srcX > sourceWidth) {
2786
+ srcX = 0;
2787
+ srcWidth = 0;
2788
+ dstX = 0;
2789
+ dstWidth = 0;
2790
+ } else if (srcX <= 0) {
2791
+ dstX = -srcX;
2792
+ srcX = 0;
2793
+ srcWidth = Math.min(sourceWidth, initialWidth + srcX);
2794
+ dstWidth = srcWidth;
2795
+ } else if (srcX <= sourceWidth) {
2796
+ dstX = 0;
2797
+ srcWidth = Math.min(initialWidth, sourceWidth - srcX);
2798
+ dstWidth = srcWidth;
2799
+ }
2800
+ if (srcWidth <= 0 || srcY <= -initialHeight || srcY > sourceHeight) {
2801
+ srcY = 0;
2802
+ srcHeight = 0;
2803
+ dstY = 0;
2804
+ dstHeight = 0;
2805
+ } else if (srcY <= 0) {
2806
+ dstY = -srcY;
2807
+ srcY = 0;
2808
+ srcHeight = Math.min(sourceHeight, initialHeight + srcY);
2809
+ dstHeight = srcHeight;
2810
+ } else if (srcY <= sourceHeight) {
2811
+ dstY = 0;
2812
+ srcHeight = Math.min(initialHeight, sourceHeight - srcY);
2813
+ dstHeight = srcHeight;
2814
+ }
2815
+ var params = [srcX, srcY, srcWidth, srcHeight];
2816
+
2817
+ // Avoid "IndexSizeError"
2818
+ if (dstWidth > 0 && dstHeight > 0) {
2819
+ var scale = width / initialWidth;
2820
+ params.push(dstX * scale, dstY * scale, dstWidth * scale, dstHeight * scale);
2821
+ }
2822
+
2823
+ // All the numerical parameters should be integer for `drawImage`
2824
+ // https://github.com/fengyuanchen/cropper/issues/476
2825
+ context.drawImage.apply(context, [source].concat(_toConsumableArray(params.map(function (param) {
2826
+ return Math.floor(normalizeDecimalNumber(param));
2827
+ }))));
2828
+ return canvas;
2829
+ },
2830
+ /**
2831
+ * Change the aspect ratio of the crop box.
2832
+ * @param {number} aspectRatio - The new aspect ratio.
2833
+ * @returns {Cropper} this
2834
+ */
2835
+ setAspectRatio: function setAspectRatio(aspectRatio) {
2836
+ var options = this.options;
2837
+ if (!this.disabled && !isUndefined(aspectRatio)) {
2838
+ // 0 -> NaN
2839
+ options.aspectRatio = Math.max(0, aspectRatio) || NaN;
2840
+ if (this.ready) {
2841
+ this.initCropBox();
2842
+ if (this.cropped) {
2843
+ this.renderCropBox();
2844
+ }
2845
+ }
2846
+ }
2847
+ return this;
2848
+ },
2849
+ /**
2850
+ * Change the drag mode.
2851
+ * @param {string} mode - The new drag mode.
2852
+ * @returns {Cropper} this
2853
+ */
2854
+ setDragMode: function setDragMode(mode) {
2855
+ var options = this.options,
2856
+ dragBox = this.dragBox,
2857
+ face = this.face;
2858
+ if (this.ready && !this.disabled) {
2859
+ var croppable = mode === DRAG_MODE_CROP;
2860
+ var movable = options.movable && mode === DRAG_MODE_MOVE;
2861
+ mode = croppable || movable ? mode : DRAG_MODE_NONE;
2862
+ options.dragMode = mode;
2863
+ setData(dragBox, DATA_ACTION, mode);
2864
+ toggleClass(dragBox, CLASS_CROP, croppable);
2865
+ toggleClass(dragBox, CLASS_MOVE, movable);
2866
+ if (!options.cropBoxMovable) {
2867
+ // Sync drag mode to crop box when it is not movable
2868
+ setData(face, DATA_ACTION, mode);
2869
+ toggleClass(face, CLASS_CROP, croppable);
2870
+ toggleClass(face, CLASS_MOVE, movable);
2871
+ }
2872
+ }
2873
+ return this;
2874
+ }
2875
+ };
2876
+
2877
+ var AnotherCropper = WINDOW.Cropper;
2878
+ var Cropper = /*#__PURE__*/function () {
2879
+ /**
2880
+ * Create a new Cropper.
2881
+ * @param {Element} element - The target element for cropping.
2882
+ * @param {Object} [options={}] - The configuration options.
2883
+ */
2884
+ function Cropper(element) {
2885
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
2886
+ _classCallCheck(this, Cropper);
2887
+ if (!element || !REGEXP_TAG_NAME.test(element.tagName)) {
2888
+ throw new Error('The first argument is required and must be an <img> or <canvas> element.');
2889
+ }
2890
+ this.element = element;
2891
+ this.options = assign({}, DEFAULTS, isPlainObject(options) && options);
2892
+ this.cropped = false;
2893
+ this.disabled = false;
2894
+ this.pointers = {};
2895
+ this.ready = false;
2896
+ this.reloading = false;
2897
+ this.replaced = false;
2898
+ this.sized = false;
2899
+ this.sizing = false;
2900
+ this.init();
2901
+ }
2902
+ _createClass(Cropper, [{
2903
+ key: "init",
2904
+ value: function init() {
2905
+ var element = this.element;
2906
+ var tagName = element.tagName.toLowerCase();
2907
+ var url;
2908
+ if (element[NAMESPACE]) {
2909
+ return;
2910
+ }
2911
+ element[NAMESPACE] = this;
2912
+ if (tagName === 'img') {
2913
+ this.isImg = true;
2914
+
2915
+ // e.g.: "img/picture.jpg"
2916
+ url = element.getAttribute('src') || '';
2917
+ this.originalUrl = url;
2918
+
2919
+ // Stop when it's a blank image
2920
+ if (!url) {
2921
+ return;
2922
+ }
2923
+
2924
+ // e.g.: "https://example.com/img/picture.jpg"
2925
+ url = element.src;
2926
+ } else if (tagName === 'canvas' && window.HTMLCanvasElement) {
2927
+ url = element.toDataURL();
2928
+ }
2929
+ this.load(url);
2930
+ }
2931
+ }, {
2932
+ key: "load",
2933
+ value: function load(url) {
2934
+ var _this = this;
2935
+ if (!url) {
2936
+ return;
2937
+ }
2938
+ this.url = url;
2939
+ this.imageData = {};
2940
+ var element = this.element,
2941
+ options = this.options;
2942
+ if (!options.rotatable && !options.scalable) {
2943
+ options.checkOrientation = false;
2944
+ }
2945
+
2946
+ // Only IE10+ supports Typed Arrays
2947
+ if (!options.checkOrientation || !window.ArrayBuffer) {
2948
+ this.clone();
2949
+ return;
2950
+ }
2951
+
2952
+ // Detect the mime type of the image directly if it is a Data URL
2953
+ if (REGEXP_DATA_URL.test(url)) {
2954
+ // Read ArrayBuffer from Data URL of JPEG images directly for better performance
2955
+ if (REGEXP_DATA_URL_JPEG.test(url)) {
2956
+ this.read(dataURLToArrayBuffer(url));
2957
+ } else {
2958
+ // Only a JPEG image may contains Exif Orientation information,
2959
+ // the rest types of Data URLs are not necessary to check orientation at all.
2960
+ this.clone();
2961
+ }
2962
+ return;
2963
+ }
2964
+
2965
+ // 1. Detect the mime type of the image by a XMLHttpRequest.
2966
+ // 2. Load the image as ArrayBuffer for reading orientation if its a JPEG image.
2967
+ var xhr = new XMLHttpRequest();
2968
+ var clone = this.clone.bind(this);
2969
+ this.reloading = true;
2970
+ this.xhr = xhr;
2971
+
2972
+ // 1. Cross origin requests are only supported for protocol schemes:
2973
+ // http, https, data, chrome, chrome-extension.
2974
+ // 2. Access to XMLHttpRequest from a Data URL will be blocked by CORS policy
2975
+ // in some browsers as IE11 and Safari.
2976
+ xhr.onabort = clone;
2977
+ xhr.onerror = clone;
2978
+ xhr.ontimeout = clone;
2979
+ xhr.onprogress = function () {
2980
+ // Abort the request directly if it not a JPEG image for better performance
2981
+ if (xhr.getResponseHeader('content-type') !== MIME_TYPE_JPEG) {
2982
+ xhr.abort();
2983
+ }
2984
+ };
2985
+ xhr.onload = function () {
2986
+ _this.read(xhr.response);
2987
+ };
2988
+ xhr.onloadend = function () {
2989
+ _this.reloading = false;
2990
+ _this.xhr = null;
2991
+ };
2992
+
2993
+ // Bust cache when there is a "crossOrigin" property to avoid browser cache error
2994
+ if (options.checkCrossOrigin && isCrossOriginURL(url) && element.crossOrigin) {
2995
+ url = addTimestamp(url);
2996
+ }
2997
+
2998
+ // The third parameter is required for avoiding side-effect (#682)
2999
+ xhr.open('GET', url, true);
3000
+ xhr.responseType = 'arraybuffer';
3001
+ xhr.withCredentials = element.crossOrigin === 'use-credentials';
3002
+ xhr.send();
3003
+ }
3004
+ }, {
3005
+ key: "read",
3006
+ value: function read(arrayBuffer) {
3007
+ var options = this.options,
3008
+ imageData = this.imageData;
3009
+
3010
+ // Reset the orientation value to its default value 1
3011
+ // as some iOS browsers will render image with its orientation
3012
+ var orientation = resetAndGetOrientation(arrayBuffer);
3013
+ var rotate = 0;
3014
+ var scaleX = 1;
3015
+ var scaleY = 1;
3016
+ if (orientation > 1) {
3017
+ // Generate a new URL which has the default orientation value
3018
+ this.url = arrayBufferToDataURL(arrayBuffer, MIME_TYPE_JPEG);
3019
+ var _parseOrientation = parseOrientation(orientation);
3020
+ rotate = _parseOrientation.rotate;
3021
+ scaleX = _parseOrientation.scaleX;
3022
+ scaleY = _parseOrientation.scaleY;
3023
+ }
3024
+ if (options.rotatable) {
3025
+ imageData.rotate = rotate;
3026
+ }
3027
+ if (options.scalable) {
3028
+ imageData.scaleX = scaleX;
3029
+ imageData.scaleY = scaleY;
3030
+ }
3031
+ this.clone();
3032
+ }
3033
+ }, {
3034
+ key: "clone",
3035
+ value: function clone() {
3036
+ var element = this.element,
3037
+ url = this.url;
3038
+ var crossOrigin = element.crossOrigin;
3039
+ var crossOriginUrl = url;
3040
+ if (this.options.checkCrossOrigin && isCrossOriginURL(url)) {
3041
+ if (!crossOrigin) {
3042
+ crossOrigin = 'anonymous';
3043
+ }
3044
+
3045
+ // Bust cache when there is not a "crossOrigin" property (#519)
3046
+ crossOriginUrl = addTimestamp(url);
3047
+ }
3048
+ this.crossOrigin = crossOrigin;
3049
+ this.crossOriginUrl = crossOriginUrl;
3050
+ var image = document.createElement('img');
3051
+ if (crossOrigin) {
3052
+ image.crossOrigin = crossOrigin;
3053
+ }
3054
+ image.src = crossOriginUrl || url;
3055
+ image.alt = element.alt || 'The image to crop';
3056
+ this.image = image;
3057
+ image.onload = this.start.bind(this);
3058
+ image.onerror = this.stop.bind(this);
3059
+ addClass(image, CLASS_HIDE);
3060
+ element.parentNode.insertBefore(image, element.nextSibling);
3061
+ }
3062
+ }, {
3063
+ key: "start",
3064
+ value: function start() {
3065
+ var _this2 = this;
3066
+ var image = this.image;
3067
+ image.onload = null;
3068
+ image.onerror = null;
3069
+ this.sizing = true;
3070
+
3071
+ // Match all browsers that use WebKit as the layout engine in iOS devices,
3072
+ // such as Safari for iOS, Chrome for iOS, and in-app browsers.
3073
+ var isIOSWebKit = WINDOW.navigator && /(?:iPad|iPhone|iPod).*?AppleWebKit/i.test(WINDOW.navigator.userAgent);
3074
+ var done = function done(naturalWidth, naturalHeight) {
3075
+ assign(_this2.imageData, {
3076
+ naturalWidth: naturalWidth,
3077
+ naturalHeight: naturalHeight,
3078
+ aspectRatio: naturalWidth / naturalHeight
3079
+ });
3080
+ _this2.initialImageData = assign({}, _this2.imageData);
3081
+ _this2.sizing = false;
3082
+ _this2.sized = true;
3083
+ _this2.build();
3084
+ };
3085
+
3086
+ // Most modern browsers (excepts iOS WebKit)
3087
+ if (image.naturalWidth && !isIOSWebKit) {
3088
+ done(image.naturalWidth, image.naturalHeight);
3089
+ return;
3090
+ }
3091
+ var sizingImage = document.createElement('img');
3092
+ var body = document.body || document.documentElement;
3093
+ this.sizingImage = sizingImage;
3094
+ sizingImage.onload = function () {
3095
+ done(sizingImage.width, sizingImage.height);
3096
+ if (!isIOSWebKit) {
3097
+ body.removeChild(sizingImage);
3098
+ }
3099
+ };
3100
+ sizingImage.src = image.src;
3101
+
3102
+ // iOS WebKit will convert the image automatically
3103
+ // with its orientation once append it into DOM (#279)
3104
+ if (!isIOSWebKit) {
3105
+ sizingImage.style.cssText = 'left:0;' + 'max-height:none!important;' + 'max-width:none!important;' + 'min-height:0!important;' + 'min-width:0!important;' + 'opacity:0;' + 'position:absolute;' + 'top:0;' + 'z-index:-1;';
3106
+ body.appendChild(sizingImage);
3107
+ }
3108
+ }
3109
+ }, {
3110
+ key: "stop",
3111
+ value: function stop() {
3112
+ var image = this.image;
3113
+ image.onload = null;
3114
+ image.onerror = null;
3115
+ image.parentNode.removeChild(image);
3116
+ this.image = null;
3117
+ }
3118
+ }, {
3119
+ key: "build",
3120
+ value: function build() {
3121
+ if (!this.sized || this.ready) {
3122
+ return;
3123
+ }
3124
+ var element = this.element,
3125
+ options = this.options,
3126
+ image = this.image;
3127
+
3128
+ // Create cropper elements
3129
+ var container = element.parentNode;
3130
+ var template = document.createElement('div');
3131
+ template.innerHTML = TEMPLATE;
3132
+ var cropper = template.querySelector(".".concat(NAMESPACE, "-container"));
3133
+ var canvas = cropper.querySelector(".".concat(NAMESPACE, "-canvas"));
3134
+ var dragBox = cropper.querySelector(".".concat(NAMESPACE, "-drag-box"));
3135
+ var cropBox = cropper.querySelector(".".concat(NAMESPACE, "-crop-box"));
3136
+ var face = cropBox.querySelector(".".concat(NAMESPACE, "-face"));
3137
+ this.container = container;
3138
+ this.cropper = cropper;
3139
+ this.canvas = canvas;
3140
+ this.dragBox = dragBox;
3141
+ this.cropBox = cropBox;
3142
+ this.viewBox = cropper.querySelector(".".concat(NAMESPACE, "-view-box"));
3143
+ this.face = face;
3144
+ canvas.appendChild(image);
3145
+
3146
+ // Hide the original image
3147
+ addClass(element, CLASS_HIDDEN);
3148
+
3149
+ // Inserts the cropper after to the current image
3150
+ container.insertBefore(cropper, element.nextSibling);
3151
+
3152
+ // Show the hidden image
3153
+ removeClass(image, CLASS_HIDE);
3154
+ this.initPreview();
3155
+ this.bind();
3156
+ options.initialAspectRatio = Math.max(0, options.initialAspectRatio) || NaN;
3157
+ options.aspectRatio = Math.max(0, options.aspectRatio) || NaN;
3158
+ options.viewMode = Math.max(0, Math.min(3, Math.round(options.viewMode))) || 0;
3159
+ addClass(cropBox, CLASS_HIDDEN);
3160
+ if (!options.guides) {
3161
+ addClass(cropBox.getElementsByClassName("".concat(NAMESPACE, "-dashed")), CLASS_HIDDEN);
3162
+ }
3163
+ if (!options.center) {
3164
+ addClass(cropBox.getElementsByClassName("".concat(NAMESPACE, "-center")), CLASS_HIDDEN);
3165
+ }
3166
+ if (options.background) {
3167
+ addClass(cropper, "".concat(NAMESPACE, "-bg"));
3168
+ }
3169
+ if (!options.highlight) {
3170
+ addClass(face, CLASS_INVISIBLE);
3171
+ }
3172
+ if (options.cropBoxMovable) {
3173
+ addClass(face, CLASS_MOVE);
3174
+ setData(face, DATA_ACTION, ACTION_ALL);
3175
+ }
3176
+ if (!options.cropBoxResizable) {
3177
+ addClass(cropBox.getElementsByClassName("".concat(NAMESPACE, "-line")), CLASS_HIDDEN);
3178
+ addClass(cropBox.getElementsByClassName("".concat(NAMESPACE, "-point")), CLASS_HIDDEN);
3179
+ }
3180
+ this.render();
3181
+ this.ready = true;
3182
+ this.setDragMode(options.dragMode);
3183
+ if (options.autoCrop) {
3184
+ this.crop();
3185
+ }
3186
+ this.setData(options.data);
3187
+ if (isFunction(options.ready)) {
3188
+ addListener(element, EVENT_READY, options.ready, {
3189
+ once: true
3190
+ });
3191
+ }
3192
+ dispatchEvent(element, EVENT_READY);
3193
+ }
3194
+ }, {
3195
+ key: "unbuild",
3196
+ value: function unbuild() {
3197
+ if (!this.ready) {
3198
+ return;
3199
+ }
3200
+ this.ready = false;
3201
+ this.unbind();
3202
+ this.resetPreview();
3203
+ var parentNode = this.cropper.parentNode;
3204
+ if (parentNode) {
3205
+ parentNode.removeChild(this.cropper);
3206
+ }
3207
+ removeClass(this.element, CLASS_HIDDEN);
3208
+ }
3209
+ }, {
3210
+ key: "uncreate",
3211
+ value: function uncreate() {
3212
+ if (this.ready) {
3213
+ this.unbuild();
3214
+ this.ready = false;
3215
+ this.cropped = false;
3216
+ } else if (this.sizing) {
3217
+ this.sizingImage.onload = null;
3218
+ this.sizing = false;
3219
+ this.sized = false;
3220
+ } else if (this.reloading) {
3221
+ this.xhr.onabort = null;
3222
+ this.xhr.abort();
3223
+ } else if (this.image) {
3224
+ this.stop();
3225
+ }
3226
+ }
3227
+
3228
+ /**
3229
+ * Get the no conflict cropper class.
3230
+ * @returns {Cropper} The cropper class.
3231
+ */
3232
+ }], [{
3233
+ key: "noConflict",
3234
+ value: function noConflict() {
3235
+ window.Cropper = AnotherCropper;
3236
+ return Cropper;
3237
+ }
3238
+
3239
+ /**
3240
+ * Change the default options.
3241
+ * @param {Object} options - The new default options.
3242
+ */
3243
+ }, {
3244
+ key: "setDefaults",
3245
+ value: function setDefaults(options) {
3246
+ assign(DEFAULTS, isPlainObject(options) && options);
3247
+ }
3248
+ }]);
3249
+ return Cropper;
3250
+ }();
3251
+ assign(Cropper.prototype, render, preview, events, handlers, change, methods);
3252
+
3253
+ module.exports = Cropper;