Installations
npm install @antv/util
Score
98.6
Supply Chain
99.6
Quality
86.9
Maintenance
100
Vulnerability
100
License
Developer
antvis
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
20.16.0
NPM Version
10.8.1
Statistics
74 Stars
34 Commits
27 Forks
32 Watching
3 Branches
58 Contributors
Updated on 20 Sept 2024
Bundle Size
49.62 kB
Minified
15.79 kB
Minified + Gzipped
Languages
TypeScript (97.92%)
JavaScript (2.08%)
Total Downloads
Cumulative downloads
Total Downloads
42,935,399
Last day
-13.8%
59,582
Compared to previous day
Last week
10.4%
347,227
Compared to previous week
Last month
13.8%
1,320,431
Compared to previous month
Last year
46.4%
14,056,253
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
3
Dev Dependencies
25
util
AntV 底层依赖的工具库,不建议在自己业务中使用。
Usage
1import { gradient } from '@antv/util';
原则
- util 只有一个 npm 包,按照目录来组织不同类型的方法,避免 monorepo 互相依赖
- 内容和 AntV 强相关,避免做和 lodash 等相同的工具库
- 不使用的方法,及时删除,并保持新增方法可以按需引入
- 旧版本的不维护,如果 AntV 技术栈的旧版本需要迭代,请升级到 v3
API
提供以下 Path 工具方法,包含转换、几何计算等。
path2String
将 PathArray 转换成字符串形式,不会对原始定义中的命令进行修改:
1const str: PathArray = [ 2 ['M', 10, 10], 3 ['L', 100, 100], 4 ['l', 10, 10], 5 ['h', 20], 6 ['v', 20], 7]; 8expect(path2String(str)).toEqual('M10 10L100 100l10 10h20v20');
path2Array
将 PathArray 转换成数组,不会对原始定义中的命令进行修改:
1const str = 'M10 10L100 100l10 10h20v20'; 2expect(path2Array(str)).toEqual([ 3 ['M', 10, 10], 4 ['L', 100, 100], 5 ['l', 10, 10], 6 ['h', 20], 7 ['v', 20], 8]);
path2Absolute
将定义中的相对命令转换成绝对命令,例如:
- l -> L
- h -> H
- v -> V
完整方法签名如下:
1path2Absolute(pathInput: string | PathArray): AbsoluteArray;
1const str: PathArray = [ 2 ['M', 10, 10], 3 ['L', 100, 100], 4 ['l', 10, 10], 5 ['h', 20], 6 ['v', 20], 7]; 8const arr = path2Absolute(str); 9expect(arr).toEqual([ 10 ['M', 10, 10], 11 ['L', 100, 100], 12 ['L', 110, 110], 13 ['H', 130], 14 ['V', 130], 15]);
path2Curve
将部分命令转曲,例如 L / A 转成 C 命令,借助 cubic bezier 易于分割的特性用于实现形变动画。 该方法内部会调用 path2Absolute,因此最终返回的 PathArray 中仅包含 M 和 C 命令。
完整方法签名如下:
1path2Curve(pathInput: string | PathArray): CurveArray;
1expect( 2 path2Curve([ 3 ['M', 0, 0], 4 ['L', 100, 100], 5 ]), 6).toEqual([ 7 ['M', 0, 0], 8 ['C', 44.194173824159215, 44.194173824159215, 68.75, 68.75, 100, 100], 9]);
clonePath
复制路径:
1const cloned = clonePath(pathInput);
reverseCurve
1const pathArray: CurveArray = [ 2 ['M', 170, 90], 3 ['C', 150, 90, 155, 10, 130, 10], 4 ['C', 105, 10, 110, 90, 90, 90], 5 ['C', 70, 90, 75, 10, 50, 10], 6 ['C', 25, 10, 30, 90, 10, 90], 7]; 8 9const reversed = reverseCurve(pathArray);
getPathBBox
获取几何定义下的包围盒,形如:
1export interface PathBBox { 2 width: number; 3 height: number; 4 x: number; 5 y: number; 6 x2: number; 7 y2: number; 8 cx: number; 9 cy: number; 10 cz: number; 11}
1const bbox = getPathBBox([['M', 0, 0], ['L', 100, 0], ['L', 100, 100], ['L', 0, 100], ['Z']]); 2 3expect(bbox).toEqual({ cx: 50, cy: 50, cz: 150, height: 100, width: 100, x: 0, x2: 100, y: 0, y2: 100 });
getTotalLength
获取路径总长度。
1const length = getTotalLength([['M', 0, 0], ['L', 100, 0], ['L', 100, 100], ['L', 0, 100], ['Z']]); 2 3expect(length).toEqual(400);
getPointAtLength
获取路径上从起点出发,到指定距离的点。
1const point = getPointAtLength([['M', 0, 0], ['L', 100, 0], ['L', 100, 100], ['L', 0, 100], ['Z']], 0); 2expect(point).toEqual({ x: 0, y: 0 });
getPathArea
计算路径包围的面积。内部实现中首先通过 path2Curve 转曲,再计算 cubic curve 面积,详见。
方法签名如下:
1function getPathArea(path: PathArray): number;
isPointInStroke
判断一个点是否在路径上,仅通过几何定义计算,不考虑其他样式属性例如线宽、lineJoin、miter 等。
方法签名如下:
1isPointInStroke(pathInput: string | PathArray, point: Point): boolean;
1const result = isPointInStroke(segments, { x: 10, y: 10 });
distanceSquareRoot
计算两点之间的距离。
方法签名如下:
1distanceSquareRoot(a: [number, number], b: [number, number]): number;
equalizeSegments
将两条路径处理成段数相同,用于形变动画前的分割操作。
1const [formattedPath1, formattedPath2] = equalizeSegments(path1, path2);
isPointInPolygon
判断一个点是否在多边形内。多边形形如:
1const polygon = [ 2 [0, 0], 3 [0, 100], 4 [30, 100], 5 [30, 0], 6]; 7 8// [0, 0] 在多边形的边上 9isPointInPolygon(polygon, 0, 0); // true
isPolygonsIntersect
判断两个多边形是否相交:
1isPolygonsIntersect(polygon1, polygon2);
Benchmarks
Build first.
1yarn run benchmarks
We can get the following output in the console, it can be seen that the same method from 5.0 is ~3 times faster than 4.0.
1// logs 2// Path2String#4.0 x 14,795 ops/sec ±3.35% (79 runs sampled) 3// Path2String#5.0 x 51,710 ops/sec ±2.05% (85 runs sampled) 4// Fastest is Path2String#5.0 5 6// Path2Absolute#4.0 x 14,524 ops/sec ±2.55% (80 runs sampled) 7// Path2Absolute#5.0 x 35,120 ops/sec ±3.10% (81 runs sampled) 8// Fastest is Path2Absolute#5.0
License
MIT@AntV.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
3 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 3
Reason
Found 10/30 approved changesets -- score normalized to 3
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/antvis/util/build.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/build.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/antvis/util/build.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/build.yml:18
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 1 out of 2 npmCommand dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/build.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 26 are checked with a SAST tool
Score
4.6
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More