points-on-path
Advanced tools
Comparing version 0.1.1 to 0.2.0
import { Point } from 'points-on-curve'; | ||
export { Point } from 'points-on-curve'; | ||
interface PathPoints { | ||
points: Point[]; | ||
continuous: boolean; | ||
} | ||
export declare function pointsOnPath(path: string, tolerance?: number, distance?: number): PathPoints; | ||
export declare function pointsOnPath(path: string, tolerance?: number, distance?: number): Point[][]; |
@@ -1,2 +0,2 @@ | ||
import { pointsOnBezierCurves } from 'points-on-curve'; | ||
import { pointsOnBezierCurves, simplify } from 'points-on-curve'; | ||
import { parsePath, absolutize, normalize } from 'path-data-parser'; | ||
@@ -6,28 +6,33 @@ export function pointsOnPath(path, tolerance, distance) { | ||
const normalized = normalize(absolutize(segments)); | ||
const points = []; | ||
const sets = []; | ||
let currentPoints = []; | ||
let start = [0, 0]; | ||
let moves = 0; | ||
let pendingCurve = []; | ||
const appendPendingCurve = () => { | ||
if (pendingCurve && pendingCurve.length >= 4) { | ||
points.push(...pointsOnBezierCurves(pendingCurve, tolerance, distance)); | ||
if (pendingCurve.length >= 4) { | ||
currentPoints.push(...pointsOnBezierCurves(pendingCurve, tolerance)); | ||
} | ||
pendingCurve = []; | ||
}; | ||
for (const segment of normalized) { | ||
const data = segment.data; | ||
switch (segment.key) { | ||
const appendPendingPoints = () => { | ||
appendPendingCurve(); | ||
if (currentPoints.length) { | ||
sets.push(currentPoints); | ||
currentPoints = []; | ||
} | ||
}; | ||
for (const { key, data } of normalized) { | ||
switch (key) { | ||
case 'M': | ||
appendPendingCurve(); | ||
moves++; | ||
appendPendingPoints(); | ||
start = [data[0], data[1]]; | ||
points.push(start); | ||
currentPoints.push(start); | ||
break; | ||
case 'L': | ||
appendPendingCurve(); | ||
points.push([data[0], data[1]]); | ||
currentPoints.push([data[0], data[1]]); | ||
break; | ||
case 'C': | ||
if (!pendingCurve.length) { | ||
const lastPoint = points.length ? points[points.length - 1] : start; | ||
const lastPoint = currentPoints.length ? currentPoints[currentPoints.length - 1] : start; | ||
pendingCurve.push([lastPoint[0], lastPoint[1]]); | ||
@@ -41,11 +46,18 @@ } | ||
appendPendingCurve(); | ||
points.push([start[0], start[1]]); | ||
currentPoints.push([start[0], start[1]]); | ||
break; | ||
} | ||
} | ||
appendPendingCurve(); | ||
return { | ||
continuous: moves < 2, | ||
points | ||
}; | ||
appendPendingPoints(); | ||
if (!distance) { | ||
return sets; | ||
} | ||
const out = []; | ||
for (const set of sets) { | ||
const simplifiedSet = simplify(set, distance); | ||
if (simplifiedSet.length) { | ||
out.push(simplifiedSet); | ||
} | ||
} | ||
return out; | ||
} |
{ | ||
"name": "points-on-path", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Estimate points on a SVG path", | ||
@@ -31,4 +31,4 @@ "main": "lib/index.js", | ||
"path-data-parser": "0.1.0", | ||
"points-on-curve": "0.1.1" | ||
"points-on-curve": "0.2.0" | ||
} | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import { Point, pointsOnBezierCurves } from 'points-on-curve'; | ||
import { Point, pointsOnBezierCurves, simplify } from 'points-on-curve'; | ||
import { parsePath, absolutize, normalize } from 'path-data-parser'; | ||
@@ -6,18 +6,14 @@ | ||
interface PathPoints { | ||
points: Point[]; | ||
continuous: boolean; | ||
} | ||
export function pointsOnPath(path: string, tolerance?: number, distance?: number): PathPoints { | ||
export function pointsOnPath(path: string, tolerance?: number, distance?: number): Point[][] { | ||
const segments = parsePath(path); | ||
const normalized = normalize(absolutize(segments)); | ||
const points: Point[] = []; | ||
const sets: Point[][] = []; | ||
let currentPoints: Point[] = []; | ||
let start: Point = [0, 0]; | ||
let moves = 0; | ||
let pendingCurve: Point[] = []; | ||
const appendPendingCurve = () => { | ||
if (pendingCurve && pendingCurve.length >= 4) { | ||
points.push(...pointsOnBezierCurves(pendingCurve, tolerance, distance)); | ||
if (pendingCurve.length >= 4) { | ||
currentPoints.push(...pointsOnBezierCurves(pendingCurve, tolerance)); | ||
} | ||
@@ -27,18 +23,24 @@ pendingCurve = []; | ||
for (const segment of normalized) { | ||
const data = segment.data; | ||
switch (segment.key) { | ||
const appendPendingPoints = () => { | ||
appendPendingCurve(); | ||
if (currentPoints.length) { | ||
sets.push(currentPoints); | ||
currentPoints = []; | ||
} | ||
}; | ||
for (const { key, data } of normalized) { | ||
switch (key) { | ||
case 'M': | ||
appendPendingCurve(); | ||
moves++; | ||
appendPendingPoints(); | ||
start = [data[0], data[1]]; | ||
points.push(start); | ||
currentPoints.push(start); | ||
break; | ||
case 'L': | ||
appendPendingCurve(); | ||
points.push([data[0], data[1]]); | ||
currentPoints.push([data[0], data[1]]); | ||
break; | ||
case 'C': | ||
if (!pendingCurve.length) { | ||
const lastPoint = points.length ? points[points.length - 1] : start; | ||
const lastPoint = currentPoints.length ? currentPoints[currentPoints.length - 1] : start; | ||
pendingCurve.push([lastPoint[0], lastPoint[1]]); | ||
@@ -52,11 +54,20 @@ } | ||
appendPendingCurve(); | ||
points.push([start[0], start[1]]); | ||
currentPoints.push([start[0], start[1]]); | ||
break; | ||
} | ||
} | ||
appendPendingCurve(); | ||
return { | ||
continuous: moves < 2, | ||
points | ||
}; | ||
appendPendingPoints(); | ||
if (!distance) { | ||
return sets; | ||
} | ||
const out: Point[][] = []; | ||
for (const set of sets) { | ||
const simplifiedSet = simplify(set, distance); | ||
if (simplifiedSet.length) { | ||
out.push(simplifiedSet); | ||
} | ||
} | ||
return out; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9601
207
+ Addedpoints-on-curve@0.2.0(transitive)
- Removedpoints-on-curve@0.1.1(transitive)
Updatedpoints-on-curve@0.2.0