11export interface FigmaSquircleParams {
2- radius : number
3- smoothing : number
2+ cornerRadius : number
3+ cornerSmoothing : number
44 width : number
55 height : number
66}
77
88export function getSvgPath ( {
9- radius ,
10- smoothing ,
9+ cornerRadius ,
10+ cornerSmoothing ,
1111 width,
1212 height,
1313} : FigmaSquircleParams ) {
14- radius = Math . min ( radius , width / 2 , height / 2 )
14+ cornerRadius = Math . min ( cornerRadius , width / 2 , height / 2 )
1515
1616 // Keeping these variable names the same as the original code for now
1717 // https://github.com/MartinRGB/Figma_Squircles_Approximation/blob/bf29714aab58c54329f3ca130ffa16d39a2ff08c/js/rounded-corners.js#L64
1818 const shortest_l = Math . min ( width , height )
1919
20- const p = Math . min ( shortest_l / 2 , ( 1 + smoothing ) * radius )
20+ const p = Math . min ( shortest_l / 2 , ( 1 + cornerSmoothing ) * cornerRadius )
2121
2222 let angle_alpha : number , angle_beta : number
23- if ( radius > shortest_l / 4 ) {
24- const change_percentage = ( radius - shortest_l / 4 ) / ( shortest_l / 4 )
25- angle_beta = 90 * ( 1 - smoothing * ( 1 - change_percentage ) )
26- angle_alpha = 45 * smoothing * ( 1 - change_percentage )
23+ if ( cornerRadius > shortest_l / 4 ) {
24+ const change_percentage = ( cornerRadius - shortest_l / 4 ) / ( shortest_l / 4 )
25+ angle_beta = 90 * ( 1 - cornerSmoothing * ( 1 - change_percentage ) )
26+ angle_alpha = 45 * cornerSmoothing * ( 1 - change_percentage )
2727 } else {
28- angle_beta = 90 * ( 1 - smoothing )
29- angle_alpha = 45 * smoothing
28+ angle_beta = 90 * ( 1 - cornerSmoothing )
29+ angle_alpha = 45 * cornerSmoothing
3030 }
3131
3232 const angle_theta = ( 90 - angle_beta ) / 2
3333
3434 const d_div_c = Math . tan ( toRadians ( angle_alpha ) )
35- const h_longest = radius * Math . tan ( toRadians ( angle_theta / 2 ) )
35+ const h_longest = cornerRadius * Math . tan ( toRadians ( angle_theta / 2 ) )
3636
37- const l = Math . sin ( toRadians ( angle_beta / 2 ) ) * radius * Math . pow ( 2 , 1 / 2 )
37+ const l =
38+ Math . sin ( toRadians ( angle_beta / 2 ) ) * cornerRadius * Math . pow ( 2 , 1 / 2 )
3839 const c = h_longest * Math . cos ( toRadians ( angle_alpha ) )
3940 const d = c * d_div_c
4041 const b = ( p - l - ( 1 + d_div_c ) * c ) / 3
@@ -45,31 +46,31 @@ export function getSvgPath({
4546 L ${ Math . max ( width / 2 , width - p ) } 0
4647 C ${ width - ( p - a ) } 0 ${ width - ( p - a - b ) } 0 ${ width -
4748 ( p - a - b - c ) } ${ d }
48- a ${ radius } ${ radius } 0 0 1 ${ l } ${ l }
49+ a ${ cornerRadius } ${ cornerRadius } 0 0 1 ${ l } ${ l }
4950 C ${ width } ${ p - a - b }
5051 ${ width } ${ p - a }
5152 ${ width } ${ Math . min ( height / 2 , p ) }
5253 L ${ width } ${ Math . max ( height / 2 , height - p ) }
5354 C ${ width } ${ height - ( p - a ) }
5455 ${ width } ${ height - ( p - a - b ) }
5556 ${ width - d } ${ height - ( p - a - b - c ) }
56- a ${ radius } ${ radius } 0 0 1 -${ l } ${ l }
57+ a ${ cornerRadius } ${ cornerRadius } 0 0 1 -${ l } ${ l }
5758 C ${ width - ( p - a - b ) } ${ height }
5859 ${ width - ( p - a ) } ${ height }
5960 ${ Math . max ( width / 2 , width - p ) } ${ height }
6061 L ${ Math . min ( width / 2 , p ) } ${ height }
6162 C ${ p - a } ${ height }
6263 ${ p - a - b } ${ height }
6364 ${ p - a - b - c } ${ height - d }
64- a ${ radius } ${ radius } 0 0 1 -${ l } -${ l }
65+ a ${ cornerRadius } ${ cornerRadius } 0 0 1 -${ l } -${ l }
6566 C 0 ${ height - ( p - a - b ) }
6667 0 ${ height - ( p - a ) }
6768 0 ${ Math . max ( height / 2 , height - p ) }
6869 L 0 ${ Math . min ( height / 2 , p ) }
6970 C 0 ${ p - a }
7071 0 ${ p - a - b }
7172 ${ d } ${ p - a - b - c }
72- a ${ radius } ${ radius } 0 0 1 ${ l } -${ l }
73+ a ${ cornerRadius } ${ cornerRadius } 0 0 1 ${ l } -${ l }
7374 C ${ p - a - b } 0
7475 ${ p - a } 0
7576 ${ + Math . min ( width / 2 , p ) } 0
0 commit comments