storytellurx-v1/app/components/utils/svg-path-to-vertices.tsx
2025-07-13 22:01:50 +02:00

38 lines
1.1 KiB
TypeScript

import SVGPathCommander from "svg-path-commander";
// Function to convert SVG path `d` to vertices
export function parsePathToVertices(path: string, sampleLength = 15) {
// Convert path to absolute commands
const commander = new SVGPathCommander(path);
const points: { x: number; y: number }[] = [];
let lastPoint: { x: number; y: number } | null = null;
// Get total length of the path
const totalLength = commander.getTotalLength();
let length = 0;
// Sample points along the path
while (length < totalLength) {
const point = commander.getPointAtLength(length);
// Only add point if it's different from the last one
if (!lastPoint || point.x !== lastPoint.x || point.y !== lastPoint.y) {
points.push({ x: point.x, y: point.y });
lastPoint = point;
}
length += sampleLength;
}
// Ensure we get the last point
const finalPoint = commander.getPointAtLength(totalLength);
if (
lastPoint &&
(finalPoint.x !== lastPoint.x || finalPoint.y !== lastPoint.y)
) {
points.push({ x: finalPoint.x, y: finalPoint.y });
}
return points;
}