-
Notifications
You must be signed in to change notification settings - Fork 225
/
VueCropper.js
374 lines (340 loc) · 8.46 KB
/
VueCropper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { h } from 'vue'
import Cropper from 'cropperjs'
const previewPropType = typeof window === 'undefined'
? [String, Array]
: [String, Array, Element, NodeList]
export default {
render() {
const crossorigin = this.crossorigin || undefined;
return h('div', { style: this.containerStyle }, [
h('img', {
ref: 'img',
src: this.src,
alt: this.alt || 'image',
style: [
{ 'max-width': '100%' },
this.imgStyle
],
crossorigin,
})
])
},
props: {
// Library props
containerStyle: Object,
src: {
type: String,
default: ''
},
alt: String,
imgStyle: Object,
// CropperJS props
viewMode: Number,
dragMode: String,
initialAspectRatio: Number,
aspectRatio: Number,
data: Object,
preview: previewPropType,
responsive: {
type: Boolean,
default: true
},
restore: {
type: Boolean,
default: true
},
checkCrossOrigin: {
type: Boolean,
default: true
},
checkOrientation: {
type: Boolean,
default: true
},
crossorigin: {
type: String,
},
modal: {
type: Boolean,
default: true
},
guides: {
type: Boolean,
default: true
},
center: {
type: Boolean,
default: true
},
highlight: {
type: Boolean,
default: true
},
background: {
type: Boolean,
default: true
},
autoCrop: {
type: Boolean,
default: true
},
autoCropArea: Number,
movable: {
type: Boolean,
default: true
},
rotatable: {
type: Boolean,
default: true
},
scalable: {
type: Boolean,
default: true
},
zoomable: {
type: Boolean,
default: true
},
zoomOnTouch: {
type: Boolean,
default: true
},
zoomOnWheel: {
type: Boolean,
default: true
},
wheelZoomRatio: Number,
cropBoxMovable: {
type: Boolean,
default: true
},
cropBoxResizable: {
type: Boolean,
default: true
},
toggleDragModeOnDblclick: {
type: Boolean,
default: true
},
// Size limitation
minCanvasWidth: Number,
minCanvasHeight: Number,
minCropBoxWidth: Number,
minCropBoxHeight: Number,
minContainerWidth: Number,
minContainerHeight: Number,
// callbacks
ready: Function,
cropstart: Function,
cropmove: Function,
cropend: Function,
crop: Function,
zoom: Function
},
mounted() {
const { containerStyle, src, alt, imgStyle, ...data } = this.$options.props
const props = {}
for (const key in data) {
if (this[key] !== undefined) {
props[key] = this[key]
}
}
this.cropper = new Cropper(this.$refs.img, props)
},
methods: {
// Reset the image and crop box to their initial states
reset() {
return this.cropper.reset()
},
// Clear the crop box
clear() {
return this.cropper.clear()
},
// Init crop box manually
initCrop() {
return this.cropper.crop()
},
/**
* Replace the image's src and rebuild the cropper
* @param {string} url - The new URL.
* @param {boolean} [onlyColorChanged] - Indicate if the new image only changed color.
* @returns {Object} this
*/
replace(url, onlyColorChanged = false) {
return this.cropper.replace(url, onlyColorChanged)
},
// Enable (unfreeze) the cropper
enable() {
return this.cropper.enable()
},
// Disable (freeze) the cropper
disable() {
return this.cropper.disable()
},
// Destroy the cropper and remove the instance from the image
destroy() {
return this.cropper.destroy()
},
/**
* Move the canvas with relative offsets
* @param {number} offsetX - The relative offset distance on the x-axis.
* @param {number} offsetY - The relative offset distance on the y-axis.
* @returns {Object} this
*/
move(offsetX, offsetY) {
return this.cropper.move(offsetX, offsetY)
},
/**
* Move the canvas to an absolute point
* @param {number} x - The x-axis coordinate.
* @param {number} [y=x] - The y-axis coordinate.
* @returns {Object} this
*/
moveTo(x, y = x) {
return this.cropper.moveTo(x, y)
},
/**
* Zoom the canvas with a relative ratio
* @param {number} ratio - The target ratio.
* @param {Event} _originalEvent - The original event if any.
* @returns {Object} this
*/
relativeZoom(ratio, _originalEvent) {
return this.cropper.zoom(ratio, _originalEvent)
},
/**
* Zoom the canvas to an absolute ratio
* @param {number} ratio - The target ratio.
* @param {Event} _originalEvent - The original event if any.
* @returns {Object} this
*/
zoomTo(ratio, _originalEvent) {
return this.cropper.zoomTo(ratio, _originalEvent)
},
/**
* Rotate the canvas with a relative degree
* @param {number} degree - The rotate degree.
* @returns {Object} this
*/
rotate(degree) {
return this.cropper.rotate(degree)
},
/**
* Rotate the canvas to an absolute degree
* @param {number} degree - The rotate degree.
* @returns {Object} this
*/
rotateTo(degree) {
return this.cropper.rotateTo(degree)
},
/**
* Scale the image on the x-axis.
* @param {number} scaleX - The scale ratio on the x-axis.
* @returns {Object} this
*/
scaleX(scaleX) {
return this.cropper.scaleX(scaleX)
},
/**
* Scale the image on the y-axis.
* @param {number} scaleY - The scale ratio on the y-axis.
* @returns {Object} this
*/
scaleY(scaleY) {
return this.cropper.scaleY(scaleY)
},
/**
* Scale the image
* @param {number} scaleX - The scale ratio on the x-axis.
* @param {number} [scaleY=scaleX] - The scale ratio on the y-axis.
* @returns {Object} this
*/
scale(scaleX, scaleY = scaleX) {
return this.cropper.scale(scaleX, scaleY)
},
/**
* Get the cropped area position and size data (base on the original image)
* @param {boolean} [rounded=false] - Indicate if round the data values or not.
* @returns {Object} The result cropped data.
*/
getData(rounded = false) {
return this.cropper.getData(rounded)
},
/**
* Set the cropped area position and size with new data
* @param {Object} data - The new data.
* @returns {Object} this
*/
setData(data) {
return this.cropper.setData(data)
},
/**
* Get the container size data.
* @returns {Object} The result container data.
*/
getContainerData() {
return this.cropper.getContainerData()
},
/**
* Get the image position and size data.
* @returns {Object} The result image data.
*/
getImageData() {
return this.cropper.getImageData()
},
/**
* Get the canvas position and size data.
* @returns {Object} The result canvas data.
*/
getCanvasData() {
return this.cropper.getCanvasData()
},
/**
* Set the canvas position and size with new data.
* @param {Object} data - The new canvas data.
* @returns {Object} this
*/
setCanvasData(data) {
return this.cropper.setCanvasData(data)
},
/**
* Get the crop box position and size data.
* @returns {Object} The result crop box data.
*/
getCropBoxData() {
return this.cropper.getCropBoxData()
},
/**
* Set the crop box position and size with new data.
* @param {Object} data - The new crop box data.
* @returns {Object} this
*/
setCropBoxData(data) {
return this.cropper.setCropBoxData(data)
},
/**
* Get a canvas drawn the cropped image.
* @param {Object} [options={}] - The config options.
* @returns {HTMLCanvasElement} - The result canvas.
*/
getCroppedCanvas(options = {}) {
return this.cropper.getCroppedCanvas(options)
},
/**
* Change the aspect ratio of the crop box.
* @param {number} aspectRatio - The new aspect ratio.
* @returns {Object} this
*/
setAspectRatio(aspectRatio) {
return this.cropper.setAspectRatio(aspectRatio)
},
/**
* Change the drag mode.
* @param {string} mode - The new drag mode.
* @returns {Object} this
*/
setDragMode(mode) {
return this.cropper.setDragMode(mode)
}
}
}