The KonvaVector component uses a PointCreationManager as the single source of truth for all point creation, whether it's regular points or bezier points. This ensures consistent behavior and centralized point creation logic.
The component exposes three methods through the ref:
interface KonvaVectorRef {
// ... existing methods ...
// Programmatic point creation methods
startPoint: (x: number, y: number) => boolean;
updatePoint: (x: number, y: number) => boolean;
commitPoint: (x: number, y: number) => boolean;
}
import { useRef } from "react";
import { KonvaVector, type KonvaVectorRef } from "./KonvaVector";
function MyComponent() {
const vectorRef = useRef<KonvaVectorRef>(null);
const handleProgrammaticPointCreation = () => {
if (!vectorRef.current) return;
// Start creating a point at (100, 200)
const started = vectorRef.current.startPoint(100, 200);
if (!started) return;
// Simulate dragging to create a bezier point
// Move 10 pixels away from start position
vectorRef.current.updatePoint(110, 210);
// Continue dragging
vectorRef.current.updatePoint(120, 220);
// Commit the point creation
vectorRef.current.commitPoint(125, 225);
};
const handleSimplePointCreation = () => {
if (!vectorRef.current) return;
// Start creating a point
const started = vectorRef.current.startPoint(150, 250);
if (!started) return;
// Small movement (less than 5 pixels) creates a regular point
vectorRef.current.updatePoint(152, 252);
// Commit the point
vectorRef.current.commitPoint(152, 252);
};
return (
<KonvaVector
ref={vectorRef}
initialPoints={[]}
onPointsChange={setPoints}
allowBezier={true}
allowClose={false}
width={800}
height={600}
/>
);
}
true if successful, false if already creating or constraintstrue if successful, false if not creatingtrue if successful, false if not creatingThe component exposes four methods for programmatic point transformation:
translatePoints(dx, dy, pointIds?)Translates points by the specified delta. If pointIds is provided, only those points are transformed.
// Translate all points by (50, 30)
vectorRef.current?.translatePoints(50, 30);
// Translate specific points by (20, -20)
vectorRef.current?.translatePoints(20, -20, ["point1", "point2"]);
rotatePoints(angle, centerX, centerY, pointIds?)Rotates points around the specified center point. Angle is in degrees.
// Rotate all points 45° around the shape's center (calculated automatically)
vectorRef.current?.rotatePoints(45, 0, 0);
// Rotate specific points 90° around a specific point
vectorRef.current?.rotatePoints(90, 200, 150, ["point1", "point2"]);
scalePoints(scaleX, scaleY, centerX, centerY, pointIds?)Scales points around the specified center point.
// Scale all points 1.5x around the shape's center (calculated automatically)
vectorRef.current?.scalePoints(1.5, 1.5, 0, 0);
// Scale specific points 0.8x around a specific point
vectorRef.current?.scalePoints(0.8, 0.8, 200, 150, ["point1", "point2"]);
transformPoints(transformation, pointIds?)Applies a complex transformation combining translation, rotation, and scaling.
// Transform all points around the shape's center (calculated automatically)
vectorRef.current?.transformPoints({
dx: 30, // Translation X
dy: -20, // Translation Y
rotation: 30, // Rotation in degrees
scaleX: 1.2, // Scale X
scaleY: 0.9, // Scale Y
// centerX and centerY are calculated automatically when not provided
});
// Transform specific points around a specific center
vectorRef.current?.transformPoints({
dx: 30,
dy: -20,
rotation: 30,
scaleX: 1.2,
scaleY: 0.9,
centerX: 200, // Center point for rotation/scaling
centerY: 150,
}, ["point1", "point2"]);
pointIds is not provided, all points are transformed as a single shapepointIds), the center point is automatically calculated from the bounding box of all points (including control points)onPointsChangegetShapeBoundingBox()Returns the bounding box of the entire shape, including bezier curves.
const bbox = vectorRef.current?.getShapeBoundingBox();
// Returns: { left: number, top: number, right: number, bottom: number }
Features:
- Accurate Bounding Box: Calculates the true bounding box including bezier curve extrema
- Mathematical Precision: Uses derivative calculations to find curve extrema points
- Complete Coverage: Ensures the entire shape (including curved segments) is contained within the bounding box
The PointCreationManager is the single source of truth for all point creation:
The PointCreationManager is integrated with the existing mouse handlers:
isCreating() returns true), theThe PointCreationManager uses a singleton pattern, ensuring only one instance
exists per KonvaVector component. This prevents multiple point creation
processes from running simultaneously.
The manager respects all the same constraints as manual point creation:
maxPoints limitminPoints requirementallowBezier settingskeletonEnabled modeWhen skeletonEnabled is true, new points are connected to the activePointId
instead of the last point in the array, allowing for branching path creation.