UNPKG

@liammartens/svg-path-properties

Version:

Calculate the length for an SVG path, to use it with node or a Canvas element

37 lines (36 loc) 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LinearPosition = void 0; class LinearPosition { constructor(x0, x1, y0, y1) { this.getTotalLength = () => { return Math.sqrt(Math.pow(this.x0 - this.x1, 2) + Math.pow(this.y0 - this.y1, 2)); }; this.getPointAtLength = (pos) => { let fraction = pos / Math.sqrt(Math.pow(this.x0 - this.x1, 2) + Math.pow(this.y0 - this.y1, 2)); fraction = Number.isNaN(fraction) ? 1 : fraction; const newDeltaX = (this.x1 - this.x0) * fraction; const newDeltaY = (this.y1 - this.y0) * fraction; return { x: this.x0 + newDeltaX, y: this.y0 + newDeltaY }; }; this.getTangentAtLength = (_) => { const module = Math.sqrt((this.x1 - this.x0) * (this.x1 - this.x0) + (this.y1 - this.y0) * (this.y1 - this.y0)); return { x: (this.x1 - this.x0) / module, y: (this.y1 - this.y0) / module, }; }; this.getPropertiesAtLength = (pos) => { const point = this.getPointAtLength(pos); const tangent = this.getTangentAtLength(pos); return { x: point.x, y: point.y, tangentX: tangent.x, tangentY: tangent.y }; }; this.x0 = x0; this.x1 = x1; this.y0 = y0; this.y1 = y1; } } exports.LinearPosition = LinearPosition;