Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

points-on-path

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

points-on-path - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

6

lib/index.d.ts
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[][];

50

lib/index.js

@@ -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;
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc