-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage-cropper.js
157 lines (137 loc) · 5.05 KB
/
image-cropper.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
var dom = require('dom-events')
, makeDraggable = require('./lib/draggable')
, ensureElement = require('./lib/ensure-element')
, getCropData = require('./lib/get-crop-data')
, moveImage = require('./lib/move-image')
, navigation = require('./lib/navigation')
, setImage = require('./lib/set-image')
, setupElements = require('./lib/setup-elements')
, setupResultImage = require('./lib/result-image')
, init = function (containerElm, options, callback) {
var navigationElm = ensureElement({ container: containerElm, className: 'navigation' })
, croppedImage = new Image()
, overlayImage = options.overlay ? new Image() : undefined
, images = options.overlay ? [ croppedImage, overlayImage ] : [ croppedImage ]
, width = options.width
, height = options.height
, maxZoom = options.maxZoom || 3
, draggable = makeDraggable((overlayImage || croppedImage), function (event) {
images.forEach(function (image) {
moveImage(
image
, event.movementX
, event.movementY
, options.width
, options.height
)
})
})
, resultImage
, enable = function (options) {
var callback = options.callback || function () {}
, sliderHandle = containerElm.querySelector('.navigation .slider .handle')
, originalState = {
imageWidth: croppedImage.width
, imageHeight: croppedImage.height
, imageTop: croppedImage.style.top
, imageLeft: croppedImage.style.left
, sliderHandleLeft: sliderHandle.style.left
}
containerElm.classList.add('enabled')
draggable.enable()
// disable resultImage when cropper is enabled
if (resultImage) {
resultImage.disable()
}
options.navigation.enable(function (err, data) {
if (err) return callback(err)
if (data && !data.save) {
images.forEach(function (image) {
image.width = originalState.imageWidth
image.height = originalState.imageHeight
image.style.top = originalState.imageTop
image.style.left = originalState.imageLeft
})
sliderHandle.style.left = originalState.sliderHandleLeft
}
disable()
callback(null, data)
})
}
, disable = function () {
// enable resultImage when cropper is disabled
if (resultImage) {
resultImage.enable()
}
draggable.disable()
containerElm.classList.remove('enabled')
}
if (options.resultSrc) {
resultImage = setupResultImage({
container: containerElm
, src: options.resultSrc
, width: options.width
, height: options.height
})
}
setupElements({
containerElm: containerElm
, croppedImage: croppedImage
, overlayImage: overlayImage
, navigation: navigationElm
, width: width
, height: height
})
var setImageOptions = {
images: images
, src: options.src
, width: width
, height: height
, cropData: options.cropData
}
setImage(setImageOptions, function (err) {
if (err) return callback(err)
// reset opacity, e.g. show the image
images.forEach(function (image) { image.style.opacity = '' })
var nav = navigation({
container: navigationElm
, maxZoom: maxZoom
, images: images
, width: width
, height: height
, sliderWidth: options.sliderWidth
})
, results = {
enable: function (callback) {
enable({ navigation: nav, callback: callback })
}
, getCropData: function () {
return getCropData({ image: croppedImage, container: containerElm })
}
, changeImage: function (options, callback) {
var sliderHandle = containerElm.querySelector('.navigation .slider .handle')
sliderHandle.style.left = '0px'
setImage({
images: images
, src: options.src
, width: width
, height: height
}
, callback
)
}
, setResultImage: function (options) {
resultImage = setupResultImage({
src: options.src
, container: containerElm
, width: width
, height: height
})
resultImage.enable()
}
}
disable()
callback(null, results)
})
}
module.exports = init