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

@daeinc/geom

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@daeinc/geom - npm Package Compare versions

Comparing version 0.7.2 to 0.8.0

12

dist/index.d.ts

@@ -1,4 +0,4 @@

export declare type Pt = number[];
export declare type Pts = number[][];
export declare type GenericObject = Record<string, any>;
export type Pt = number[];
export type Pts = number[][];
export type GenericObject = Record<string, any>;
/**

@@ -84,8 +84,2 @@ * generates an array of paths (excl. original 2 paths)

/**
* squared version of getPathLength() for performance boost
* @param path array of [x, y] points
* @returns total squared length of path
*/
export declare const getPathLengthSq: (path: Pts) => number;
/**
* converts angle [-pi, pi] to [0, 2pi)

@@ -92,0 +86,0 @@ * @param pt1

@@ -1,10 +0,4 @@

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.combinePath = exports.calcTByLength = exports.scalePath = exports.scalePoint = exports.rotatePoint = exports.reflectPath = exports.reflectPoint = exports.projectPointOnLine = exports.interpolate = exports.interpolateObject = exports.interpolatePath = exports.interpolateArray = exports.getSegmentLengths = exports.getPositiveAngleBetween = exports.getPathLengthSq = exports.getPathLength = exports.getAngleBetween = exports.generateSmoothPath = exports.extrudePath = exports.distSq = exports.dist = exports.createShapeFunc = exports.blendPath = void 0;
const math_1 = require("@daeinc/math");
const array_1 = require("@daeinc/array");
const gl_vec2_1 = __importDefault(require("gl-vec2"));
import { mix, reflect, roundF, TWO_PI } from "@daeinc/math";
import { interpolateArray as importedInterpolateArray } from "@daeinc/array";
import vec2 from "gl-vec2";
/**

@@ -23,3 +17,3 @@ * generates an array of paths (excl. original 2 paths)

*/
const blendPath = (path1, path2, numBlends, guidePath) => {
export const blendPath = (path1, path2, numBlends, guidePath) => {
return Array(numBlends)

@@ -29,6 +23,5 @@ .fill([])

const t = (i + 1) / (numBlends + 1);
return (0, exports.interpolatePath)(path1, path2, t);
return interpolatePath(path1, path2, t);
});
};
exports.blendPath = blendPath;
/**

@@ -40,3 +33,3 @@ * the resulting function is transformed to draw from center [0, 0]

*/
const createShapeFunc = (pts, anchor = [0.5, 0.5]) => {
export const createShapeFunc = (pts, anchor = [0.5, 0.5]) => {
return (x, y, w, h) => pts.map((pt) => {

@@ -48,3 +41,2 @@ const xdiff = pt[0] - anchor[0];

};
exports.createShapeFunc = createShapeFunc;
/**

@@ -56,6 +48,5 @@ * calculate distance between two point[]s

*/
const dist = (pt1, pt2) => {
export const dist = (pt1, pt2) => {
return Math.sqrt(Math.pow(pt2[0] - pt1[0], 2) + Math.pow(pt2[1] - pt1[1], 2));
};
exports.dist = dist;
/**

@@ -67,6 +58,5 @@ * squared distance (x^2 + y^2) between two point[]s

*/
const distSq = (pt1, pt2) => {
export const distSq = (pt1, pt2) => {
return Math.pow(pt2[0] - pt1[0], 2) + Math.pow(pt2[1] - pt1[1], 2);
};
exports.distSq = distSq;
/**

@@ -85,3 +75,3 @@ * extrude path in 2d space

*/
const extrudePath = (path, numPointsToExtrude, offset, mode = "end", shapeFunc) => {
export const extrudePath = (path, numPointsToExtrude, offset, mode = "end", shapeFunc) => {
if (numPointsToExtrude > path.length) {

@@ -115,3 +105,2 @@ throw new Error("extrudePath(): numPointsToExtrude can't exceed length of path");

};
exports.extrudePath = extrudePath;
/**

@@ -126,3 +115,3 @@ * generate extra points for smooth hard corners of path

*/
const generateSmoothPath = (pts, smoothFactor) => {
export const generateSmoothPath = (pts, smoothFactor) => {
const smoothPoints = [];

@@ -133,10 +122,10 @@ smoothPoints.push(pts[0]);

const b = pts[i + 1];
const diff = gl_vec2_1.default.sub([], b, a);
const diffScaled1 = gl_vec2_1.default.mul([], diff, [smoothFactor, smoothFactor]);
const mid1 = gl_vec2_1.default.add([], a, diffScaled1);
const diffScaled2 = gl_vec2_1.default.mul([], diff, [
const diff = vec2.sub([], b, a);
const diffScaled1 = vec2.mul([], diff, [smoothFactor, smoothFactor]);
const mid1 = vec2.add([], a, diffScaled1);
const diffScaled2 = vec2.mul([], diff, [
1 - smoothFactor,
1 - smoothFactor,
]);
const mid2 = gl_vec2_1.default.add([], a, diffScaled2);
const mid2 = vec2.add([], a, diffScaled2);
smoothPoints.push(mid1, mid2, b);

@@ -146,3 +135,2 @@ }

};
exports.generateSmoothPath = generateSmoothPath;
/**

@@ -157,6 +145,5 @@ * atan2() gives angle between [-PI, PI]

*/
const getAngleBetween = (pt1, pt2) => {
export const getAngleBetween = (pt1, pt2) => {
return Math.atan2(pt2[1] - pt1[1], pt2[0] - pt1[0]);
};
exports.getAngleBetween = getAngleBetween;
/**

@@ -171,3 +158,3 @@ * take an array of points and return total length of path

*/
const getPathLength = (path) => {
export const getPathLength = (path) => {
return path.reduce((totalLen, pt, i, arr) => {

@@ -183,21 +170,3 @@ if (arr.length < 2)

};
exports.getPathLength = getPathLength;
/**
* squared version of getPathLength() for performance boost
* @param path array of [x, y] points
* @returns total squared length of path
*/
const getPathLengthSq = (path) => {
return path.reduce((totalLen, pt, i, arr) => {
if (arr.length < 2)
return 0; // handle single point length
if (i === arr.length - 1)
return totalLen; // skip last one (no i+1 there)
return (totalLen +
Math.pow(arr[i + 1][0] - arr[i][0], 2) +
Math.pow(arr[i + 1][1] - arr[i][1], 2));
}, 0);
};
exports.getPathLengthSq = getPathLengthSq;
/**
* converts angle [-pi, pi] to [0, 2pi)

@@ -208,7 +177,6 @@ * @param pt1

*/
const getPositiveAngleBetween = (pt1, pt2) => {
const angle = (0, exports.getAngleBetween)(pt1, pt2);
return angle >= 0 ? angle : angle + math_1.TWO_PI;
export const getPositiveAngleBetween = (pt1, pt2) => {
const angle = getAngleBetween(pt1, pt2);
return angle >= 0 ? angle : angle + TWO_PI;
};
exports.getPositiveAngleBetween = getPositiveAngleBetween;
/**

@@ -219,6 +187,6 @@ * calculate each segment length(distance)

*/
const getSegmentLengths = (pts) => {
export const getSegmentLengths = (pts) => {
const result = [];
for (let i = 0; i < pts.length - 1; i++) {
const d = (0, exports.dist)(pts[i], pts[i + 1]);
const d = dist(pts[i], pts[i + 1]);
result.push(d);

@@ -228,4 +196,3 @@ }

};
exports.getSegmentLengths = getSegmentLengths;
exports.interpolateArray = array_1.interpolateArray; // REVIEW: hmm...
export const interpolateArray = importedInterpolateArray; // REVIEW: hmm...
/**

@@ -238,3 +205,3 @@ * mix/lerp 2d number array. usually used for path data of [x, y]

*/
const interpolatePath = (pathStart, pathTarget, t) => {
export const interpolatePath = (pathStart, pathTarget, t) => {
if (pathStart.length === 0 || pathTarget.length === 0)

@@ -248,8 +215,7 @@ throw new Error("interpolatePath(): path cannot be empty");

return [
(0, math_1.mix)(pathStart[i][0], pathTarget[i][0], t),
(0, math_1.mix)(pathStart[i][1], pathTarget[i][1], t),
mix(pathStart[i][0], pathTarget[i][0], t),
mix(pathStart[i][1], pathTarget[i][1], t),
];
});
};
exports.interpolatePath = interpolatePath;
/**

@@ -263,3 +229,3 @@ * interpolate object with {string:number}. ie. {x:10}.

*/
const interpolateObject = (objStart, objTarget, t) => {
export const interpolateObject = (objStart, objTarget, t) => {
const obj = {};

@@ -271,7 +237,6 @@ if (Object.keys(objStart).length !== Object.keys(objTarget).length)

throw new Error("interpolateObject(): objects must have same keys");
obj[key] = (0, math_1.mix)(objStart[key], objTarget[key], t);
obj[key] = mix(objStart[key], objTarget[key], t);
}
return obj;
};
exports.interpolateObject = interpolateObject;
/**

@@ -289,3 +254,3 @@ * interpolate number, number[], number[][] or generic object

*/
const interpolate = (
export const interpolate = (
// start: number | number[] | Pts | GenericObject,

@@ -297,3 +262,3 @@ // target: number | number[] | Pts | GenericObject,

if (typeof start === "number" && typeof target === "number") {
return (0, math_1.mix)(start, target, t);
return mix(start, target, t);
}

@@ -303,7 +268,7 @@ else if (Array.isArray(start) && Array.isArray(target)) {

// 2d array
return (0, exports.interpolatePath)(start, target, t);
return interpolatePath(start, target, t);
}
else {
// 1d array
return (0, exports.interpolateArray)(start, target, t);
return interpolateArray(start, target, t);
}

@@ -317,3 +282,3 @@ // } else if (start.constructor === Object) {

// object
return (0, exports.interpolateObject)(start, target, t);
return interpolateObject(start, target, t);
}

@@ -325,3 +290,2 @@ else {

};
exports.interpolate = interpolate;
/**

@@ -333,14 +297,13 @@ * project a point on a line using vector.

*/
const projectPointOnLine = (pt, line) => {
const ptVec = gl_vec2_1.default.fromValues(pt[0] - line[1][0], pt[1] - line[1][1]);
const lineVec = gl_vec2_1.default.fromValues(line[0][0] - line[1][0], line[0][1] - line[1][1]);
const prod = gl_vec2_1.default.dot(ptVec, lineVec);
const proj = prod / gl_vec2_1.default.len(lineVec);
const projVec = gl_vec2_1.default.fromValues(proj, proj);
const result = gl_vec2_1.default.normalize(lineVec, lineVec);
gl_vec2_1.default.mul(result, lineVec, projVec);
gl_vec2_1.default.add(result, result, line[1]);
export const projectPointOnLine = (pt, line) => {
const ptVec = vec2.fromValues(pt[0] - line[1][0], pt[1] - line[1][1]);
const lineVec = vec2.fromValues(line[0][0] - line[1][0], line[0][1] - line[1][1]);
const prod = vec2.dot(ptVec, lineVec);
const proj = prod / vec2.len(lineVec);
const projVec = vec2.fromValues(proj, proj);
const result = vec2.normalize(lineVec, lineVec);
vec2.mul(result, lineVec, projVec);
vec2.add(result, result, line[1]);
return result;
};
exports.projectPointOnLine = projectPointOnLine;
/**

@@ -352,7 +315,7 @@ * reflect a point on another point or a line

*/
const reflectPoint = (pt, axis) => {
export const reflectPoint = (pt, axis) => {
if (axis[0].constructor === Array) {
const projVec = gl_vec2_1.default.fromValues(...(0, exports.projectPointOnLine)(pt, axis));
const distVec = gl_vec2_1.default.sub([], gl_vec2_1.default.fromValues(pt[0], pt[1]), projVec);
const reflVec = gl_vec2_1.default.sub(projVec, projVec, distVec);
const projVec = vec2.fromValues(...projectPointOnLine(pt, axis));
const distVec = vec2.sub([], vec2.fromValues(pt[0], pt[1]), projVec);
const reflVec = vec2.sub(projVec, projVec, distVec);
return reflVec;

@@ -362,8 +325,7 @@ }

return [
(0, math_1.reflect)(pt[0], axis[0]),
(0, math_1.reflect)(pt[1], axis[1]),
reflect(pt[0], axis[0]),
reflect(pt[1], axis[1]),
];
}
};
exports.reflectPoint = reflectPoint;
/**

@@ -375,6 +337,5 @@ * reflect a path either on a point or a line

*/
const reflectPath = (pts, axis) => {
return pts.map((pt) => (0, exports.reflectPoint)(pt, axis));
export const reflectPath = (pts, axis) => {
return pts.map((pt) => reflectPoint(pt, axis));
};
exports.reflectPath = reflectPath;
/**

@@ -388,8 +349,7 @@ * TODO: haven't tested "anchor" yet

*/
const rotatePoint = (pt, angle, anchor = [0, 0], precision = 5) => {
export const rotatePoint = (pt, angle, anchor = [0, 0], precision = 5) => {
const x = anchor[0] + Math.cos(angle) * pt[0];
const y = anchor[1] + Math.sin(angle) * pt[1];
return [(0, math_1.roundF)(x, precision), (0, math_1.roundF)(y, precision)];
return [roundF(x, precision), roundF(y, precision)];
};
exports.rotatePoint = rotatePoint;
/**

@@ -401,3 +361,3 @@ * scale a single point

*/
const scalePoint = (pt, size) => {
export const scalePoint = (pt, size) => {
const [x, y] = pt;

@@ -407,3 +367,2 @@ const [w, h] = size;

};
exports.scalePoint = scalePoint;
/**

@@ -415,6 +374,5 @@ * take normalized path data and return [ x, y ] scaled to width and height

*/
const scalePath = (path, size) => {
return path.map((pt) => (0, exports.scalePoint)(pt, size));
export const scalePath = (path, size) => {
return path.map((pt) => scalePoint(pt, size));
};
exports.scalePath = scalePath;
/**

@@ -428,6 +386,5 @@ * by default, path t value is based on number of points. this function calculates t based on each segment length.

*/
const calcTByLength = (path) => {
export const calcTByLength = (path) => {
return [];
};
exports.calcTByLength = calcTByLength;
/**

@@ -447,6 +404,5 @@ * combine 2 paths by a single connecting point.

*/
const combinePath = (path1, path2, mode) => {
export const combinePath = (path1, path2, mode) => {
return path1;
};
exports.combinePath = combinePath;
//# sourceMappingURL=index.js.map
{
"name": "@daeinc/geom",
"version": "0.7.2",
"version": "0.8.0",
"description": "Geometry utilities",

@@ -16,3 +16,3 @@ "types": "./dist/index.d.ts",

"test": "jest --watch",
"demo": "parcel ./demo/index.html --dist-dir ./demo/build",
"demo": "parcel ./demo/index.html --dist-dir ./demo/build --no-source-maps --no-cache",
"build": "tsc --build --clean && tsc --build"

@@ -23,12 +23,13 @@ },

"dependencies": {
"@daeinc/array": "^0.4.2",
"@daeinc/math": "^0.4.0",
"@daeinc/array": "^0.5.0",
"@daeinc/math": "^0.5.0",
"gl-vec2": "^1.3.0"
},
"devDependencies": {
"@daeinc/canvas": "^0.8.1",
"@daeinc/canvas": "^0.12.1",
"@daeinc/draw": "^0.2.1",
"@types/gl-vec2": "^1.3.2",
"jest": "^29.3.1",
"jest-canvas-mock": "^2.4.0",
"ts-jest": "^29.0.3"
"ts-jest": "^29.0.5"
},

@@ -35,0 +36,0 @@ "publishConfig": {

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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