export function roundedRectSDF(px: number, py: number, halfWidth: number, halfHeight: number, radius: number): number { const qx = Math.abs(px) - halfWidth + radius; const qy = Math.abs(py) - halfHeight + radius; return Math.sqrt(Math.max(qx, 0) ** 2 + Math.max(qy, 0) ** 2) + Math.min(Math.max(qx, qy), 0) - radius; } export function pixelate(p: number, pixelSize: number): number { if (pixelSize <= 1) return p; return Math.floor(p / pixelSize) * pixelSize + pixelSize * 0.5; } export function pixelate2D(px: number, py: number, pixelSize: number): [number, number] { return [pixelate(px, pixelSize), pixelate(py, pixelSize)]; } interface PathOptions { width: number; height: number; rounding: number; pixelation: number; } export function generateRoundedRectPath(options: PathOptions): string { const { width, height, rounding, pixelation } = options; const halfWidth = width / 2; const halfHeight = height / 2; const radius = Math.min(Math.min(halfWidth, halfHeight), rounding); if (radius <= 0) { return `M 0 0 H ${width} V ${height} H 0 Z`; } const pixelatedRadius = pixelation > 1 ? Math.floor(radius / pixelation) * pixelation : radius; if (pixelatedRadius <= 0) { return `M 0 0 H ${width} V ${height} H 0 Z`; } if (pixelation <= 1) { return generateSmoothRoundedRect(width, height, pixelatedRadius); } return generatePixelatedRoundedRect(width, height, pixelatedRadius, pixelation); } function generateSmoothRoundedRect(width: number, height: number, radius: number): string { const r = radius; return [ `M ${r} 0`, `H ${width - r}`, `A ${r} ${r} 0 0 1 ${width} ${r}`, `V ${height - r}`, `A ${r} ${r} 0 0 1 ${width - r} ${height}`, `H ${r}`, `A ${r} ${r} 0 0 1 0 ${height - r}`, `V ${r}`, `A ${r} ${r} 0 0 1 ${r} 0`, 'Z' ].join(' '); } function generatePixelatedRoundedRect(width: number, height: number, radius: number, pixelation: number): string { const halfWidth = width / 2; const halfHeight = height / 2; const points: [number, number][] = []; const samplesPerCorner = Math.max(8, Math.ceil(radius / pixelation) * 4); function addPoint(centeredX: number, centeredY: number) { const [px, py] = pixelate2D(centeredX, centeredY, pixelation); const x = px + halfWidth; const y = py + halfHeight; const snappedX = Math.round(x / pixelation) * pixelation; const snappedY = Math.round(y / pixelation) * pixelation; points.push([snappedX, snappedY]); } // Top edge const topY = -halfHeight; for (let x = -halfWidth + radius; x <= halfWidth - radius; x += pixelation) { addPoint(x, topY); } // Top-right corner const trCenterX = halfWidth - radius; const trCenterY = -halfHeight + radius; for (let i = 0; i <= samplesPerCorner; i++) { const angle = -Math.PI / 2 + (Math.PI / 2) * (i / samplesPerCorner); addPoint(trCenterX + radius * Math.cos(angle), trCenterY + radius * Math.sin(angle)); } // Right edge const rightX = halfWidth; for (let y = -halfHeight + radius + pixelation; y <= halfHeight - radius; y += pixelation) { addPoint(rightX, y); } // Bottom-right corner const brCenterX = halfWidth - radius; const brCenterY = halfHeight - radius; for (let i = 0; i <= samplesPerCorner; i++) { const angle = (Math.PI / 2) * (i / samplesPerCorner); addPoint(brCenterX + radius * Math.cos(angle), brCenterY + radius * Math.sin(angle)); } // Bottom edge const bottomY = halfHeight; for (let x = halfWidth - radius - pixelation; x >= -halfWidth + radius; x -= pixelation) { addPoint(x, bottomY); } // Bottom-left corner const blCenterX = -halfWidth + radius; const blCenterY = halfHeight - radius; for (let i = 0; i <= samplesPerCorner; i++) { const angle = Math.PI / 2 + (Math.PI / 2) * (i / samplesPerCorner); addPoint(blCenterX + radius * Math.cos(angle), blCenterY + radius * Math.sin(angle)); } // Left edge const leftX = -halfWidth; for (let y = halfHeight - radius - pixelation; y >= -halfHeight + radius; y -= pixelation) { addPoint(leftX, y); } // Top-left corner const tlCenterX = -halfWidth + radius; const tlCenterY = -halfHeight + radius; for (let i = 0; i <= samplesPerCorner; i++) { const angle = Math.PI + (Math.PI / 2) * (i / samplesPerCorner); addPoint(tlCenterX + radius * Math.cos(angle), tlCenterY + radius * Math.sin(angle)); } // Remove duplicate consecutive points const dedupedPoints: [number, number][] = []; for (const point of points) { const last = dedupedPoints[dedupedPoints.length - 1]; if (!last || last[0] !== point[0] || last[1] !== point[1]) { dedupedPoints.push(point); } } if (dedupedPoints.length > 1) { const first = dedupedPoints[0]; const last = dedupedPoints[dedupedPoints.length - 1]; if (first[0] === last[0] && first[1] === last[1]) { dedupedPoints.pop(); } } if (dedupedPoints.length < 3) { return `M 0 0 H ${width} V ${height} H 0 Z`; } // Build path with stair-steps (no diagonal lines) const pathParts = [`M ${dedupedPoints[0][0]} ${dedupedPoints[0][1]}`]; for (let i = 1; i < dedupedPoints.length; i++) { const prev = dedupedPoints[i - 1]; const curr = dedupedPoints[i]; if (prev[0] !== curr[0] && prev[1] !== curr[1]) { pathParts.push(`H ${curr[0]}`); pathParts.push(`V ${curr[1]}`); } else if (prev[0] !== curr[0]) { pathParts.push(`H ${curr[0]}`); } else if (prev[1] !== curr[1]) { pathParts.push(`V ${curr[1]}`); } } const last = dedupedPoints[dedupedPoints.length - 1]; const first = dedupedPoints[0]; if (last[0] !== first[0] && last[1] !== first[1]) { pathParts.push(`H ${first[0]}`); } pathParts.push('Z'); return pathParts.join(' '); }