mirror of
https://github.com/hex248/tsos.git
synced 2026-02-07 18:23:05 +00:00
extended morphable shape to use generated points
This commit is contained in:
@@ -1,6 +1,12 @@
|
|||||||
import { Circle } from "react-konva";
|
import { morphPoints } from "@/lib/shapes/morph";
|
||||||
import type { KonvaEventObject } from "konva/lib/Node";
|
import { generateCirclePoints, generateSquarePoints, generateTrianglePoints } from "@/lib/shapes/points";
|
||||||
import type { ShapeState } from "@/types/shape";
|
import type { ShapeState } from "@/types/shape";
|
||||||
|
import type { KonvaEventObject } from "konva/lib/Node";
|
||||||
|
import { useMemo } from "react";
|
||||||
|
import { Group, Line } from "react-konva";
|
||||||
|
|
||||||
|
const SHAPE_RADIUS = 100;
|
||||||
|
const NUM_POINTS = 64;
|
||||||
|
|
||||||
export default function MorphableShape({
|
export default function MorphableShape({
|
||||||
state,
|
state,
|
||||||
@@ -17,7 +23,29 @@ export default function MorphableShape({
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const flatPoints = useMemo(() => {
|
||||||
|
const presetPoints = (() => {
|
||||||
|
switch (state.preset) {
|
||||||
|
case "triangle":
|
||||||
|
return generateTrianglePoints(0, 0, SHAPE_RADIUS, NUM_POINTS);
|
||||||
|
case "square":
|
||||||
|
return generateSquarePoints(0, 0, SHAPE_RADIUS, NUM_POINTS);
|
||||||
|
case "circle":
|
||||||
|
return generateCirclePoints(0, 0, SHAPE_RADIUS, NUM_POINTS);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
|
const circlePoints = generateCirclePoints(0, 0, SHAPE_RADIUS, NUM_POINTS);
|
||||||
|
const t = state.roundness / 100;
|
||||||
|
const morphed = morphPoints(presetPoints, circlePoints, t);
|
||||||
|
|
||||||
|
// flatten to [x1, y1, x2, y2, ...]
|
||||||
|
return morphed.flatMap((p) => [p.x, p.y]);
|
||||||
|
}, [state.preset, state.roundness]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Circle x={state.x} y={state.y} radius={100} fill={state.color} draggable onDragMove={handleDrag} />
|
<Group x={state.x} y={state.y} draggable onDragMove={handleDrag}>
|
||||||
|
<Line points={flatPoints} closed fill={state.color} />
|
||||||
|
</Group>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import type { ShapeState } from "@/types/shape";
|
|||||||
const DEFAULT_STATE: ShapeState = {
|
const DEFAULT_STATE: ShapeState = {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
preset: "circle",
|
preset: "square",
|
||||||
roundness: 100, // full circle
|
roundness: 15, // full circle
|
||||||
size: 50, // medium
|
size: 50, // medium
|
||||||
wobble: 20, // subtle
|
wobble: 20, // subtle
|
||||||
wobbleSpeed: 50, // medium
|
wobbleSpeed: 50, // medium
|
||||||
|
|||||||
Reference in New Issue
Block a user