@@ -28,77 +28,71 @@ function createBox({
2828 rounding = [ 0 , 0 , 0 , 0 ] ,
2929 anchor = 'center' ,
3030 isClosed = true ,
31- tangentMult = 0.5 ,
31+ // From https://spencermortensen.com/articles/bezier-circle/
32+ tangentMult = 0.55 ,
3233} : BoxProps ) {
33- const pointOrder : Anchor [ ] = [
34- 'topLeft' ,
35- 'topRight' ,
36- 'bottomRight' ,
37- 'bottomLeft' ,
38- ] ;
39-
40- function positionToCenter (
41- position : Vector2D ,
42- size : Vector2D ,
43- anchor : Anchor
44- ) : Vector2D {
45- const positionCalculations = {
46- center : ( ) : Vector2D => position ,
47- topLeft : ( ) : Vector2D => [
48- position [ 0 ] + size [ 0 ] / 2 ,
49- position [ 1 ] + size [ 1 ] / 2 ,
50- ] ,
51- topRight : ( ) : Vector2D => [
52- position [ 0 ] - size [ 0 ] / 2 ,
53- position [ 1 ] + size [ 1 ] / 2 ,
54- ] ,
55- bottomLeft : ( ) : Vector2D => [
56- position [ 0 ] + size [ 0 ] / 2 ,
57- position [ 1 ] - size [ 1 ] / 2 ,
58- ] ,
59- bottomRight : ( ) : Vector2D => [
60- position [ 0 ] - size [ 0 ] / 2 ,
61- position [ 1 ] - size [ 1 ] / 2 ,
62- ] ,
63- } ;
34+ const tempPoints = sizeToPoints ( size ) ;
35+ const centerPosition = anchorPositionToCenterPosition ( position , size , anchor ) ;
36+ const centeredPoints = movePoints ( tempPoints , [ 0 , 0 ] , centerPosition ) ;
37+ const compPositionPoints = pointsToComp ( centeredPoints ) ;
6438
65- return positionCalculations [ anchor ] ( ) ;
66- }
39+ let boxPoints = compPositionPoints ;
6740
68- function sizeToPoints ( size : Vector2D ) : Points {
69- return [
70- [ - size [ 0 ] / 2 , - size [ 1 ] / 2 ] ,
71- [ size [ 0 ] / 2 , - size [ 1 ] / 2 ] ,
72- [ size [ 0 ] / 2 , size [ 1 ] / 2 ] ,
73- [ - size [ 0 ] / 2 , size [ 1 ] / 2 ] ,
41+ function scaleBoxPoints ( scale : Vector2D , anchor : Anchor ) : void {
42+ // Remap scale to [0..1]
43+ const normalizedScale : Vector2D = scale . map (
44+ scale => scale / 100
45+ ) as Vector2D ;
46+
47+ // Get index of anchor point
48+ const pointOrder : Anchor [ ] = [
49+ 'topLeft' ,
50+ 'topRight' ,
51+ 'bottomRight' ,
52+ 'bottomLeft' ,
7453 ] ;
75- }
54+ const anchorPointIndex : number = pointOrder . indexOf ( anchor ) ;
55+ const anchorPoint : Vector2D = boxPoints [ anchorPointIndex ] ;
7656
77- function movePoints (
78- points : Points ,
79- oldPosition : Vector2D ,
80- newPosition : Vector2D
81- ) : Points {
82- const positionDelta : Vector2D = newPosition . map (
83- ( dimension , dimensionIndex ) : number => {
84- return dimension - oldPosition [ dimensionIndex ] ;
85- }
86- ) as Vector2D ;
57+ // Calculate distance from anchor point
58+ const pointDeltas : Points = boxPoints . map ( point => {
59+ return point . map ( ( dimension , dimensionIndex ) : number => {
60+ return dimension - anchorPoint [ dimensionIndex ] ;
61+ } ) as Vector2D ;
62+ } ) as Points ;
8763
88- return points . map (
89- ( point : Vector2D ) : Vector2D => {
90- return point . map ( ( dimension , dimensionIndex ) => {
91- return dimension + positionDelta [ dimensionIndex ] ;
64+ // Scale the point deltas according to input scale
65+ const scaledPointDeltas : Points = pointDeltas . map (
66+ ( point ) : Vector2D => {
67+ return point . map ( ( dimension , dimensionIndex ) : number => {
68+ return dimension * normalizedScale [ dimensionIndex ] ;
9269 } ) as Vector2D ;
9370 }
9471 ) as Points ;
95- }
9672
97- function pointsToComp ( points : Points ) : Points {
98- return points . map ( ( point ) : Vector2D => thisLayer . fromComp ( point ) ) as Points ;
73+ const scaledPoints : Points = boxPoints . map (
74+ ( point , pointIndex ) : Vector2D => {
75+ if ( pointIndex !== anchorPointIndex ) {
76+ // If not the anchor point
77+ // Create the point from the scaledPointDelta
78+ return point . map ( ( pointDimension , dimensionIndex ) : number => {
79+ return (
80+ anchorPoint [ dimensionIndex ] +
81+ scaledPointDeltas [ pointIndex ] [ dimensionIndex ]
82+ ) ;
83+ } ) as Vector2D ;
84+ } else {
85+ // If the anchor point
86+ // Return as is
87+ return point ;
88+ }
89+ }
90+ ) as Points ;
91+
92+ boxPoints = scaledPoints ;
9993 }
10094
101- function pointsToPath ( points : Points , rounding : Rounding , isClosed : boolean ) {
95+ function getRoundedPathForPoints ( points : Points ) {
10296 const [ pTl , pTr , pBr , pBl ] = points ;
10397
10498 const width = pTr [ 0 ] - pTl [ 0 ] ;
@@ -162,86 +156,71 @@ function createBox({
162156 ) ;
163157 }
164158
165- const centerPosition = positionToCenter ( position , size , anchor ) ;
166- interface OutputBox extends BoxProps {
167- centerPosition : Vector2D ;
168- }
159+ return {
160+ setScale : scaleBoxPoints ,
161+ getPath : ( ) => getRoundedPathForPoints ( boxPoints ) ,
162+ } ;
169163
170- let boxPoints : Points = createPointsFromBoxProps ( {
171- size,
172- position,
173- anchor,
174- isClosed,
175- centerPosition,
176- rounding,
177- } ) ;
178-
179- function getBoxPath ( ) {
180- return pointsToPath ( boxPoints , rounding , isClosed ) ;
181- }
164+ function anchorPositionToCenterPosition (
165+ position : Vector2D ,
166+ size : Vector2D ,
167+ anchor : Anchor
168+ ) : Vector2D {
169+ const positionCalculations = {
170+ center : ( ) : Vector2D => position ,
171+ topLeft : ( ) : Vector2D => [
172+ position [ 0 ] + size [ 0 ] / 2 ,
173+ position [ 1 ] + size [ 1 ] / 2 ,
174+ ] ,
175+ topRight : ( ) : Vector2D => [
176+ position [ 0 ] - size [ 0 ] / 2 ,
177+ position [ 1 ] + size [ 1 ] / 2 ,
178+ ] ,
179+ bottomLeft : ( ) : Vector2D => [
180+ position [ 0 ] + size [ 0 ] / 2 ,
181+ position [ 1 ] - size [ 1 ] / 2 ,
182+ ] ,
183+ bottomRight : ( ) : Vector2D => [
184+ position [ 0 ] - size [ 0 ] / 2 ,
185+ position [ 1 ] - size [ 1 ] / 2 ,
186+ ] ,
187+ } ;
182188
183- function createPointsFromBoxProps (
184- boxProps : Omit < OutputBox , 'tangentMult' >
185- ) : Points {
186- const points = sizeToPoints ( boxProps . size ) ;
187- const centeredPoints = movePoints ( points , [ 0 , 0 ] , boxProps . centerPosition ) ;
188- const compPositionPoints = pointsToComp ( centeredPoints ) ;
189+ return positionCalculations [ anchor ] ( ) ;
190+ }
189191
190- return compPositionPoints ;
192+ function sizeToPoints ( size : Vector2D ) : Points {
193+ return [
194+ [ - size [ 0 ] / 2 , - size [ 1 ] / 2 ] ,
195+ [ size [ 0 ] / 2 , - size [ 1 ] / 2 ] ,
196+ [ size [ 0 ] / 2 , size [ 1 ] / 2 ] ,
197+ [ - size [ 0 ] / 2 , size [ 1 ] / 2 ] ,
198+ ] ;
191199 }
192200
193- function scalePoints ( scale : Vector2D = [ 100 , 100 ] , anchor : Anchor ) : void {
194- // Remap scale to [0..1]
195- const normalizedScale : Vector2D = scale . map (
196- scale => scale / 100
201+ function movePoints (
202+ points : Points ,
203+ oldPosition : Vector2D ,
204+ newPosition : Vector2D
205+ ) : Points {
206+ const positionDelta : Vector2D = newPosition . map (
207+ ( dimension , dimensionIndex ) : number => {
208+ return dimension - oldPosition [ dimensionIndex ] ;
209+ }
197210 ) as Vector2D ;
198211
199- // Get index of anchor point
200- const anchorPointIndex : number = pointOrder . indexOf ( anchor ) ;
201- const anchorPoint : Vector2D = boxPoints [ anchorPointIndex ] ;
202-
203- // Calculate distance from anchor point
204- const pointDeltas : Points = boxPoints . map ( point => {
205- return point . map ( ( dimension , dimensionIndex ) : number => {
206- return dimension - anchorPoint [ dimensionIndex ] ;
207- } ) as Vector2D ;
208- } ) as Points ;
209-
210- // Scale the point deltas according to input scale
211- const scaledPointDeltas : Points = pointDeltas . map (
212- ( point ) : Vector2D => {
213- return point . map ( ( dimension , dimensionIndex ) : number => {
214- return dimension * normalizedScale [ dimensionIndex ] ;
212+ return points . map (
213+ ( point : Vector2D ) : Vector2D => {
214+ return point . map ( ( dimension , dimensionIndex ) => {
215+ return dimension + positionDelta [ dimensionIndex ] ;
215216 } ) as Vector2D ;
216217 }
217218 ) as Points ;
218-
219- const scaledPoints : Points = boxPoints . map (
220- ( point , pointIndex ) : Vector2D => {
221- if ( pointIndex !== anchorPointIndex ) {
222- // If not the anchor point
223- // Create the point from the scaledPointDelta
224- return point . map ( ( pointDimension , dimensionIndex ) : number => {
225- return (
226- anchorPoint [ dimensionIndex ] +
227- scaledPointDeltas [ pointIndex ] [ dimensionIndex ]
228- ) ;
229- } ) as Vector2D ;
230- } else {
231- // If the anchor point
232- // Return as is
233- return point ;
234- }
235- }
236- ) as Points ;
237-
238- boxPoints = scaledPoints ;
239219 }
240220
241- return {
242- setScale : scalePoints ,
243- getPath : getBoxPath ,
244- } ;
221+ function pointsToComp ( points : Points ) : Points {
222+ return points . map ( ( point ) : Vector2D => thisLayer . fromComp ( point ) ) as Points ;
223+ }
245224}
246225
247226const version : string = '_npmVersion' ;
0 commit comments