@thienvu18/designable-shared 2.0.1 → 2.0.2
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.
- package/esm/animation.d.ts +8 -2
- package/esm/animation.js +29 -30
- package/esm/array.d.ts +163 -37
- package/esm/array.js +104 -87
- package/esm/clone.d.ts +4 -4
- package/esm/clone.js +79 -86
- package/esm/compose.d.ts +3 -1
- package/esm/compose.js +6 -6
- package/esm/coordinate.d.ts +120 -76
- package/esm/coordinate.js +480 -411
- package/esm/element.d.ts +11 -6
- package/esm/element.js +123 -125
- package/esm/event.d.ts +162 -75
- package/esm/event.js +197 -197
- package/esm/globalThisPolyfill.d.ts +1 -1
- package/esm/globalThisPolyfill.js +14 -17
- package/esm/index.d.ts +17 -17
- package/esm/index.js +17 -17
- package/esm/instanceof.d.ts +1 -1
- package/esm/instanceof.js +9 -10
- package/esm/keycode.d.ts +145 -145
- package/esm/keycode.js +149 -149
- package/esm/lru.d.ts +50 -47
- package/esm/lru.js +236 -245
- package/esm/observer.d.ts +7 -7
- package/esm/observer.js +37 -40
- package/esm/request-idle.d.ts +9 -6
- package/esm/request-idle.js +6 -6
- package/esm/scroller.d.ts +24 -8
- package/esm/scroller.js +78 -75
- package/esm/subscribable.d.ts +10 -10
- package/esm/subscribable.js +39 -40
- package/esm/types.d.ts +13 -13
- package/esm/types.js +19 -20
- package/esm/uid.d.ts +1 -1
- package/esm/uid.js +7 -7
- package/lib/animation.d.ts +8 -2
- package/lib/animation.js +34 -35
- package/lib/array.d.ts +163 -37
- package/lib/array.js +129 -101
- package/lib/clone.d.ts +4 -4
- package/lib/clone.js +84 -91
- package/lib/compose.d.ts +3 -1
- package/lib/compose.js +10 -10
- package/lib/coordinate.d.ts +120 -76
- package/lib/coordinate.js +538 -440
- package/lib/element.d.ts +11 -6
- package/lib/element.js +139 -133
- package/lib/event.d.ts +162 -75
- package/lib/event.js +206 -202
- package/lib/globalThisPolyfill.d.ts +1 -1
- package/lib/globalThisPolyfill.js +17 -20
- package/lib/index.d.ts +17 -17
- package/lib/index.js +49 -33
- package/lib/instanceof.d.ts +1 -1
- package/lib/instanceof.js +13 -14
- package/lib/keycode.d.ts +145 -145
- package/lib/keycode.js +153 -153
- package/lib/lru.d.ts +50 -47
- package/lib/lru.js +240 -249
- package/lib/observer.d.ts +7 -7
- package/lib/observer.js +41 -44
- package/lib/request-idle.d.ts +9 -6
- package/lib/request-idle.js +14 -11
- package/lib/scroller.d.ts +24 -8
- package/lib/scroller.js +89 -81
- package/lib/subscribable.d.ts +10 -10
- package/lib/subscribable.js +43 -44
- package/lib/types.d.ts +13 -13
- package/lib/types.js +40 -28
- package/lib/uid.d.ts +1 -1
- package/lib/uid.js +11 -11
- package/package.json +1 -1
package/lib/coordinate.js
CHANGED
|
@@ -1,502 +1,600 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
3
|
-
exports.calcClosestEdges =
|
|
4
|
-
|
|
1
|
+
'use strict'
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true })
|
|
3
|
+
exports.calcClosestEdges =
|
|
4
|
+
exports.calcCombineSnapLineSegment =
|
|
5
|
+
exports.calcDistanceOfSnapLineToEdges =
|
|
6
|
+
exports.calcOffsetOfSnapLineSegmentToEdge =
|
|
7
|
+
exports.calcExtendsLineSegmentOfRect =
|
|
8
|
+
exports.calcSpaceBlockOfRect =
|
|
9
|
+
exports.calcRectOfAxisLineSegment =
|
|
10
|
+
exports.calcEdgeLinesOfRect =
|
|
11
|
+
exports.calcRectByStartEndPoint =
|
|
12
|
+
exports.calcBoundingRect =
|
|
13
|
+
exports.calcRelativeOfPointToRect =
|
|
14
|
+
exports.isNearAfter =
|
|
15
|
+
exports.calcDistancePointToEdge =
|
|
16
|
+
exports.calcDistanceOfPointToRect =
|
|
17
|
+
exports.calcQuadrantOfPointToRect =
|
|
18
|
+
exports.isCrossRectInRect =
|
|
19
|
+
exports.isRectInRect =
|
|
20
|
+
exports.getRectPoints =
|
|
21
|
+
exports.isEqualRect =
|
|
22
|
+
exports.isPointInRect =
|
|
23
|
+
exports.RectQuadrant =
|
|
24
|
+
exports.LineSegment =
|
|
25
|
+
exports.Rect =
|
|
26
|
+
exports.Point =
|
|
27
|
+
exports.isLineSegment =
|
|
28
|
+
exports.isPoint =
|
|
29
|
+
exports.isRect =
|
|
30
|
+
void 0
|
|
31
|
+
const types_1 = require('./types')
|
|
5
32
|
function isRect(rect) {
|
|
6
|
-
|
|
33
|
+
return rect?.x && rect?.y && rect?.width && rect?.height
|
|
7
34
|
}
|
|
8
|
-
exports.isRect = isRect
|
|
35
|
+
exports.isRect = isRect
|
|
9
36
|
function isPoint(val) {
|
|
10
|
-
|
|
37
|
+
return (
|
|
38
|
+
(0, types_1.isValidNumber)(val?.x) && (0, types_1.isValidNumber)(val?.y)
|
|
39
|
+
)
|
|
11
40
|
}
|
|
12
|
-
exports.isPoint = isPoint
|
|
41
|
+
exports.isPoint = isPoint
|
|
13
42
|
function isLineSegment(val) {
|
|
14
|
-
|
|
43
|
+
return isPoint(val?.start) && isPoint(val?.end)
|
|
15
44
|
}
|
|
16
|
-
exports.isLineSegment = isLineSegment
|
|
45
|
+
exports.isLineSegment = isLineSegment
|
|
17
46
|
class Point {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
47
|
+
constructor(x, y) {
|
|
48
|
+
this.x = x
|
|
49
|
+
this.y = y
|
|
50
|
+
}
|
|
22
51
|
}
|
|
23
|
-
exports.Point = Point
|
|
52
|
+
exports.Point = Point
|
|
24
53
|
class Rect {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
54
|
+
constructor(x, y, width, height) {
|
|
55
|
+
this.x = 0
|
|
56
|
+
this.y = 0
|
|
57
|
+
this.width = 0
|
|
58
|
+
this.height = 0
|
|
59
|
+
this.x = x
|
|
60
|
+
this.y = y
|
|
61
|
+
this.width = width
|
|
62
|
+
this.height = height
|
|
63
|
+
}
|
|
64
|
+
get left() {
|
|
65
|
+
return this.x
|
|
66
|
+
}
|
|
67
|
+
get right() {
|
|
68
|
+
return this.x + this.width
|
|
69
|
+
}
|
|
70
|
+
get top() {
|
|
71
|
+
return this.y
|
|
72
|
+
}
|
|
73
|
+
get bottom() {
|
|
74
|
+
return this.y + this.height
|
|
75
|
+
}
|
|
47
76
|
}
|
|
48
|
-
exports.Rect = Rect
|
|
77
|
+
exports.Rect = Rect
|
|
49
78
|
class LineSegment {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
79
|
+
constructor(start, end) {
|
|
80
|
+
this.start = { ...start }
|
|
81
|
+
this.end = { ...end }
|
|
82
|
+
}
|
|
54
83
|
}
|
|
55
|
-
exports.LineSegment = LineSegment
|
|
56
|
-
var RectQuadrant
|
|
57
|
-
(function (RectQuadrant) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
})(RectQuadrant || (exports.RectQuadrant = RectQuadrant = {}))
|
|
84
|
+
exports.LineSegment = LineSegment
|
|
85
|
+
var RectQuadrant
|
|
86
|
+
;(function (RectQuadrant) {
|
|
87
|
+
RectQuadrant['Inner1'] = 'I1'
|
|
88
|
+
RectQuadrant['Inner2'] = 'I2'
|
|
89
|
+
RectQuadrant['Inner3'] = 'I3'
|
|
90
|
+
RectQuadrant['Inner4'] = 'I4'
|
|
91
|
+
RectQuadrant['Outer1'] = 'O1'
|
|
92
|
+
RectQuadrant['Outer2'] = 'O2'
|
|
93
|
+
RectQuadrant['Outer3'] = 'O3'
|
|
94
|
+
RectQuadrant['Outer4'] = 'O4'
|
|
95
|
+
})(RectQuadrant || (exports.RectQuadrant = RectQuadrant = {}))
|
|
67
96
|
function isPointInRect(point, rect, sensitive = true) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
point.y <= rect.y + rect.height - boundSensor(rect.height));
|
|
97
|
+
const boundSensor = (value) => {
|
|
98
|
+
if (!sensitive) return 0
|
|
99
|
+
const sensor = value * 0.1
|
|
100
|
+
if (sensor > 20) return 20
|
|
101
|
+
if (sensor < 10) return 10
|
|
102
|
+
return sensor
|
|
103
|
+
}
|
|
104
|
+
return (
|
|
105
|
+
point.x >= rect.x + boundSensor(rect.width) &&
|
|
106
|
+
point.x <= rect.x + rect.width - boundSensor(rect.width) &&
|
|
107
|
+
point.y >= rect.y + boundSensor(rect.height) &&
|
|
108
|
+
point.y <= rect.y + rect.height - boundSensor(rect.height)
|
|
109
|
+
)
|
|
82
110
|
}
|
|
83
|
-
exports.isPointInRect = isPointInRect
|
|
111
|
+
exports.isPointInRect = isPointInRect
|
|
84
112
|
function isEqualRect(target, source) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
113
|
+
return (
|
|
114
|
+
target?.x === source?.x &&
|
|
115
|
+
target.y === source.y &&
|
|
116
|
+
target.width === source.width &&
|
|
117
|
+
target.height === source.height
|
|
118
|
+
)
|
|
89
119
|
}
|
|
90
|
-
exports.isEqualRect = isEqualRect
|
|
120
|
+
exports.isEqualRect = isEqualRect
|
|
91
121
|
function getRectPoints(source) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
122
|
+
const p1 = new Point(source.x, source.y)
|
|
123
|
+
const p2 = new Point(source.x + source.width, source.y)
|
|
124
|
+
const p3 = new Point(source.x + source.width, source.y + source.height)
|
|
125
|
+
const p4 = new Point(source.x, source.y + source.height)
|
|
126
|
+
return [p1, p2, p3, p4]
|
|
97
127
|
}
|
|
98
|
-
exports.getRectPoints = getRectPoints
|
|
128
|
+
exports.getRectPoints = getRectPoints
|
|
99
129
|
function isRectInRect(target, source) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
130
|
+
const [p1, p2, p3, p4] = getRectPoints(target)
|
|
131
|
+
return (
|
|
132
|
+
isPointInRect(p1, source, false) &&
|
|
133
|
+
isPointInRect(p2, source, false) &&
|
|
134
|
+
isPointInRect(p3, source, false) &&
|
|
135
|
+
isPointInRect(p4, source, false)
|
|
136
|
+
)
|
|
105
137
|
}
|
|
106
|
-
exports.isRectInRect = isRectInRect
|
|
138
|
+
exports.isRectInRect = isRectInRect
|
|
107
139
|
function isCrossRectInRect(target, source) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
140
|
+
const targetCenterPoint = new Point(
|
|
141
|
+
target.x + target.width / 2,
|
|
142
|
+
target.y + target.height / 2
|
|
143
|
+
)
|
|
144
|
+
const sourceCenterPoint = new Point(
|
|
145
|
+
source.x + source.width / 2,
|
|
146
|
+
source.y + source.height / 2
|
|
147
|
+
)
|
|
148
|
+
return (
|
|
149
|
+
Math.abs(targetCenterPoint.x - sourceCenterPoint.x) <=
|
|
150
|
+
target.width / 2 + source.width / 2 &&
|
|
151
|
+
Math.abs(targetCenterPoint.y - sourceCenterPoint.y) <=
|
|
152
|
+
target.height / 2 + source.height / 2
|
|
153
|
+
)
|
|
114
154
|
}
|
|
115
|
-
exports.isCrossRectInRect = isCrossRectInRect
|
|
155
|
+
exports.isCrossRectInRect = isCrossRectInRect
|
|
116
156
|
/**
|
|
117
157
|
* 计算点在矩形的哪个象限
|
|
118
158
|
* @param point
|
|
119
159
|
* @param rect
|
|
120
160
|
*/
|
|
121
161
|
function calcQuadrantOfPointToRect(point, rect) {
|
|
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
|
-
return RectQuadrant.Inner3;
|
|
153
|
-
}
|
|
154
|
-
else {
|
|
155
|
-
return RectQuadrant.Outer3;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
}
|
|
162
|
+
const isInner = isPointInRect(point, rect)
|
|
163
|
+
if (point.x <= rect.x + rect.width / 2) {
|
|
164
|
+
if (point.y <= rect.y + rect.height / 2) {
|
|
165
|
+
if (isInner) {
|
|
166
|
+
return RectQuadrant.Inner1
|
|
167
|
+
} else {
|
|
168
|
+
return RectQuadrant.Outer1
|
|
169
|
+
}
|
|
170
|
+
} else {
|
|
171
|
+
if (isInner) {
|
|
172
|
+
return RectQuadrant.Inner4
|
|
173
|
+
} else {
|
|
174
|
+
return RectQuadrant.Outer4
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
} else {
|
|
178
|
+
if (point.y <= rect.y + rect.height / 2) {
|
|
179
|
+
if (isInner) {
|
|
180
|
+
return RectQuadrant.Inner2
|
|
181
|
+
} else {
|
|
182
|
+
return RectQuadrant.Outer2
|
|
183
|
+
}
|
|
184
|
+
} else {
|
|
185
|
+
if (isInner) {
|
|
186
|
+
return RectQuadrant.Inner3
|
|
187
|
+
} else {
|
|
188
|
+
return RectQuadrant.Outer3
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
159
192
|
}
|
|
160
|
-
exports.calcQuadrantOfPointToRect = calcQuadrantOfPointToRect
|
|
193
|
+
exports.calcQuadrantOfPointToRect = calcQuadrantOfPointToRect
|
|
161
194
|
function calcDistanceOfPointToRect(point, rect) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
195
|
+
let minX = Math.min(
|
|
196
|
+
Math.abs(point.x - rect.x),
|
|
197
|
+
Math.abs(point.x - (rect.x + rect.width))
|
|
198
|
+
)
|
|
199
|
+
let minY = Math.min(
|
|
200
|
+
Math.abs(point.y - rect.y),
|
|
201
|
+
Math.abs(point.y - (rect.y + rect.height))
|
|
202
|
+
)
|
|
203
|
+
if (point.x >= rect.x && point.x <= rect.x + rect.width) {
|
|
204
|
+
minX = 0
|
|
205
|
+
}
|
|
206
|
+
if (point.y >= rect.y && point.y <= rect.y + rect.height) {
|
|
207
|
+
minY = 0
|
|
208
|
+
}
|
|
209
|
+
return Math.sqrt(minX ** 2 + minY ** 2)
|
|
171
210
|
}
|
|
172
|
-
exports.calcDistanceOfPointToRect = calcDistanceOfPointToRect
|
|
211
|
+
exports.calcDistanceOfPointToRect = calcDistanceOfPointToRect
|
|
173
212
|
function calcDistancePointToEdge(point, rect) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
213
|
+
const distanceTop = Math.abs(point.y - rect.y)
|
|
214
|
+
const distanceBottom = Math.abs(point.y - (rect.y + rect.height))
|
|
215
|
+
const distanceLeft = Math.abs(point.x - rect.x)
|
|
216
|
+
const distanceRight = Math.abs(point.x - (rect.x + rect.width))
|
|
217
|
+
return Math.min(distanceTop, distanceBottom, distanceLeft, distanceRight)
|
|
179
218
|
}
|
|
180
|
-
exports.calcDistancePointToEdge = calcDistancePointToEdge
|
|
219
|
+
exports.calcDistancePointToEdge = calcDistancePointToEdge
|
|
181
220
|
function isNearAfter(point, rect, inline = false) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
221
|
+
if (inline) {
|
|
222
|
+
return (
|
|
223
|
+
Math.abs(point.x - rect.x) + Math.abs(point.y - rect.y) >
|
|
224
|
+
Math.abs(point.x - (rect.x + rect.width)) +
|
|
225
|
+
Math.abs(point.y - (rect.y + rect.height))
|
|
226
|
+
)
|
|
227
|
+
}
|
|
228
|
+
return Math.abs(point.y - rect.y) > Math.abs(point.y - (rect.y + rect.height))
|
|
188
229
|
}
|
|
189
|
-
exports.isNearAfter = isNearAfter
|
|
230
|
+
exports.isNearAfter = isNearAfter
|
|
190
231
|
/**
|
|
191
232
|
* 计算点鱼矩形的相对位置信息
|
|
192
233
|
* @param point
|
|
193
234
|
* @param rect
|
|
194
235
|
*/
|
|
195
236
|
function calcRelativeOfPointToRect(point, rect) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
237
|
+
const distance = calcDistanceOfPointToRect(point, rect)
|
|
238
|
+
const quadrant = calcQuadrantOfPointToRect(point, rect)
|
|
239
|
+
return {
|
|
240
|
+
quadrant,
|
|
241
|
+
distance,
|
|
242
|
+
}
|
|
202
243
|
}
|
|
203
|
-
exports.calcRelativeOfPointToRect = calcRelativeOfPointToRect
|
|
244
|
+
exports.calcRelativeOfPointToRect = calcRelativeOfPointToRect
|
|
204
245
|
function calcBoundingRect(rects) {
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
});
|
|
228
|
-
return new Rect(minLeft, minTop, maxRight - minLeft, maxBottom - minTop);
|
|
246
|
+
if (!rects?.length) return
|
|
247
|
+
if (rects?.length === 1 && !rects[0]) return
|
|
248
|
+
let minTop = Infinity
|
|
249
|
+
let maxBottom = -Infinity
|
|
250
|
+
let minLeft = Infinity
|
|
251
|
+
let maxRight = -Infinity
|
|
252
|
+
rects.forEach((item) => {
|
|
253
|
+
const rect = new Rect(item.x, item.y, item.width, item.height)
|
|
254
|
+
if (rect.top <= minTop) {
|
|
255
|
+
minTop = rect.top
|
|
256
|
+
}
|
|
257
|
+
if (rect.bottom >= maxBottom) {
|
|
258
|
+
maxBottom = rect.bottom
|
|
259
|
+
}
|
|
260
|
+
if (rect.left <= minLeft) {
|
|
261
|
+
minLeft = rect.left
|
|
262
|
+
}
|
|
263
|
+
if (rect.right >= maxRight) {
|
|
264
|
+
maxRight = rect.right
|
|
265
|
+
}
|
|
266
|
+
})
|
|
267
|
+
return new Rect(minLeft, minTop, maxRight - minLeft, maxBottom - minTop)
|
|
229
268
|
}
|
|
230
|
-
exports.calcBoundingRect = calcBoundingRect
|
|
231
|
-
function calcRectByStartEndPoint(
|
|
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
|
-
|
|
269
|
+
exports.calcBoundingRect = calcBoundingRect
|
|
270
|
+
function calcRectByStartEndPoint(
|
|
271
|
+
startPoint,
|
|
272
|
+
endPoint,
|
|
273
|
+
scrollX = 0,
|
|
274
|
+
scrollY = 0
|
|
275
|
+
) {
|
|
276
|
+
let drawStartX = 0,
|
|
277
|
+
drawStartY = 0
|
|
278
|
+
if (
|
|
279
|
+
endPoint.x + scrollX >= startPoint.x &&
|
|
280
|
+
endPoint.y + scrollY >= startPoint.y
|
|
281
|
+
) {
|
|
282
|
+
//4象限
|
|
283
|
+
drawStartX = startPoint.x
|
|
284
|
+
drawStartY = startPoint.y
|
|
285
|
+
return new Rect(
|
|
286
|
+
drawStartX - scrollX,
|
|
287
|
+
drawStartY - scrollY,
|
|
288
|
+
Math.abs(endPoint.x - startPoint.x + scrollX),
|
|
289
|
+
Math.abs(endPoint.y - startPoint.y + scrollY)
|
|
290
|
+
)
|
|
291
|
+
} else if (
|
|
292
|
+
endPoint.x + scrollX < startPoint.x &&
|
|
293
|
+
endPoint.y + scrollY < startPoint.y
|
|
294
|
+
) {
|
|
295
|
+
//1象限
|
|
296
|
+
drawStartX = endPoint.x
|
|
297
|
+
drawStartY = endPoint.y
|
|
298
|
+
return new Rect(
|
|
299
|
+
drawStartX,
|
|
300
|
+
drawStartY,
|
|
301
|
+
Math.abs(endPoint.x - startPoint.x + scrollX),
|
|
302
|
+
Math.abs(endPoint.y - startPoint.y + scrollY)
|
|
303
|
+
)
|
|
304
|
+
} else if (
|
|
305
|
+
endPoint.x + scrollX < startPoint.x &&
|
|
306
|
+
endPoint.y + scrollY >= startPoint.y
|
|
307
|
+
) {
|
|
308
|
+
//3象限
|
|
309
|
+
drawStartX = endPoint.x
|
|
310
|
+
drawStartY = startPoint.y
|
|
311
|
+
return new Rect(
|
|
312
|
+
drawStartX - scrollX,
|
|
313
|
+
drawStartY - scrollY,
|
|
314
|
+
Math.abs(endPoint.x - startPoint.x + scrollX),
|
|
315
|
+
Math.abs(endPoint.y - startPoint.y + scrollY)
|
|
316
|
+
)
|
|
317
|
+
} else {
|
|
318
|
+
//2象限
|
|
319
|
+
drawStartX = startPoint.x
|
|
320
|
+
drawStartY = endPoint.y
|
|
321
|
+
return new Rect(
|
|
322
|
+
drawStartX,
|
|
323
|
+
drawStartY,
|
|
324
|
+
Math.abs(endPoint.x - startPoint.x + scrollX),
|
|
325
|
+
Math.abs(endPoint.y - startPoint.y + scrollY)
|
|
326
|
+
)
|
|
327
|
+
}
|
|
260
328
|
}
|
|
261
|
-
exports.calcRectByStartEndPoint = calcRectByStartEndPoint
|
|
329
|
+
exports.calcRectByStartEndPoint = calcRectByStartEndPoint
|
|
262
330
|
function calcEdgeLinesOfRect(rect) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
331
|
+
return {
|
|
332
|
+
v: [
|
|
333
|
+
new LineSegment(
|
|
334
|
+
new Point(rect.x, rect.y),
|
|
335
|
+
new Point(rect.x, rect.y + rect.height)
|
|
336
|
+
),
|
|
337
|
+
new LineSegment(
|
|
338
|
+
new Point(rect.x + rect.width / 2, rect.y),
|
|
339
|
+
new Point(rect.x + rect.width / 2, rect.y + rect.height)
|
|
340
|
+
),
|
|
341
|
+
new LineSegment(
|
|
342
|
+
new Point(rect.x + rect.width, rect.y),
|
|
343
|
+
new Point(rect.x + rect.width, rect.y + rect.height)
|
|
344
|
+
),
|
|
345
|
+
],
|
|
346
|
+
h: [
|
|
347
|
+
new LineSegment(
|
|
348
|
+
new Point(rect.x, rect.y),
|
|
349
|
+
new Point(rect.x + rect.width, rect.y)
|
|
350
|
+
),
|
|
351
|
+
new LineSegment(
|
|
352
|
+
new Point(rect.x, rect.y + rect.height / 2),
|
|
353
|
+
new Point(rect.x + rect.width, rect.y + rect.height / 2)
|
|
354
|
+
),
|
|
355
|
+
new LineSegment(
|
|
356
|
+
new Point(rect.x, rect.y + rect.height),
|
|
357
|
+
new Point(rect.x + rect.width, rect.y + rect.height)
|
|
358
|
+
),
|
|
359
|
+
],
|
|
360
|
+
}
|
|
275
361
|
}
|
|
276
|
-
exports.calcEdgeLinesOfRect = calcEdgeLinesOfRect
|
|
362
|
+
exports.calcEdgeLinesOfRect = calcEdgeLinesOfRect
|
|
277
363
|
function calcRectOfAxisLineSegment(line) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
364
|
+
if (!isLineSegment(line)) return
|
|
365
|
+
const isXAxis = line.start.x === line.end.x
|
|
366
|
+
return new Rect(
|
|
367
|
+
line.start.x,
|
|
368
|
+
line.start.y,
|
|
369
|
+
isXAxis ? 0 : line.end.x - line.start.x,
|
|
370
|
+
isXAxis ? line.end.y - line.start.y : 0
|
|
371
|
+
)
|
|
282
372
|
}
|
|
283
|
-
exports.calcRectOfAxisLineSegment = calcRectOfAxisLineSegment
|
|
373
|
+
exports.calcRectOfAxisLineSegment = calcRectOfAxisLineSegment
|
|
284
374
|
function calcSpaceBlockOfRect(target, source, type) {
|
|
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
|
-
const bottom = Math.max(sourceRect.bottom, targetRect.bottom);
|
|
335
|
-
if (type && type !== 'right')
|
|
336
|
-
return;
|
|
337
|
-
return {
|
|
338
|
-
type: 'right',
|
|
339
|
-
distance,
|
|
340
|
-
rect: new Rect(targetRect.right, top, distance, bottom - top),
|
|
341
|
-
};
|
|
375
|
+
const targetRect = new Rect(target.x, target.y, target.width, target.height)
|
|
376
|
+
const sourceRect = new Rect(source.x, source.y, source.width, source.height)
|
|
377
|
+
if (sourceRect.bottom < targetRect.top && sourceRect.left > targetRect.right)
|
|
378
|
+
return
|
|
379
|
+
if (sourceRect.top > targetRect.bottom && sourceRect.left > targetRect.right)
|
|
380
|
+
return
|
|
381
|
+
if (sourceRect.bottom < targetRect.top && sourceRect.right < targetRect.left)
|
|
382
|
+
return
|
|
383
|
+
if (sourceRect.top > targetRect.bottom && sourceRect.right < targetRect.left)
|
|
384
|
+
return
|
|
385
|
+
if (sourceRect.bottom < targetRect.top) {
|
|
386
|
+
const distance = targetRect.top - sourceRect.bottom
|
|
387
|
+
const left = Math.min(sourceRect.left, targetRect.left)
|
|
388
|
+
const right = Math.max(sourceRect.right, targetRect.right)
|
|
389
|
+
if (type && type !== 'top') return
|
|
390
|
+
return {
|
|
391
|
+
type: 'top',
|
|
392
|
+
distance,
|
|
393
|
+
rect: new Rect(left, sourceRect.bottom, right - left, distance),
|
|
394
|
+
}
|
|
395
|
+
} else if (sourceRect.top > targetRect.bottom) {
|
|
396
|
+
const distance = sourceRect.top - targetRect.bottom
|
|
397
|
+
const left = Math.min(sourceRect.left, targetRect.left)
|
|
398
|
+
const right = Math.max(sourceRect.right, targetRect.right)
|
|
399
|
+
if (type && type !== 'bottom') return
|
|
400
|
+
return {
|
|
401
|
+
type: 'bottom',
|
|
402
|
+
distance,
|
|
403
|
+
rect: new Rect(left, targetRect.bottom, right - left, distance),
|
|
404
|
+
}
|
|
405
|
+
} else if (sourceRect.right < targetRect.left) {
|
|
406
|
+
const distance = targetRect.left - sourceRect.right
|
|
407
|
+
const top = Math.min(sourceRect.top, targetRect.top)
|
|
408
|
+
const bottom = Math.max(sourceRect.bottom, targetRect.bottom)
|
|
409
|
+
if (type && type !== 'left') return
|
|
410
|
+
return {
|
|
411
|
+
type: 'left',
|
|
412
|
+
distance,
|
|
413
|
+
rect: new Rect(sourceRect.right, top, distance, bottom - top),
|
|
414
|
+
}
|
|
415
|
+
} else if (sourceRect.left > targetRect.right) {
|
|
416
|
+
const distance = sourceRect.left - targetRect.right
|
|
417
|
+
const top = Math.min(sourceRect.top, targetRect.top)
|
|
418
|
+
const bottom = Math.max(sourceRect.bottom, targetRect.bottom)
|
|
419
|
+
if (type && type !== 'right') return
|
|
420
|
+
return {
|
|
421
|
+
type: 'right',
|
|
422
|
+
distance,
|
|
423
|
+
rect: new Rect(targetRect.right, top, distance, bottom - top),
|
|
342
424
|
}
|
|
425
|
+
}
|
|
343
426
|
}
|
|
344
|
-
exports.calcSpaceBlockOfRect = calcSpaceBlockOfRect
|
|
427
|
+
exports.calcSpaceBlockOfRect = calcSpaceBlockOfRect
|
|
345
428
|
function calcExtendsLineSegmentOfRect(targetRect, referRect) {
|
|
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
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
429
|
+
if (
|
|
430
|
+
referRect.right < targetRect.right &&
|
|
431
|
+
targetRect.left <= referRect.right
|
|
432
|
+
) {
|
|
433
|
+
//右侧
|
|
434
|
+
if (referRect.bottom < targetRect.top) {
|
|
435
|
+
//上方
|
|
436
|
+
return {
|
|
437
|
+
start: { x: referRect.right, y: referRect.bottom },
|
|
438
|
+
end: { x: targetRect.right, y: referRect.bottom },
|
|
439
|
+
}
|
|
440
|
+
} else if (referRect.top > targetRect.bottom) {
|
|
441
|
+
//下方
|
|
442
|
+
return {
|
|
443
|
+
start: { x: referRect.right, y: referRect.top },
|
|
444
|
+
end: { x: targetRect.right, y: referRect.top },
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
} else if (
|
|
448
|
+
referRect.left > targetRect.left &&
|
|
449
|
+
targetRect.right >= referRect.left
|
|
450
|
+
) {
|
|
451
|
+
//左侧
|
|
452
|
+
if (referRect.bottom < targetRect.top) {
|
|
453
|
+
//上方
|
|
454
|
+
return {
|
|
455
|
+
start: { x: targetRect.left, y: referRect.bottom },
|
|
456
|
+
end: { x: referRect.left, y: referRect.bottom },
|
|
457
|
+
}
|
|
458
|
+
} else if (referRect.top > targetRect.bottom) {
|
|
459
|
+
//下方
|
|
460
|
+
return {
|
|
461
|
+
start: { x: targetRect.left, y: referRect.top },
|
|
462
|
+
end: { x: referRect.left, y: referRect.top },
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
if (referRect.top < targetRect.top && targetRect.bottom >= referRect.top) {
|
|
467
|
+
//refer在上方
|
|
468
|
+
if (referRect.right < targetRect.left) {
|
|
469
|
+
//右侧
|
|
470
|
+
return {
|
|
471
|
+
start: { x: referRect.right, y: referRect.bottom },
|
|
472
|
+
end: { x: referRect.right, y: targetRect.bottom },
|
|
473
|
+
}
|
|
474
|
+
} else if (referRect.left > targetRect.right) {
|
|
475
|
+
//左侧
|
|
476
|
+
return {
|
|
477
|
+
start: { x: referRect.left, y: referRect.bottom },
|
|
478
|
+
end: { x: referRect.left, y: targetRect.bottom },
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
} else if (
|
|
482
|
+
referRect.bottom > targetRect.bottom &&
|
|
483
|
+
referRect.top <= targetRect.bottom
|
|
484
|
+
) {
|
|
485
|
+
//refer下方
|
|
486
|
+
if (referRect.right < targetRect.left) {
|
|
487
|
+
//右侧
|
|
488
|
+
return {
|
|
489
|
+
start: { x: referRect.right, y: targetRect.top },
|
|
490
|
+
end: { x: referRect.right, y: referRect.top },
|
|
491
|
+
}
|
|
492
|
+
} else if (referRect.left > targetRect.right) {
|
|
493
|
+
//左侧
|
|
494
|
+
return {
|
|
495
|
+
start: { x: referRect.left, y: targetRect.top },
|
|
496
|
+
end: { x: referRect.left, y: referRect.top },
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
}
|
|
417
500
|
}
|
|
418
|
-
exports.calcExtendsLineSegmentOfRect = calcExtendsLineSegmentOfRect
|
|
501
|
+
exports.calcExtendsLineSegmentOfRect = calcExtendsLineSegmentOfRect
|
|
419
502
|
function calcOffsetOfSnapLineSegmentToEdge(line, current) {
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
503
|
+
const edges = calcEdgeLinesOfRect(current)
|
|
504
|
+
const isVerticalLine = line.start.x === line.end.x
|
|
505
|
+
if (isVerticalLine) {
|
|
506
|
+
return { x: calcMinDistanceValue(edges.x, line.start.x) - current.x, y: 0 }
|
|
507
|
+
}
|
|
508
|
+
function calcEdgeLinesOfRect(rect) {
|
|
509
|
+
return {
|
|
510
|
+
x: [rect.x, rect.x + rect.width / 2, rect.x + rect.width],
|
|
511
|
+
y: [rect.y, rect.y + rect.height / 2, rect.y + rect.height],
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
function calcMinDistanceValue(edges, targetValue) {
|
|
515
|
+
let minDistance = Infinity,
|
|
516
|
+
minDistanceIndex = -1
|
|
517
|
+
for (let i = 0; i < edges.length; i++) {
|
|
518
|
+
const distance = Math.abs(edges[i] - targetValue)
|
|
519
|
+
if (minDistance > distance) {
|
|
520
|
+
minDistance = distance
|
|
521
|
+
minDistanceIndex = i
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return edges[minDistanceIndex]
|
|
525
|
+
}
|
|
526
|
+
return { x: 0, y: calcMinDistanceValue(edges.y, line.start.y) - current.y }
|
|
443
527
|
}
|
|
444
|
-
exports.calcOffsetOfSnapLineSegmentToEdge = calcOffsetOfSnapLineSegmentToEdge
|
|
528
|
+
exports.calcOffsetOfSnapLineSegmentToEdge = calcOffsetOfSnapLineSegmentToEdge
|
|
445
529
|
function calcDistanceOfSnapLineToEdges(line, edges) {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
}
|
|
466
|
-
return distance;
|
|
530
|
+
let distance = Infinity
|
|
531
|
+
if (line?.start?.y === line?.end?.y) {
|
|
532
|
+
edges.h.forEach((target) => {
|
|
533
|
+
const _distance = Math.abs(target.start.y - line.start.y)
|
|
534
|
+
if (_distance < distance) {
|
|
535
|
+
distance = _distance
|
|
536
|
+
}
|
|
537
|
+
})
|
|
538
|
+
} else if (line?.start?.x === line?.end?.x) {
|
|
539
|
+
edges.v.forEach((target) => {
|
|
540
|
+
const _distance = Math.abs(target.start.x - line.start.x)
|
|
541
|
+
if (_distance < distance) {
|
|
542
|
+
distance = _distance
|
|
543
|
+
}
|
|
544
|
+
})
|
|
545
|
+
} else {
|
|
546
|
+
throw new Error('can not calculate slash distance')
|
|
547
|
+
}
|
|
548
|
+
return distance
|
|
467
549
|
}
|
|
468
|
-
exports.calcDistanceOfSnapLineToEdges = calcDistanceOfSnapLineToEdges
|
|
550
|
+
exports.calcDistanceOfSnapLineToEdges = calcDistanceOfSnapLineToEdges
|
|
469
551
|
function calcCombineSnapLineSegment(target, source) {
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
552
|
+
if (target.start.x === target.end.x) {
|
|
553
|
+
return new LineSegment(
|
|
554
|
+
new Point(
|
|
555
|
+
target.start.x,
|
|
556
|
+
target.start.y > source.start.y ? source.start.y : target.start.y
|
|
557
|
+
),
|
|
558
|
+
new Point(
|
|
559
|
+
target.start.x,
|
|
560
|
+
target.end.y > source.end.y ? target.end.y : source.end.y
|
|
561
|
+
)
|
|
562
|
+
)
|
|
563
|
+
}
|
|
564
|
+
return new LineSegment(
|
|
565
|
+
new Point(
|
|
566
|
+
target.start.x > source.start.x ? source.start.x : target.start.x,
|
|
567
|
+
target.start.y
|
|
568
|
+
),
|
|
569
|
+
new Point(
|
|
570
|
+
target.end.x > source.end.x ? target.end.x : source.end.x,
|
|
571
|
+
target.end.y
|
|
572
|
+
)
|
|
573
|
+
)
|
|
474
574
|
}
|
|
475
|
-
exports.calcCombineSnapLineSegment = calcCombineSnapLineSegment
|
|
575
|
+
exports.calcCombineSnapLineSegment = calcCombineSnapLineSegment
|
|
476
576
|
function calcClosestEdges(line, edges) {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
}
|
|
500
|
-
return [distance, result];
|
|
577
|
+
let result
|
|
578
|
+
let distance = Infinity
|
|
579
|
+
if (line?.start?.y === line?.end?.y) {
|
|
580
|
+
edges.h.forEach((target) => {
|
|
581
|
+
const _distance = Math.abs(target.start.y - line.start.y)
|
|
582
|
+
if (_distance < distance) {
|
|
583
|
+
distance = _distance
|
|
584
|
+
result = target
|
|
585
|
+
}
|
|
586
|
+
})
|
|
587
|
+
} else if (line?.start?.x === line?.end?.x) {
|
|
588
|
+
edges.v.forEach((target) => {
|
|
589
|
+
const _distance = Math.abs(target.start.x - line.start.x)
|
|
590
|
+
if (_distance < distance) {
|
|
591
|
+
distance = _distance
|
|
592
|
+
result = target
|
|
593
|
+
}
|
|
594
|
+
})
|
|
595
|
+
} else {
|
|
596
|
+
throw new Error('can not calculate slash distance')
|
|
597
|
+
}
|
|
598
|
+
return [distance, result]
|
|
501
599
|
}
|
|
502
|
-
exports.calcClosestEdges = calcClosestEdges
|
|
600
|
+
exports.calcClosestEdges = calcClosestEdges
|