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