1- import {
2- Layer ,
3- PathProperty ,
4- Vector2D ,
5- Points ,
6- PathValue ,
7- SourceText ,
8- } from 'expression-globals-typescript' ;
1+ import { Layer , PathProperty } from 'expression-globals-typescript' ;
92
103// Creating layer and property mocks
114const thisLayer = new Layer ( ) ;
12- const thisProperty = new PathProperty < PathValue > ( [ [ 0 , 0 ] ] ) ;
5+ const thisProperty = new PathProperty ( [ [ 0 , 0 ] ] ) ;
136
147// eBox types
158type Anchor = 'topLeft' | 'topRight' | 'bottomRight' | 'bottomLeft' | 'center' ;
9+ type Rounding = [ number , number , number , number ] ;
10+ type Vector2D = [ number , number ] ;
11+ type Points = Array < Vector2D > ;
12+
1613interface BoxProps {
1714 size : Vector2D ;
1815 position : Vector2D ;
1916 anchor : Anchor ;
2017 isClosed : boolean ;
18+ rounding : Rounding ;
19+ tangentMult : number ;
2120}
2221
2322function createBox ( {
2423 size = [ 100 , 100 ] ,
2524 position = [ 0 , 0 ] ,
25+ rounding = [ 0 , 0 , 0 , 0 ] ,
2626 anchor = 'center' ,
2727 isClosed = true ,
28+ tangentMult = 0.5 ,
2829} : BoxProps ) {
2930 const pointOrder : Anchor [ ] = [
3031 'topLeft' ,
@@ -69,6 +70,7 @@ function createBox({
6970 [ - size [ 0 ] / 2 , size [ 1 ] / 2 ] ,
7071 ] ;
7172 }
73+
7274 function movePoints (
7375 points : Points ,
7476 oldPosition : Vector2D ,
@@ -92,26 +94,91 @@ function createBox({
9294 function pointsToComp ( points : Points ) : Points {
9395 return points . map ( ( point ) : Vector2D => thisLayer . fromComp ( point ) ) as Points ;
9496 }
95- function pointsToPath ( points : Points , isClosed : boolean ) {
96- return thisProperty . createPath ( points , [ ] , [ ] , isClosed ) ;
97+
98+ function pointsToPath ( points : Points , rounding : Rounding , isClosed : boolean ) {
99+ const [ pTl , pTr , pBr , pBl ] = points ;
100+
101+ const width = pTr [ 0 ] - pTl [ 0 ] ;
102+ const height = pBl [ 1 ] - pTl [ 1 ] ;
103+
104+ const [ rTl , rTr , rBr , rBl ] = rounding . map ( radius => {
105+ const minDimension = Math . min ( width , height ) ;
106+ return Math . max ( Math . min ( minDimension / 2 , radius ) , 0 ) ;
107+ } ) ;
108+
109+ const cornerPoints = [
110+ // Top left
111+ thisLayer . add ( pTl , [ 0 , rTl ] ) ,
112+ thisLayer . add ( pTl , [ rTl , 0 ] ) ,
113+ // Top right
114+ thisLayer . add ( pTr , [ - rTr , 0 ] ) ,
115+ thisLayer . add ( pTr , [ 0 , rTr ] ) ,
116+ // Bottom right
117+ thisLayer . add ( pBr , [ 0 , - rBr ] ) ,
118+ thisLayer . add ( pBr , [ - rBr , 0 ] ) ,
119+ // Bottom left
120+ thisLayer . add ( pBl , [ rBl , 0 ] ) ,
121+ thisLayer . add ( pBl , [ 0 , - rBl ] ) ,
122+ ] ;
123+
124+ const inTangents = [
125+ // Top left
126+ [ 0 , 0 ] ,
127+ [ - rTl , 0 ] ,
128+ // Top right
129+ [ 0 , 0 ] ,
130+ [ 0 , - rTr ] ,
131+ // Bottom right
132+ [ 0 , 0 ] ,
133+ [ rBr , 0 ] ,
134+ // Bottom left
135+ [ 0 , 0 ] ,
136+ [ 0 , rBl ] ,
137+ ] . map ( p => thisLayer . mul ( p , tangentMult ) ) ;
138+ const outTangents = [
139+ // Top left
140+ [ 0 , - rTl ] ,
141+ [ 0 , 0 ] ,
142+ // Top right
143+ [ rTr , 0 ] ,
144+ [ 0 , 0 ] ,
145+ // Bottom right
146+ [ 0 , rBr ] ,
147+ [ 0 , 0 ] ,
148+ // Bottom left
149+ [ - rBl , 0 ] ,
150+ [ 0 , 0 ] ,
151+ ] . map ( p => thisLayer . mul ( p , tangentMult ) ) ;
152+
153+ return thisProperty . createPath (
154+ cornerPoints ,
155+ inTangents ,
156+ outTangents ,
157+ isClosed
158+ ) ;
97159 }
98160
99161 const centerPosition = positionToCenter ( position , size , anchor ) ;
100162 interface OutputBox extends BoxProps {
101163 centerPosition : Vector2D ;
102164 }
165+
103166 let boxPoints : Points = createPointsFromBoxProps ( {
104167 size,
105168 position,
106169 anchor,
107170 isClosed,
108171 centerPosition,
172+ rounding,
109173 } ) ;
110174
111175 function getBoxPath ( ) {
112- return pointsToPath ( boxPoints , isClosed ) ;
176+ return pointsToPath ( boxPoints , rounding , isClosed ) ;
113177 }
114- function createPointsFromBoxProps ( boxProps : OutputBox ) : Points {
178+
179+ function createPointsFromBoxProps (
180+ boxProps : Omit < OutputBox , 'tangentMult' >
181+ ) : Points {
115182 const points = sizeToPoints ( boxProps . size ) ;
116183 const centeredPoints = movePoints ( points , [ 0 , 0 ] , boxProps . centerPosition ) ;
117184 const compPositionPoints = pointsToComp ( centeredPoints ) ;
0 commit comments