-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheBox.jsx
126 lines (103 loc) · 3.74 KB
/
eBox.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
{
"createBox": function(boxProps = {}, layer = thisLayer, property = thisProperty) {
const pointOrder = ['topLeft', 'topRight', 'bottomRight', 'bottomLeft'];
const positionToCenter = (position, size, anchor) => {
const positionCalculations = {
'center': () => position,
'topLeft': () => [position[0] + size[0]/2, position[1] + size[1]/2],
'topRight': () => [position[0] -size[0]/2, position[1] + size[1]/2],
'bottomLeft': () => [position[0] + size[0]/2, position[1] - size[1]/2],
'bottomRight': () => [position[0] - size[0]/2, position[1] - size[1]/2],
}
return positionCalculations[anchor]();
}
const sizeToPoints = (size) => {
return [
[-size[0]/2, -size[1]/2],
[size[0]/2, -size[1]/2],
[size[0]/2, size[1]/2],
[-size[0]/2, size[1]/2]
];
}
const movePoints = (points, oldPosition, newPosition) => {
const positionDelta = newPosition.map((dimension, dimensionIndex) => {
return dimension - oldPosition[dimensionIndex];
});
return points.map(
(point) => {
return point.map(
(dimension, dimensionIndex) => {
return dimension + positionDelta[dimensionIndex];
});
}
);
};
const pointsToComp = points => points.map(point => layer.fromCompToSurface(point));
const pointsToPath = (points, isClosed) => property.createPath(points, [], [], isClosed);
function createPointsFromBoxProps(boxProps) {
const points = sizeToPoints(boxProps.size);
const centeredPoints = movePoints(points, [0, 0], boxProps.centerPosition);
const compPositionPoints = pointsToComp(centeredPoints);
return compPositionPoints;
}
// Destructuring boxProps, with defaults
const {
size = [800, 200],
position = [960, 540],
anchor = 'bottomRight',
isClosed = true,
} = boxProps;
const centerPosition = positionToCenter(position, size, anchor);
let boxPoints = createPointsFromBoxProps({size, position, anchor, isClosed, centerPosition});
const getBoxPath = () => { return pointsToPath(boxPoints, isClosed) };
const scalePoints = function (scale = [100, 100], anchor) {
// Remap scale to [0..1]
const normalizedScale = scale.map(scale => scale / 100);
// Get index of anchor point
const anchorPointIndex = pointOrder.indexOf(anchor);
const anchorPoint = boxPoints[anchorPointIndex];
// Calculate distance from anchor point
const pointDeltas = boxPoints.map(
(point) => {
return point.map(
(dimension, dimensionIndex) => {
return dimension - anchorPoint[dimensionIndex];
}
);
}
);
// Scale the point deltas according to input scale
const scaledPointDeltas = pointDeltas.map(
(point) => {
return point.map(
(dimension, dimensionIndex) => {
return dimension * normalizedScale[dimensionIndex];
}
);
}
);
const scaledPoints = boxPoints.map(
(point, pointIndex) => {
if (pointIndex !== anchorPointIndex) {
// If not the anchor point
// Create the point from the scaledPointDelta
return point.map(
(_pointDimension, dimensionIndex) => {
return anchorPoint[dimensionIndex] + scaledPointDeltas[pointIndex][dimensionIndex];
}
);
} else {
// If the anchor point
// Return as is
return point;
}
}
);
boxPoints = scaledPoints;
}
return {
setScale: scalePoints,
getPath: getBoxPath,
}
}
}