forked from motiondeveloper/eBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheBox.jsx
More file actions
202 lines (202 loc) 路 8.72 KB
/
Copy patheBox.jsx
File metadata and controls
202 lines (202 loc) 路 8.72 KB
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
{
// Creating layer and property mocks
createBox({ size = [100, 100], position = [0, 0], rounding = [0, 0, 0, 0], anchor = 'center', isClosed = true,
// From https://spencermortensen.com/articles/bezier-circle/
tangentMult = 0.55, }) {
const tempPoints = sizeToPoints(size);
const centerPosition = anchorPositionToCenterPosition(position, size, anchor);
const centeredPoints = movePoints(tempPoints, [0, 0], centerPosition);
const compPositionPoints = pointsToComp(centeredPoints);
let boxPoints = compPositionPoints;
function scaleBoxPoints(scale, anchor) {
// Remap scale to [0..1]
const normalizedScale = scale.map((scale) => scale / 100);
const [pTl, pTr, pBr, pBl] = boxPoints;
const boxWidth = pTr[0] - pTl[0];
const boxHeight = pBr[1] - pTl[1];
const boxCenter = thisLayer.add(pTl, [boxWidth / 2, boxHeight / 2]);
// Get offsets from center for each point
const deltasFromCenter = boxPoints.map((point) => thisLayer.sub(point, boxCenter));
boxPoints = getScaledDeltas().map((delta) => thisLayer.add(boxCenter, delta));
function getScaledDeltas() {
const [dTl, dTr, dBr, dBl] = deltasFromCenter;
const [sx, sy] = normalizedScale;
/**
* Used to multiply a delta so it scales not to zero,
* but to -1, for scaling from corner anchors
*/
const doubleMult = (val, mul) => val * (2 * mul - 1);
if (anchor === 'topLeft') {
const sTl = dTl;
const sTr = [doubleMult(dTr[0], sx), dTr[1]];
const sBr = [doubleMult(dBr[0], sx), doubleMult(dBr[1], sy)];
const sBl = [dBl[0], doubleMult(dBl[1], sy)];
return [sTl, sTr, sBr, sBl];
}
if (anchor === 'topRight') {
const sTl = [doubleMult(dTl[0], sx), dTl[1]];
const sTr = dTr;
const sBr = [dBr[0], doubleMult(dBr[1], sy)];
const sBl = [doubleMult(dBl[0], sx), doubleMult(dBl[1], sy)];
return [sTl, sTr, sBr, sBl];
}
if (anchor === 'topCenter') {
const sTl = [dTl[0] * sx, dTl[1]];
const sTr = [dTr[0] * sx, dTr[1]];
const sBr = [dBr[0] * sx, doubleMult(dBr[1], sy)];
const sBl = [dBl[0] * sx, doubleMult(dBl[1], sy)];
return [sTl, sTr, sBr, sBl];
}
if (anchor === 'bottomRight') {
const sTl = [doubleMult(dTl[0], sx), doubleMult(dTl[1], sy)];
const sTr = [dTr[0], doubleMult(dTr[1], sy)];
const sBr = dBr;
const sBl = [doubleMult(dBl[0], sx), dBl[1]];
return [sTl, sTr, sBr, sBl];
}
if (anchor === 'bottomLeft') {
const sTl = [dTl[0], doubleMult(dTl[1], sy)];
const sTr = [doubleMult(dTr[0], sx), doubleMult(dTr[1], sy)];
const sBr = [doubleMult(dBr[0], sx), dBr[1]];
const sBl = dBl;
return [sTl, sTr, sBr, sBl];
}
if (anchor === 'bottomCenter') {
const sTl = [dTl[0] * sx, doubleMult(dTl[1], sy)];
const sTr = [dTr[0] * sx, doubleMult(dTr[1], sy)];
const sBr = [dBr[0] * sx, dBr[1]];
const sBl = [dBl[0] * sx, dBl[1]];
return [sTl, sTr, sBr, sBl];
}
if (anchor === 'centerLeft') {
const sTl = [dTl[0], dTl[1] * sy];
const sTr = [doubleMult(dTr[0], sx), dTr[1] * sy];
const sBr = [doubleMult(dBr[0], sx), dBr[1] * sy];
const sBl = [dBl[0], dBl[1] * sy];
return [sTl, sTr, sBr, sBl];
}
if (anchor === 'centerRight') {
const sTl = [doubleMult(dTl[0], sx), dTl[1] * sy];
const sTr = [dTr[0], dTr[1] * sy];
const sBr = [dBr[0], dBr[1] * sy];
const sBl = [doubleMult(dBl[0], sx), dBl[1] * sy];
return [sTl, sTr, sBr, sBl];
}
/**
* anchor === 'center'
* no doubling needed, just scale
* each delta
*/
return deltasFromCenter.map((delta) => {
return delta.map((dimension, index) => {
return dimension * normalizedScale[index];
});
});
}
}
function getRoundedPathForPoints(points) {
const [pTl, pTr, pBr, pBl] = points;
const width = pTr[0] - pTl[0];
const height = pBl[1] - pTl[1];
const [rTl, rTr, rBr, rBl] = rounding.map((radius) => {
const minDimension = Math.min(width, height);
return Math.max(Math.min(minDimension / 2, radius), 0);
});
const cornerPoints = [
// Top left
thisLayer.add(pTl, [0, rTl]),
thisLayer.add(pTl, [rTl, 0]),
// Top right
thisLayer.add(pTr, [-rTr, 0]),
thisLayer.add(pTr, [0, rTr]),
// Bottom right
thisLayer.add(pBr, [0, -rBr]),
thisLayer.add(pBr, [-rBr, 0]),
// Bottom left
thisLayer.add(pBl, [rBl, 0]),
thisLayer.add(pBl, [0, -rBl]),
];
const inTangents = [
// Top left
[0, 0],
[-rTl, 0],
// Top right
[0, 0],
[0, -rTr],
// Bottom right
[0, 0],
[rBr, 0],
// Bottom left
[0, 0],
[0, rBl],
].map((p) => thisLayer.mul(p, tangentMult));
const outTangents = [
// Top left
[0, -rTl],
[0, 0],
// Top right
[rTr, 0],
[0, 0],
// Bottom right
[0, rBr],
[0, 0],
// Bottom left
[-rBl, 0],
[0, 0],
].map((p) => thisLayer.mul(p, tangentMult));
return thisProperty.createPath(cornerPoints, inTangents, outTangents, isClosed);
}
return {
setScale: scaleBoxPoints,
getPath: () => getRoundedPathForPoints(boxPoints),
};
function anchorPositionToCenterPosition(position, size, anchor) {
const positionCalculations = {
topLeft: () => [
position[0] + size[0] / 2,
position[1] + size[1] / 2,
],
topCenter: () => [position[0], position[1] + size[1] / 2],
topRight: () => [
position[0] - size[0] / 2,
position[1] + size[1] / 2,
],
centerLeft: () => [position[0] + size[1] / 2, position[1]],
center: () => position,
centerRight: () => [position[0] - size[1] / 2, position[1]],
bottomLeft: () => [
position[0] + size[0] / 2,
position[1] - size[1] / 2,
],
bottomCenter: () => [position[0], position[1] - size[1] / 2],
bottomRight: () => [
position[0] - size[0] / 2,
position[1] - size[1] / 2,
],
};
return positionCalculations[anchor]();
}
function 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],
];
}
function 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];
});
});
}
function pointsToComp(points) {
return points.map((point) => thisLayer.fromComp(point));
}
},
version: '2.0.0',
}