|
1 | 1 | export interface FigmaSquircleParams { |
2 | | - cornerRadius: number |
| 2 | + cornerRadius?: number |
| 3 | + topLeftCornerRadius?: number |
| 4 | + topRightCornerRadius?: number |
| 5 | + bottomRightCornerRadius?: number |
| 6 | + bottomLeftCornerRadius?: number |
3 | 7 | cornerSmoothing: number |
4 | 8 | width: number |
5 | 9 | height: number |
6 | 10 | } |
7 | 11 |
|
8 | 12 | export function getSvgPath({ |
9 | | - cornerRadius, |
| 13 | + cornerRadius = 0, |
| 14 | + topLeftCornerRadius, |
| 15 | + topRightCornerRadius, |
| 16 | + bottomRightCornerRadius, |
| 17 | + bottomLeftCornerRadius, |
10 | 18 | cornerSmoothing, |
11 | 19 | width, |
12 | 20 | height, |
13 | 21 | }: FigmaSquircleParams) { |
| 22 | + const defaultPathParams = getPathParamsForCorner({ |
| 23 | + width, |
| 24 | + height, |
| 25 | + cornerRadius, |
| 26 | + cornerSmoothing, |
| 27 | + }) |
| 28 | + |
| 29 | + // Most of the time, all corners will have the same radius |
| 30 | + // Instead of calculating path params for all 4 corners, |
| 31 | + // we want to use the default path params whenever possible |
| 32 | + const topLeftPathPathParams = |
| 33 | + topLeftCornerRadius !== undefined |
| 34 | + ? getPathParamsForCorner({ |
| 35 | + width, |
| 36 | + height, |
| 37 | + cornerRadius: topLeftCornerRadius, |
| 38 | + cornerSmoothing, |
| 39 | + }) |
| 40 | + : defaultPathParams |
| 41 | + |
| 42 | + const topRightPathPathParams = |
| 43 | + topRightCornerRadius !== undefined |
| 44 | + ? getPathParamsForCorner({ |
| 45 | + width, |
| 46 | + height, |
| 47 | + cornerRadius: topRightCornerRadius, |
| 48 | + cornerSmoothing, |
| 49 | + }) |
| 50 | + : defaultPathParams |
| 51 | + |
| 52 | + const bottomRightPathPathParams = |
| 53 | + bottomRightCornerRadius !== undefined |
| 54 | + ? getPathParamsForCorner({ |
| 55 | + width, |
| 56 | + height, |
| 57 | + cornerRadius: bottomRightCornerRadius, |
| 58 | + cornerSmoothing, |
| 59 | + }) |
| 60 | + : defaultPathParams |
| 61 | + |
| 62 | + const bottomLeftPathPathParams = |
| 63 | + bottomLeftCornerRadius !== undefined |
| 64 | + ? getPathParamsForCorner({ |
| 65 | + width, |
| 66 | + height, |
| 67 | + cornerRadius: bottomLeftCornerRadius, |
| 68 | + cornerSmoothing, |
| 69 | + }) |
| 70 | + : defaultPathParams |
| 71 | + |
| 72 | + return ` |
| 73 | + ${drawTopRightPath(topRightPathPathParams)} |
| 74 | + ${drawBottomRightPath(bottomRightPathPathParams)} |
| 75 | + ${drawBottomLeftPath(bottomLeftPathPathParams)} |
| 76 | + ${drawTopLeftPath(topLeftPathPathParams)} |
| 77 | + ` |
| 78 | + .replace(/[\t\s\n]+/g, ' ') |
| 79 | + .trim() |
| 80 | +} |
| 81 | + |
| 82 | +function drawTopRightPath({ |
| 83 | + cornerRadius, |
| 84 | + width, |
| 85 | + height, |
| 86 | + a, |
| 87 | + b, |
| 88 | + c, |
| 89 | + d, |
| 90 | + p, |
| 91 | + circularSectionLength, |
| 92 | +}: CornerPathParams) { |
| 93 | + if (cornerRadius) { |
| 94 | + return ` |
| 95 | + M ${Math.max(width / 2, width - p)} 0 |
| 96 | + C ${width - (p - a)} 0 ${width - (p - a - b)} 0 ${width - |
| 97 | + (p - a - b - c)} ${d} |
| 98 | + a ${cornerRadius} ${cornerRadius} 0 0 1 ${circularSectionLength} ${circularSectionLength} |
| 99 | + C ${width} ${p - a - b} |
| 100 | + ${width} ${p - a} |
| 101 | + ${width} ${Math.min(height / 2, p)}` |
| 102 | + } else { |
| 103 | + return `M ${width / 2} 0 |
| 104 | + L ${width} ${0} |
| 105 | + L ${width} ${height / 2}` |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +function drawBottomRightPath({ |
| 110 | + cornerRadius, |
| 111 | + width, |
| 112 | + height, |
| 113 | + a, |
| 114 | + b, |
| 115 | + c, |
| 116 | + d, |
| 117 | + p, |
| 118 | + circularSectionLength, |
| 119 | +}: CornerPathParams) { |
| 120 | + if (cornerRadius) { |
| 121 | + return ` |
| 122 | + L ${width} ${Math.max(height / 2, height - p)} |
| 123 | + C ${width} ${height - (p - a)} |
| 124 | + ${width} ${height - (p - a - b)} |
| 125 | + ${width - d} ${height - (p - a - b - c)} |
| 126 | + a ${cornerRadius} ${cornerRadius} 0 0 1 -${circularSectionLength} ${circularSectionLength} |
| 127 | + C ${width - (p - a - b)} ${height} |
| 128 | + ${width - (p - a)} ${height} |
| 129 | + ${Math.max(width / 2, width - p)} ${height}` |
| 130 | + } else { |
| 131 | + return `L ${width} ${height} |
| 132 | + L ${width / 2} ${height}` |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +function drawBottomLeftPath({ |
| 137 | + cornerRadius, |
| 138 | + width, |
| 139 | + height, |
| 140 | + a, |
| 141 | + b, |
| 142 | + c, |
| 143 | + d, |
| 144 | + p, |
| 145 | + circularSectionLength, |
| 146 | +}: CornerPathParams) { |
| 147 | + if (cornerRadius) { |
| 148 | + return ` |
| 149 | + L ${Math.min(width / 2, p)} ${height} |
| 150 | + C ${p - a} ${height} |
| 151 | + ${p - a - b} ${height} |
| 152 | + ${p - a - b - c} ${height - d} |
| 153 | + a ${cornerRadius} ${cornerRadius} 0 0 1 -${circularSectionLength} -${circularSectionLength} |
| 154 | + C 0 ${height - (p - a - b)} |
| 155 | + 0 ${height - (p - a)} |
| 156 | + 0 ${Math.max(height / 2, height - p)}` |
| 157 | + } else { |
| 158 | + return ` |
| 159 | + L ${0} ${height} |
| 160 | + L ${0} ${height / 2}` |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +function drawTopLeftPath({ |
| 165 | + cornerRadius, |
| 166 | + width, |
| 167 | + height, |
| 168 | + a, |
| 169 | + b, |
| 170 | + c, |
| 171 | + d, |
| 172 | + p, |
| 173 | + circularSectionLength, |
| 174 | +}: CornerPathParams) { |
| 175 | + if (cornerRadius) { |
| 176 | + return ` |
| 177 | + L 0 ${Math.min(height / 2, p)} |
| 178 | + C 0 ${p - a} |
| 179 | + 0 ${p - a - b} |
| 180 | + ${d} ${p - a - b - c} |
| 181 | + a ${cornerRadius} ${cornerRadius} 0 0 1 ${circularSectionLength} -${circularSectionLength} |
| 182 | + C ${p - a - b} 0 |
| 183 | + ${p - a} 0 |
| 184 | + ${+Math.min(width / 2, p)} 0 |
| 185 | + Z` |
| 186 | + } else { |
| 187 | + return `L ${0} ${0} |
| 188 | + Z` |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +interface CornerParams { |
| 193 | + cornerRadius: number |
| 194 | + cornerSmoothing: number |
| 195 | + width: number |
| 196 | + height: number |
| 197 | +} |
| 198 | + |
| 199 | +interface CornerPathParams { |
| 200 | + a: number |
| 201 | + b: number |
| 202 | + c: number |
| 203 | + d: number |
| 204 | + p: number |
| 205 | + cornerRadius: number |
| 206 | + circularSectionLength: number |
| 207 | + width: number |
| 208 | + height: number |
| 209 | +} |
| 210 | + |
| 211 | +function getPathParamsForCorner({ |
| 212 | + cornerRadius, |
| 213 | + cornerSmoothing, |
| 214 | + width, |
| 215 | + height, |
| 216 | +}: CornerParams): CornerPathParams { |
14 | 217 | const maxRadius = Math.min(width, height) / 2 |
15 | 218 | cornerRadius = Math.min(cornerRadius, maxRadius) |
16 | 219 |
|
@@ -56,42 +259,17 @@ export function getSvgPath({ |
56 | 259 | const b = (p - circularSectionLength - c - d) / 3 |
57 | 260 | const a = 2 * b |
58 | 261 |
|
59 | | - return ` |
60 | | - M ${Math.max(width / 2, width - p)} 0 |
61 | | - C ${width - (p - a)} 0 ${width - (p - a - b)} 0 ${width - |
62 | | - (p - a - b - c)} ${d} |
63 | | - a ${cornerRadius} ${cornerRadius} 0 0 1 ${circularSectionLength} ${circularSectionLength} |
64 | | - C ${width} ${p - a - b} |
65 | | - ${width} ${p - a} |
66 | | - ${width} ${Math.min(height / 2, p)} |
67 | | - L ${width} ${Math.max(height / 2, height - p)} |
68 | | - C ${width} ${height - (p - a)} |
69 | | - ${width} ${height - (p - a - b)} |
70 | | - ${width - d} ${height - (p - a - b - c)} |
71 | | - a ${cornerRadius} ${cornerRadius} 0 0 1 -${circularSectionLength} ${circularSectionLength} |
72 | | - C ${width - (p - a - b)} ${height} |
73 | | - ${width - (p - a)} ${height} |
74 | | - ${Math.max(width / 2, width - p)} ${height} |
75 | | - L ${Math.min(width / 2, p)} ${height} |
76 | | - C ${p - a} ${height} |
77 | | - ${p - a - b} ${height} |
78 | | - ${p - a - b - c} ${height - d} |
79 | | - a ${cornerRadius} ${cornerRadius} 0 0 1 -${circularSectionLength} -${circularSectionLength} |
80 | | - C 0 ${height - (p - a - b)} |
81 | | - 0 ${height - (p - a)} |
82 | | - 0 ${Math.max(height / 2, height - p)} |
83 | | - L 0 ${Math.min(height / 2, p)} |
84 | | - C 0 ${p - a} |
85 | | - 0 ${p - a - b} |
86 | | - ${d} ${p - a - b - c} |
87 | | - a ${cornerRadius} ${cornerRadius} 0 0 1 ${circularSectionLength} -${circularSectionLength} |
88 | | - C ${p - a - b} 0 |
89 | | - ${p - a} 0 |
90 | | - ${+Math.min(width / 2, p)} 0 |
91 | | - Z |
92 | | - ` |
93 | | - .replace(/[\t\s\n]+/g, ' ') |
94 | | - .trim() |
| 262 | + return { |
| 263 | + a, |
| 264 | + b, |
| 265 | + c, |
| 266 | + d, |
| 267 | + p, |
| 268 | + width, |
| 269 | + height, |
| 270 | + circularSectionLength, |
| 271 | + cornerRadius, |
| 272 | + } |
95 | 273 | } |
96 | 274 |
|
97 | 275 | function toRadians(degrees: number) { |
|
0 commit comments