fix: restrict the corner radius of a rectangle (#9)

The maximum radius can't be bigger than half the size of the rectangle.

Fixes #8
This commit is contained in:
Jacob
2022-06-12 22:27:18 +02:00
committed by GitHub
parent f17372d985
commit cc86a4a6d5

View File

@@ -25,8 +25,13 @@ export const CanvasHelper = {
height: number,
radius: PossibleSpacing,
): T {
//FIXME Handle too small radii
const spacing = new Spacing(radius);
const maxRadius = Math.min(height / 2, width / 2);
spacing.left = Math.min(spacing.left, maxRadius);
spacing.right = Math.min(spacing.right, maxRadius);
spacing.top = Math.min(spacing.top, maxRadius);
spacing.bottom = Math.min(spacing.bottom, maxRadius);
ctx.moveTo(x + spacing.left, y);
ctx.arcTo(x + width, y, x + width, y + height, spacing.top);
ctx.arcTo(x + width, y + height, x, y + height, spacing.right);