MCPcopy Create free account
hub / github.com/DeepNotesApp/DeepNotes / getClosestPathPointLength

Function getClosestPathPointLength

packages/@stdlib/misc/src/svg.ts:3–18  ·  view source on GitHub ↗
(path: SVGPathElement, pos: Vec2)

Source from the content-addressed store, hash-verified

1import type { Vec2 } from './vec2';
2
3export function getClosestPathPointLength(path: SVGPathElement, pos: Vec2) {
4 let closestPointLength = 0;
5 let minDistance = Infinity;
6
7 for (let i = 0; i <= path.getTotalLength(); i++) {
8 const point = path.getPointAtLength(i);
9 const distance = (point.x - pos.x) ** 2 + (point.y - pos.y) ** 2;
10
11 if (distance < minDistance) {
12 minDistance = distance;
13 closestPointLength = i;
14 }
15 }
16
17 return closestPointLength;
18}
19
20export function getClosestPathPointPercent(path: SVGPathElement, pos: Vec2) {
21 return getClosestPathPointLength(path, pos) / path.getTotalLength();

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected