UNPKG

3.44 kBJavaScriptView Raw
1import { DIRECTION } from '../constant';
2/**
3 * @ignore
4 * 方位常量转实际的 bbox 位置大小
5 * @param parentBBox
6 * @param bbox
7 * @param direction
8 */
9export function directionToPosition(parentBBox, bbox, direction) {
10 if (direction === DIRECTION.TOP) {
11 return [parentBBox.minX + parentBBox.width / 2 - bbox.width / 2, parentBBox.minY];
12 }
13 if (direction === DIRECTION.BOTTOM) {
14 return [parentBBox.minX + parentBBox.width / 2 - bbox.width / 2, parentBBox.maxY - bbox.height];
15 }
16 if (direction === DIRECTION.LEFT) {
17 return [parentBBox.minX, parentBBox.minY + parentBBox.height / 2 - bbox.height / 2];
18 }
19 if (direction === DIRECTION.RIGHT) {
20 return [parentBBox.maxX - bbox.width, parentBBox.minY + parentBBox.height / 2 - bbox.height / 2];
21 }
22 if (direction === DIRECTION.TOP_LEFT || direction === DIRECTION.LEFT_TOP) {
23 return [parentBBox.tl.x, parentBBox.tl.y];
24 }
25 if (direction === DIRECTION.TOP_RIGHT || direction === DIRECTION.RIGHT_TOP) {
26 return [parentBBox.tr.x - bbox.width, parentBBox.tr.y];
27 }
28 if (direction === DIRECTION.BOTTOM_LEFT || direction === DIRECTION.LEFT_BOTTOM) {
29 return [parentBBox.bl.x, parentBBox.bl.y - bbox.height];
30 }
31 if (direction === DIRECTION.BOTTOM_RIGHT || direction === DIRECTION.RIGHT_BOTTOM) {
32 return [parentBBox.br.x - bbox.width, parentBBox.br.y - bbox.height];
33 }
34 return [0, 0];
35}
36/**
37 * get direction after coordinate transpose
38 * @param direction
39 * @param coordinate
40 * @returns direction after transpose or not
41 */
42function getTransposedDirection(direction, coordinate) {
43 if (coordinate.isTransposed) {
44 switch (direction) {
45 case DIRECTION.BOTTOM:
46 return DIRECTION.LEFT;
47 case DIRECTION.LEFT:
48 return DIRECTION.BOTTOM;
49 case DIRECTION.RIGHT:
50 return DIRECTION.TOP;
51 case DIRECTION.TOP:
52 return DIRECTION.RIGHT;
53 }
54 }
55 return direction;
56}
57function reflectX(direct) {
58 if (direct === DIRECTION.LEFT) {
59 return DIRECTION.RIGHT;
60 }
61 if (direct === DIRECTION.RIGHT) {
62 return DIRECTION.LEFT;
63 }
64 return direct;
65}
66function reflectY(direct) {
67 if (direct === DIRECTION.TOP) {
68 return DIRECTION.BOTTOM;
69 }
70 if (direct === DIRECTION.BOTTOM) {
71 return DIRECTION.TOP;
72 }
73 return direct;
74}
75/**
76 * get direction after coordinate.scale
77 * @param direction
78 * @param coordinate
79 */
80function getScaleDirection(direction, coordinate) {
81 var x = coordinate.matrix[0];
82 var y = coordinate.matrix[4];
83 var d = direction;
84 if (x < 0) {
85 d = reflectX(d);
86 }
87 if (y < 0) {
88 d = reflectY(d);
89 }
90 return d;
91}
92/**
93 *
94 * @param direction
95 * @param coordinate
96 */
97function getReflectDirection(direction, coordinate) {
98 var d = direction;
99 if (coordinate.isReflect('x')) {
100 d = reflectX(d);
101 }
102 if (coordinate.isReflect('y')) {
103 d = reflectY(d);
104 }
105 return d;
106}
107/**
108 * @ignore
109 * get direction after coordinate translate
110 * @param direction
111 * @param coordinate
112 */
113export function getTranslateDirection(direction, coordinate) {
114 var d = direction;
115 d = getTransposedDirection(d, coordinate);
116 d = getScaleDirection(d, coordinate);
117 d = getReflectDirection(d, coordinate);
118 return d;
119}
120//# sourceMappingURL=direction.js.map
\No newline at end of file