From dfb6e662122715394cfd316a211097b012f5b09e Mon Sep 17 00:00:00 2001 From: Leo Hong <5917188+low-earth-orbit@users.noreply.github.com> Date: Sat, 21 Sep 2024 22:59:11 -0300 Subject: [PATCH] Fix resizing for star shape --- components/Canvas.tsx | 6 ++++-- components/shapes/StarShape.tsx | 27 ++++++++++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/components/Canvas.tsx b/components/Canvas.tsx index c98ae25..c5729fa 100644 --- a/components/Canvas.tsx +++ b/components/Canvas.tsx @@ -379,8 +379,10 @@ export default function Canvas() { if (selectedTool.includes("add")) { setNewObject({ ...newObject, - width, - height, + width: + newObject.shapeName === "star" ? Math.min(width, height) : width, + height: + newObject.shapeName === "star" ? Math.min(width, height) : height, }); } else { console.warn(`Unknown tool: ${selectedTool}`); diff --git a/components/shapes/StarShape.tsx b/components/shapes/StarShape.tsx index c1ccfdb..41af051 100644 --- a/components/shapes/StarShape.tsx +++ b/components/shapes/StarShape.tsx @@ -2,7 +2,11 @@ import React, { useEffect, useRef } from "react"; import { Group, Star, Transformer } from "react-konva"; import { CanvasObjectType } from "../Canvas"; import Konva from "konva"; -import { getStrokeWidth } from "./shapeUtils"; +import { + getStrokeWidth, + SHAPE_MIN_HEIGHT, + SHAPE_MIN_WIDTH, +} from "./shapeUtils"; type Props = { shapeProps: Partial; @@ -32,6 +36,8 @@ export default function StarShape({ shapeProps; const adjustedStrokeWidth = getStrokeWidth(strokeWidth, width, height); + const outerRadius = Math.min(width!, height!) / 2 - adjustedStrokeWidth! / 2; + const innerRadius = outerRadius * 0.382; return ( <> @@ -65,8 +71,8 @@ export default function StarShape({ ...shapeProps, x: group.x(), y: group.y(), - width: Math.max(5, group.width() * scaleX), - height: Math.max(5, group.height() * scaleY), + width: Math.max(SHAPE_MIN_WIDTH, group.width() * scaleX), + height: Math.max(SHAPE_MIN_HEIGHT, group.height() * scaleY), rotation: group.rotation(), }); @@ -81,11 +87,11 @@ export default function StarShape({ )}