Skip to content

Commit

Permalink
Fix resizing for star shape
Browse files Browse the repository at this point in the history
  • Loading branch information
low-earth-orbit committed Sep 22, 2024
1 parent eba90a2 commit dfb6e66
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
6 changes: 4 additions & 2 deletions components/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
27 changes: 20 additions & 7 deletions components/shapes/StarShape.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<CanvasObjectType>;
Expand Down Expand Up @@ -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 (
<>
Expand Down Expand Up @@ -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(),
});

Expand All @@ -81,11 +87,11 @@ export default function StarShape({
<Star
name={`shape-${shapeName}`}
id={`shape-${shapeName}-${id}`}
x={0}
y={0}
x={width! / 2}
y={height! / 2}
numPoints={5} // Define a 5-pointed star
innerRadius={Math.min(width!, height!) / 4} // Inner radius for star
outerRadius={Math.min(width!, height!) / 2} // Outer radius for star
innerRadius={innerRadius} // Inner radius for star
outerRadius={outerRadius} // Outer radius for star
stroke={stroke}
strokeWidth={adjustedStrokeWidth}
lineJoin="round" // round corners
Expand Down Expand Up @@ -131,6 +137,13 @@ export default function StarShape({
}
return newBox;
}}
keepRatio={true}
enabledAnchors={[
"top-left",
"top-right",
"bottom-left",
"bottom-right",
]}
/>
)}
</>
Expand Down

0 comments on commit dfb6e66

Please sign in to comment.