(path: SVGPathElement, pos: Vec2)
| 1 | import type { Vec2 } from './vec2'; |
| 2 | |
| 3 | export 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 | |
| 20 | export function getClosestPathPointPercent(path: SVGPathElement, pos: Vec2) { |
| 21 | return getClosestPathPointLength(path, pos) / path.getTotalLength(); |
no outgoing calls
no test coverage detected