-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.jsx
360 lines (346 loc) · 9.75 KB
/
utils.jsx
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
/**
* Calculates the hypotenuse of a right triangle
* @param {number} x Length of one side
* @param {number} y Length of the other side
* @returns {number} Length of the hypotenuse
*/
export const getLength = (x, y) => Math.sqrt(x * x + y * y);
/**
* Calculates the angle between two points
* @param {object} position1 First point (x, y)
* @param {object} position2 Second point (x, y)
* @returns {number} Angle in degrees
*/
export const getAngle = ({ x: x1, y: y1 }, { x: x2, y: y2 }) => {
const dot = x1 * x2 + y1 * y2;
const det = x1 * y2 - y1 * x2;
const angle = (Math.atan2(det, dot) / Math.PI) * 180;
return (angle + 360) % 360;
};
/**
* Converts degrees to radians
* @param {number} deg Angle in degrees
* @returns {number} Angle in radians
*/
export const degToRadian = deg => (deg * Math.PI) / 180;
/**
* cos(x) in degrees
*/
const cos = deg => Math.cos(degToRadian(deg));
/**
* sin(x) in degrees
* @param {*} deg Angle in degrees
*/
const sin = deg => Math.sin(degToRadian(deg));
/**
* Resolves a change in width with regards to a minWidth
* @param {number} width Current width
* @param {number} deltaW Change in width
* @param {number} minWidth Minimum width
* @returns {object} New width and change in width
*/
const setWidthAndDeltaW = (width, deltaW, minWidth) => {
const expectedWidth = width + deltaW;
if (expectedWidth > minWidth) {
width = expectedWidth;
} else {
deltaW = minWidth - width;
width = minWidth;
}
return {
deltaW,
width
};
};
/**
* Resolves a change in height with regards to a minHeight
* @param {number} height Current height
* @param {number} deltaH Change in height
* @param {number} minHeight Minimum height
* @returns {object} New height and change in height
*/
const setHeightAndDeltaH = (height, deltaH, minHeight) => {
const expectedHeight = height + deltaH;
if (expectedHeight > minHeight) {
height = expectedHeight;
} else {
deltaH = minHeight - height;
height = minHeight;
}
return {
deltaH,
height
};
};
/**
* Defines the new style of an item after a resize or rotate
* @param {string} type Resize handle type
* @param {object} rect Original rectangle
* @param {number} deltaW Change in width
* @param {number} deltaH Change in height
* @param {number} ratio Aspect ratio
* @param {number} minWidth Minimum width
* @param {number} minHeight Minimum height
* @returns {object} New style
*/
export const getNewStyle = (
type,
rect,
deltaW,
deltaH,
ratio,
minWidth,
minHeight
) => {
let { width, height, centerX, centerY, rotateAngle } = rect;
const widthFlag = width < 0 ? -1 : 1;
const heightFlag = height < 0 ? -1 : 1;
width = Math.abs(width);
height = Math.abs(height);
switch (type) {
case "e": {
const widthAndDeltaW = setWidthAndDeltaW(width, deltaW, minWidth);
width = widthAndDeltaW.width;
deltaW = widthAndDeltaW.deltaW;
if (ratio) {
deltaH = deltaW / ratio;
height = width / ratio;
// 左上角固定
centerX +=
(deltaW / 2) * cos(rotateAngle) - (deltaH / 2) * sin(rotateAngle);
centerY +=
(deltaW / 2) * sin(rotateAngle) + (deltaH / 2) * cos(rotateAngle);
} else {
// 左边固定
centerX += (deltaW / 2) * cos(rotateAngle);
centerY += (deltaW / 2) * sin(rotateAngle);
}
break;
}
case "ne": {
deltaH = -deltaH;
const widthAndDeltaW = setWidthAndDeltaW(width, deltaW, minWidth);
width = widthAndDeltaW.width;
deltaW = widthAndDeltaW.deltaW;
const heightAndDeltaH = setHeightAndDeltaH(height, deltaH, minHeight);
height = heightAndDeltaH.height;
deltaH = heightAndDeltaH.deltaH;
if (ratio) {
deltaW = deltaH * ratio;
width = height * ratio;
}
centerX +=
(deltaW / 2) * cos(rotateAngle) + (deltaH / 2) * sin(rotateAngle);
centerY +=
(deltaW / 2) * sin(rotateAngle) - (deltaH / 2) * cos(rotateAngle);
break;
}
case "se": {
const widthAndDeltaW = setWidthAndDeltaW(width, deltaW, minWidth);
width = widthAndDeltaW.width;
deltaW = widthAndDeltaW.deltaW;
const heightAndDeltaH = setHeightAndDeltaH(height, deltaH, minHeight);
height = heightAndDeltaH.height;
deltaH = heightAndDeltaH.deltaH;
if (ratio) {
deltaW = deltaH * ratio;
width = height * ratio;
}
centerX +=
(deltaW / 2) * cos(rotateAngle) - (deltaH / 2) * sin(rotateAngle);
centerY +=
(deltaW / 2) * sin(rotateAngle) + (deltaH / 2) * cos(rotateAngle);
break;
}
case "s": {
const heightAndDeltaH = setHeightAndDeltaH(height, deltaH, minHeight);
height = heightAndDeltaH.height;
deltaH = heightAndDeltaH.deltaH;
if (ratio) {
deltaW = deltaH * ratio;
width = height * ratio;
// 左上角固定
centerX +=
(deltaW / 2) * cos(rotateAngle) - (deltaH / 2) * sin(rotateAngle);
centerY +=
(deltaW / 2) * sin(rotateAngle) + (deltaH / 2) * cos(rotateAngle);
} else {
// 上边固定
centerX -= (deltaH / 2) * sin(rotateAngle);
centerY += (deltaH / 2) * cos(rotateAngle);
}
break;
}
case "sw": {
deltaW = -deltaW;
const widthAndDeltaW = setWidthAndDeltaW(width, deltaW, minWidth);
width = widthAndDeltaW.width;
deltaW = widthAndDeltaW.deltaW;
const heightAndDeltaH = setHeightAndDeltaH(height, deltaH, minHeight);
height = heightAndDeltaH.height;
deltaH = heightAndDeltaH.deltaH;
if (ratio) {
height = width / ratio;
deltaH = deltaW / ratio;
}
centerX -=
(deltaW / 2) * cos(rotateAngle) + (deltaH / 2) * sin(rotateAngle);
centerY -=
(deltaW / 2) * sin(rotateAngle) - (deltaH / 2) * cos(rotateAngle);
break;
}
case "w": {
deltaW = -deltaW;
const widthAndDeltaW = setWidthAndDeltaW(width, deltaW, minWidth);
width = widthAndDeltaW.width;
deltaW = widthAndDeltaW.deltaW;
if (ratio) {
height = width / ratio;
deltaH = deltaW / ratio;
// 右上角固定
centerX -=
(deltaW / 2) * cos(rotateAngle) + (deltaH / 2) * sin(rotateAngle);
centerY -=
(deltaW / 2) * sin(rotateAngle) - (deltaH / 2) * cos(rotateAngle);
} else {
// 右边固定
centerX -= (deltaW / 2) * cos(rotateAngle);
centerY -= (deltaW / 2) * sin(rotateAngle);
}
break;
}
case "nw": {
deltaW = -deltaW;
deltaH = -deltaH;
const widthAndDeltaW = setWidthAndDeltaW(width, deltaW, minWidth);
width = widthAndDeltaW.width;
deltaW = widthAndDeltaW.deltaW;
const heightAndDeltaH = setHeightAndDeltaH(height, deltaH, minHeight);
height = heightAndDeltaH.height;
deltaH = heightAndDeltaH.deltaH;
if (ratio) {
width = height * ratio;
deltaW = deltaH * ratio;
}
centerX -=
(deltaW / 2) * cos(rotateAngle) - (deltaH / 2) * sin(rotateAngle);
centerY -=
(deltaW / 2) * sin(rotateAngle) + (deltaH / 2) * cos(rotateAngle);
break;
}
case "n": {
deltaH = -deltaH;
const heightAndDeltaH = setHeightAndDeltaH(height, deltaH, minHeight);
height = heightAndDeltaH.height;
deltaH = heightAndDeltaH.deltaH;
if (ratio) {
width = height * ratio;
deltaW = deltaH * ratio;
// 左下角固定
centerX +=
(deltaW / 2) * cos(rotateAngle) + (deltaH / 2) * sin(rotateAngle);
centerY +=
(deltaW / 2) * sin(rotateAngle) - (deltaH / 2) * cos(rotateAngle);
} else {
centerX += (deltaH / 2) * sin(rotateAngle);
centerY -= (deltaH / 2) * cos(rotateAngle);
}
break;
}
}
return {
position: {
centerX,
centerY
},
size: {
height: height * heightFlag,
width: width * widthFlag
}
};
};
// map of resize direction to clockwise index
const cursorStartMap = {
e: 2,
n: 0,
ne: 1,
nw: 7,
s: 4,
se: 3,
sw: 5,
w: 6
};
// array of css cursor resize directions in a clockwise order forn 0 - 360 degrees in 45 degree increments
const cursorDirectionArray = [
"ns",
"nesw",
"ew",
"nwse",
"ns",
"nesw",
"ew",
"nwse"
];
// map of normalized 30 degree segment to cursor direction
const cursorMap = {
0: 0,
1: 1,
10: 7,
11: 8,
2: 2,
3: 2,
4: 3,
5: 4,
6: 4,
7: 5,
8: 6,
9: 6
};
/**
* Returns the css cursor name for the given angle and resize direction
* @param {number} rotateAngle Rotation angle of the rectangle
* @param {string} d Resize handle direction
* @returns {string} css cursor name
*/
export const getCursor = (rotateAngle, d) => {
const increment = cursorMap[Math.floor(rotateAngle / 30)];
const index = cursorStartMap[d];
const newIndex = (index + increment) % 8;
return cursorDirectionArray[newIndex];
};
/**
* Transforms a rectangle layout from center origin to top left origin
* @param {object} layout Layout object with centerX, centerY, width, height, rotateAngle
* @returns {object} Layout object with top, left, width, height, rotateAngle
*/
export const centerToTL = ({
centerX,
centerY,
width,
height,
rotateAngle
}) => ({
height,
left: centerX - width / 2,
rotateAngle,
top: centerY - height / 2,
width
});
/**
* Transforms a rectangle layout from top left origin to center origin
* @param {object} layout Layout object with top, left, width, height, rotateAngle
* @returns {object} Layout object with centerX, centerY, width, height, rotateAngle
*/
export const tLToCenter = ({ top, left, width, height, rotateAngle }) => ({
position: {
centerX: left + width / 2,
centerY: top + height / 2
},
size: {
height,
width
},
transform: {
rotateAngle
}
});