-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
255 lines (187 loc) · 7.15 KB
/
helpers.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
export const getPositionAfterDrag = (move , touch , croper, image) => {
let x = move.x
let y = move.y
var iL = image.current.offsetLeft
var iR = -((image.current.width - croper.current.offsetWidth) + image.current.offsetLeft + 2)
var iT = image.current.offsetTop
var iB = -((image.current.height - croper.current.offsetHeight) + image.current.offsetTop + 2)
var iML = iL
var iMR = iR
var iMT = iT
var iMB = iB
var iW = image.current.offsetWidth
var iH = image.current.offsetHeight
var cW = croper.current.offsetWidth
var cH = croper.current.offsetHeight
// console.log(iL)
// console.log(image)
// console.log(image.current.offsetTop)
// console.log(image.current.style.top)
// console.log(image.current.offsetLeft)
// console.log(image.current.style.left)
// console.log((image.current.width - image.current.offsetParent.offsetWidth) + image.current.offsetLeft + 2)
// console.log(image.current.style.right)
// console.log((image.current.height - image.current.offsetParent.offsetHeight) + image.current.offsetTop + 2)
// console.log(image.current.style.bottom)
//If it first clicked
if(iW > cW){
var mW = x - touch.startX
iML = iL + mW
iMR = iR - mW
// If image comes to croper left border
if(iML > 0){
iML = 0 // Stop moving right
iMR = iR
}
// If iamge comes to croper right border
if(iMR > 0 ) {
iML = iL
iMR = 0 // Stop moving left
}
}
// It runs only if image height greater than croper height
if(iH > cH){
var mH = y - touch.startY
iMT = iT + mH
iMB = iB - mH
// If image comes to croper left border
if(iMT > 0){
iMT = 0 // Stop moving right
iMB = iB
}
// If iamge comes to croper right border
if(iMB > 0 ) {
iMT = iT
iMB = 0 // Stop moving left
}
}
return {
imageMoveLeft : iML,
imageMoveRight : iMR,
imageMoveTop : iMT,
imageMoveBottom : iMB,
}
}
export const wheelZoom = (e , croper, image) => {
//console.log(e)
let imageWidth = image.current.offsetWidth
var x = e.offsetX
var y = e.offsetY
let newWidth = -(e.deltaY/4) + imageWidth
let newHeight = image.current.offsetHeight * (newWidth / image.current.offsetWidth)
var gap = croper.current.offsetWidth - newWidth
// Image Croper difference
var dif = newHeight - image.current.offsetHeight
var difW = newWidth - image.current.offsetWidth
// Cursor Points
let xLeft = x
let xRight = image.current.offsetWidth- x
let yTop = y
let yBottom = image.current.offsetHeight - y
var xLeftP = image.current.offsetLeft
var xRightP = -((image.current.width - croper.current.offsetWidth) + image.current.offsetLeft + 2)
var yTopP = image.current.offsetTop
var yBotP = -((image.current.height - croper.current.offsetHeight) + image.current.offsetTop + 2)
// Define variables for new x,y position
let newTop
let newBottom
let newLeft
let newRight
// If it is zoom in
if(e.deltaY < 0){
// Calculate position change for top and bottom according to cursor position
let newTopDif = (dif*yTop) / (yTop + yBottom)
let newBottomDif = dif - newTopDif
// Calculate position change for left and right according to cursor position
let newLeftDif = (difW*xLeft) / (xLeft + xRight )
let newRightDif = difW - newLeftDif
let imCropHeDif = image.current.offsetHeight - croper.current.offsetHeight
let imCropWiDif = image.current.offsetWidth - croper.current.offsetWidth
//console.log(imCropHeDif)
// Create new positions for image
newLeft = xLeftP - newLeftDif
newRight = xRightP - newRightDif
newTop = yTopP - newTopDif
newBottom = yBotP - newBottomDif
// Top and Bottom can't be less than cropper height
// If top and bottom greater than 0 it shows the image height will be less than croper height
// If top and bottom greater than 0 update bottom and top to 0
}else{ //If it is zoomout
// Calculate change amounts for top and bottom according the position stays outside of croper
let newTopDif = (dif*yTopP) / (yTopP + yBotP)
let newBottomDif = dif - newTopDif
// Calculate change amounts for left and right according the position stays outside of croper
let newLeftDif = (difW*xLeftP) / (xLeftP + xRightP)
let newRightDif = difW - newLeftDif
// Craete new positions for left, right, bottom and top
newTop = yTopP - newTopDif
newBottom = yBotP - newBottomDif
newLeft = xLeftP - newLeftDif
newRight = xRightP - newRightDif
// Top and Bottom can't be less than cropper height
// If top and bottom greater than 0 it shows the image height will be less than croper height
// If top and bottom greater than 0 update bottom and top to 0
// newTop = newTop >= 0 ? 0 : newTop
// newBottom = newBottom >= 0 ? 0 : newBottom
}
// If image with less than croper locate image center
if(image.current.offsetWidth < croper.current.offsetWidth){
newLeft = gap/2
newRight = gap/2
}
if(newHeight > croper.current.offsetHeight){
return {
imageTop : newTop,
imageBottom : newBottom,
imageLeft : newLeft,
imageRight : newRight,
imageHeight : newHeight,
imageWidth : newWidth,
}
}else{
return {
imageTop : 0,
imageBottom :0,
imageLeft : newLeft,
imageRight : newRight,
imageHeight : croper.current.offsetHeight,
}
}
}
export const getContainerPositionsToCroper = (gap , ratio, container) => {
var conParentWidth = container.current.parentElement.offsetWidth
let gapTop
let gapRight
let gapBottom
let gapLeft
console.log(typeof gap)
if( typeof gap === 'object' ){
if(gap.length === 4){
gapTop = gap[0]
gapRight = this.state.gap[1]
gapBottom = this.state.gap[2]
gapLeft = this.state.gap[3]
}else{
gapTop = 40
gapRight = 40
gapBottom = 40
gapLeft = 40
}
}else{
gapTop = gap
gapRight = gap
gapBottom = gap
gapLeft = gap
}
var croperWidth = conParentWidth - (gapLeft + gapRight)
return {
croperWidth : conParentWidth - (gapLeft + gapRight),
croperHeight : (croperWidth * ratio) - (gapTop + gapBottom),
containerWidth : conParentWidth,
containerHeight : (croperWidth * ratio),
croperRight : gapRight,
croperBottom : gapBottom,
croperTop : gapTop,
croperLeft : gapLeft,
}
}